首页 > 代码库 > 工作流学习笔记-------第二天

工作流学习笔记-------第二天

1.网关测试

技术分享技术分享

 

技术分享
package cn.itcast.bpmn.gateWay;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.zip.ZipInputStream;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.repository.Deployment;import org.activiti.engine.repository.DeploymentBuilder;import org.activiti.engine.runtime.ProcessInstance;import org.junit.Test;/** * 网关测试  * @author zhaoqx * */public class GatWayTest {    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();    // 部署流程定义(排他网关)    @Test    public void test1() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/bpmn/gateWay/gateWay_1.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }        //启动流程实例    @Test    public void test2(){        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("test7");        System.out.println(pi.getId());    }        //办理任务    @Test    public void test3(){        String taskId = "3804";        Map<String, Object> variables = new HashMap<String, Object>();        variables.put("qjts", 5);        processEngine.getTaskService().complete(taskId, variables);    }        //--------------------------------------------    // 部署流程定义(并行网关)    @Test    public void test4() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/bpmn/gateWay/gateWay_2.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }        //启动流程    @Test    public void test5(){        String processDefinitionKey = "test8";        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey );        System.out.println(pi.getId());    }        //办理任务    @Test    public void test6(){        String taskId = "4402";        processEngine.getTaskService().complete(taskId );    }}
View Code

2.监听测试

a.执行

技术分享技术分享

 

技术分享
package cn.itcast.bpmn.listener.execution;import org.activiti.engine.delegate.DelegateExecution;import org.activiti.engine.delegate.ExecutionListener;/** * 执行监听器实现 *  * @author zhaoqx *  */public class ExecutionListenerImpl implements ExecutionListener {    public void notify(DelegateExecution execution) throws Exception {        String id = execution.getId();        String eventName = execution.getEventName();        Object value = execution.getVariable("key1");        System.out.println("监听器执行了。。 id = " + id + " ,eventName = " + eventName                + " ,key1 = " + value);    }}
View Code
技术分享
package cn.itcast.bpmn.listener.execution;import org.activiti.engine.delegate.DelegateExecution;import org.activiti.engine.delegate.ExecutionListener;/** * 执行监听器实现 *  * @author zhaoqx *  */public class ExecutionListenerImpl2 implements ExecutionListener {    public void notify(DelegateExecution execution) throws Exception {        System.out.println("流程结束了。。。");    }}
View Code
技术分享
package cn.itcast.bpmn.listener.execution;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.zip.ZipInputStream;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.repository.Deployment;import org.activiti.engine.repository.DeploymentBuilder;import org.activiti.engine.runtime.ProcessInstance;import org.junit.Test;/** * 执行监听器测试  * @author zhaoqx * */public class ExecutionListenerTest {    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();    // 部署流程定义    @Test    public void test1() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/bpmn/listener/execution/execution.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }        //启动流程实例    @Test    public void test2(){        String processDefinitionKey = "test5";        Map<String, Object> variables = new HashMap<String, Object>();        variables.put("key1", "value1");        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables);        System.out.println(pi.getId());    }        //办理任务    @Test    public void test3(){        String taskId = "3002";        processEngine.getTaskService().complete(taskId );    }}
View Code

b.任务

技术分享
apackage cn.itcast.bpmn.listener.task;import org.activiti.engine.delegate.DelegateTask;import org.activiti.engine.delegate.TaskListener;/** * 编写任务监听器实现 *  * @author zhaoqx *  */public class TaskListenerImpl implements TaskListener {    public void notify(DelegateTask delegateTask) {        String id = delegateTask.getId();        String assignee = delegateTask.getAssignee();        String executionId = delegateTask.getExecutionId();        Object value = delegateTask.getVariable("key2");        System.out.println("id = " + id + " ,assignee = " + assignee                + " executionId = " + executionId + " key2 = " + value);        System.out.println(assignee + "需要办理任务 : " + delegateTask.getName());    }}
View Code
技术分享
package cn.itcast.bpmn.listener.task;import org.activiti.engine.delegate.DelegateTask;import org.activiti.engine.delegate.TaskListener;/** * 编写任务监听器实现 *  * @author zhaoqx *  */public class TaskListenerImpl2 implements TaskListener {    public void notify(DelegateTask delegateTask) {        String id = delegateTask.getId();        String assignee = delegateTask.getAssignee();        String executionId = delegateTask.getExecutionId();        Object value = delegateTask.getVariable("key2");        System.out.println(assignee + "办理了任务:" + delegateTask.getName());    }}
View Code
技术分享
package cn.itcast.bpmn.listener.task;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.zip.ZipInputStream;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.repository.Deployment;import org.activiti.engine.repository.DeploymentBuilder;import org.activiti.engine.runtime.ProcessInstance;import org.junit.Test;/** * 任务监听器测试  * @author zhaoqx * */public class TaskListenerTest {    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();    // 部署流程定义    @Test    public void test1() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/bpmn/listener/task/task.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }        //启动流程实例    @Test    public void test2(){        String processDefinitionKey = "test5";        Map<String, Object> variables = new HashMap<String, Object>();        variables.put("key2", "value2");        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables);        System.out.println(pi.getId());    }        //办理任务    @Test    public void test3(){        String taskId = "3305";        processEngine.getTaskService().complete(taskId );    }}
View Code

3.组任务测试

技术分享
package cn.itcast.bpmn.task.group;import java.io.InputStream;import java.util.List;import java.util.zip.ZipInputStream;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.repository.Deployment;import org.activiti.engine.repository.DeploymentBuilder;import org.activiti.engine.repository.ProcessDefinition;import org.activiti.engine.repository.ProcessDefinitionQuery;import org.activiti.engine.runtime.ProcessInstance;import org.activiti.engine.task.Task;import org.activiti.engine.task.TaskQuery;import org.junit.Test;/** * 组任务测试 * @author zhaoqx * */public class GroupTaskTest {    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();    // 部署流程定义    @Test    public void test1() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/bpmn/task/group/group.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }    // 启动流程    @Test    public void test2() {        String processDefinitionKey = "test3";        ProcessInstance pi = processEngine.getRuntimeService()                .startProcessInstanceByKey(processDefinitionKey);        System.out.println(pi.getId());    }        //查询组任务列表    @Test    public void test3(){        TaskQuery query = processEngine.getTaskService().createTaskQuery();        query.taskCandidateUser("zhangsan");        List<Task> list = query.list();        for (Task task : list) {            System.out.println(task.getId() + " " + task.getName());        }    }        //拾取任务到个人任务列表    @Test    public void test4(){        String taskId = "1704";        String userId = "zhangsan";        processEngine.getTaskService().claim(taskId, userId);    }        //退回任务到组任务列表    @Test    public void test5(){        String taskId = "1704";        String userId = null;        processEngine.getTaskService().setAssignee(taskId, userId);    }        //直接分配任务    @Test    public void test6(){        String taskId = "1704";        String userId = "zhangsan";        processEngine.getTaskService().setAssignee(taskId, userId);    }        //办理个人任务    @Test    public void test7(){        String taskId = "1704";        processEngine.getTaskService().complete(taskId);    }}
View Code

4.接收任务测试

技术分享
package cn.itcast.bpmn.task.receive;import java.io.InputStream;import java.util.zip.ZipInputStream;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.repository.Deployment;import org.activiti.engine.repository.DeploymentBuilder;import org.activiti.engine.runtime.ProcessInstance;import org.junit.Test;/** * 接收任务测试 * @author zhaoqx * */public class ReceiveTaskTest {    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();    // 部署流程定义    @Test    public void test1() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/bpmn/task/receive/receive.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }        //启动流程    @Test    public void test2(){        String processDefinitionKey = "test4";        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey );        System.out.println(pi.getId());    }    //办理任务    @Test    public void test3(){        processEngine.getTaskService().complete("2204");    }        //办理接收任务    @Test    public void test4(){        String executionId = "2201";        processEngine.getRuntimeService().signal(executionId );    }}
View Code

5.历史数据查询

技术分享

 

技术分享
package cn.itcast.history;import java.io.InputStream;import java.util.List;import java.util.zip.ZipInputStream;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.history.HistoricActivityInstance;import org.activiti.engine.history.HistoricActivityInstanceQuery;import org.activiti.engine.history.HistoricProcessInstance;import org.activiti.engine.history.HistoricProcessInstanceQuery;import org.activiti.engine.history.NativeHistoricProcessInstanceQuery;import org.activiti.engine.repository.Deployment;import org.activiti.engine.repository.DeploymentBuilder;import org.activiti.engine.repository.ProcessDefinition;import org.activiti.engine.repository.ProcessDefinitionQuery;import org.activiti.engine.runtime.ProcessInstance;import org.activiti.engine.task.Task;import org.activiti.engine.task.TaskQuery;import org.junit.Test;/** * 历史数据查询 *  * @author zhaoqx *  */public class HistoryQueryTest {    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();    // 部署流程定义    @Test    public void test1() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/history/history.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }    // 查询流程定义    @Test    public void test2() {        ProcessDefinitionQuery query = processEngine.getRepositoryService()                .createProcessDefinitionQuery();        List<ProcessDefinition> list = query.list();        for (ProcessDefinition pd : list) {            System.out.println(pd.getId() + " " + pd.getKey());        }    }    // 启动流程    @Test    public void test3() {        String processDefinitionKey = "myProcess";        ProcessInstance pi = processEngine.getRuntimeService()                .startProcessInstanceByKey(processDefinitionKey);        System.out.println(pi.getId());    }    // 查询任务    @Test    public void test4() {        TaskQuery query = processEngine.getTaskService().createTaskQuery();        List<Task> list = query.list();        for (Task task : list) {            System.out.println(task.getId() + " " + task.getName() + " "                    + task.getAssignee());        }    }    // 办理任务    @Test    public void test5() {        String taskId = "302";        processEngine.getTaskService().complete(taskId);    }    // 查询按照某个流程定义执行的流程实例数据(历史流程实例列表)    @Test    public void test6() {        // 流程实例查询对象        HistoricProcessInstanceQuery query = processEngine.getHistoryService()                .createHistoricProcessInstanceQuery();        String processDefinitionKey = "myProcess";        query.processDefinitionKey(processDefinitionKey);        query.orderByProcessInstanceEndTime().asc();        List<HistoricProcessInstance> list = query.list();        for (HistoricProcessInstance pi : list) {            System.out.println(pi.getId() + " " + pi.getProcessDefinitionId()                    + " " + pi.getStartTime() + " " + pi.getEndTime());        }    }    // 查询某个流程实例执行的步骤    @Test    public void test7() {        HistoricActivityInstanceQuery query = processEngine.getHistoryService()                .createHistoricActivityInstanceQuery();        String processInstanceId = "101";        query.processInstanceId(processInstanceId);        List<HistoricActivityInstance> list = query.list();        for (HistoricActivityInstance hai : list) {            System.out.println(hai.getActivityId() + " "                    + hai.getActivityName() + " " + hai.getAssignee() + " "                    + hai.getStartTime() + " " + hai.getEndTime());        }    }}
View Code

6.spring整合activiti测试

技术分享
<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans                         http://www.springframework.org/schema/beans/spring-beans.xsd                        http://www.springframework.org/schema/context                         http://www.springframework.org/schema/context/spring-context-2.5.xsd                        http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">        <!-- 数据源 -->    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql:///act_day2"/>        <property name="username" value="root"/>        <property name="password" value="root"/>    </bean>        <!-- 事务管理器 -->    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="ds"/>    </bean>        <!-- 配置spring流程引擎配置对象 -->    <bean id="conf" class="org.activiti.spring.SpringProcessEngineConfiguration">        <property name="dataSource" ref="ds"/>        <property name="databaseSchemaUpdate" value="true"/>        <property name="transactionManager" ref="txManager"/>    </bean>        <!-- 注册流程引擎对象 -->    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">        <property name="processEngineConfiguration" ref="conf"/>    </bean></beans>
View Code
技术分享
package cn.itcast.spring;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * spring整合activiti测试 * @author zhaoqx * */public class SpringActivitiTest {    public static void main(String[] args) {        //初始化spring工厂        ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/itcast/spring/beans.xml");        //从spring工厂中获取流程引擎对象        ProcessEngine pe = (ProcessEngine) ctx.getBean("processEngine");        System.out.println(pe);    }}
View Code

7.流程变量测试

技术分享

 

技术分享
package cn.itcast.variable;import java.io.InputStream;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.zip.ZipInputStream;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.repository.Deployment;import org.activiti.engine.repository.DeploymentBuilder;import org.activiti.engine.runtime.Execution;import org.activiti.engine.runtime.ProcessInstance;import org.activiti.engine.task.Task;import org.activiti.engine.task.TaskQuery;import org.junit.Test;/** * 流程变量测试 * @author zhaoqx * */public class VariableTest {    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();    // 部署流程定义    @Test    public void test1() {        DeploymentBuilder builder = processEngine.getRepositoryService()                .createDeployment();        InputStream in = this.getClass().getResourceAsStream(                "/cn/itcast/variable/variable.zip");        ZipInputStream zipInputStream = new ZipInputStream(in);        builder.addZipInputStream(zipInputStream);        Deployment deployment = builder.deploy();        System.out.println(deployment.getId());    }        //在启动流程实例时设置流程变量    @Test    public void test2(){        String processDefinitionKey = "test2";                Map<String, Object> variables = new HashMap<String, Object>();        variables.put("key1", "value1");        variables.put("user", new User("1", "test"));        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables );        System.out.println(pi.getId());    }        //在办理任务时设置流程变量    @Test    public void test3(){        String taskId = "905";        Map<String, Object> variables = new HashMap<String, Object>();        variables.put("qjts", 3);        variables.put("qjyy", "不想上班");        processEngine.getTaskService().complete(taskId, variables);    }        //使用RuntimeService的方法设置流程变量    @Test    public void test4(){        String executionId = "901";//执行Id,流程实例Id        String variableName = "key2";        Object value = "value2";        processEngine.getRuntimeService().setVariable(executionId, variableName, value);        Map<String, Object> variables = new HashMap<String, Object>();        variables.put("key3", "value3");        processEngine.getRuntimeService().setVariables(executionId, variables);    }        //使用TaskService的方法设置流程变量    @Test    public void test5(){        String taskId = "1004";        String variableName = "key4";        Object value = "value4";        processEngine.getTaskService().setVariable(taskId, variableName, value);    }        //使用TaskService获取流程变量    @Test    public void test6(){        String taskId = "1004";        Map<String, Object> variables = processEngine.getTaskService().getVariables(taskId);        Set<String> keys = variables.keySet();//获取当前map中所有key        for (String key : keys) {            Object value = variables.get(key);            System.out.println(key + " = " + value);        }    }        //使用RuntimeService获取流程变量    @Test    public void test7(){        String executionId = "901";        Map<String, Object> variables = processEngine.getRuntimeService().getVariables(executionId);        Set<String> keys = variables.keySet();//获取当前map中所有key        for (String key : keys) {            Object value = variables.get(key);            System.out.println(key + " = " + value);        }    }        //通过查询任务来获取执行Id,流程实例Id    @Test    public void test8(){        TaskQuery query = processEngine.getTaskService().createTaskQuery();        query.taskAssignee("李四");        List<Task> list = query.list();        for (Task task : list) {            String executionId = task.getExecutionId();            System.out.println("executionId = " + executionId);        }    }}
View Code
技术分享
package cn.itcast.variable;public class User implements java.io.Serializable{    private String id;    private String name;        public User() {}            public User(String id, String name) {        this.id = id;        this.name = name;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    }
View Code

 

工作流学习笔记-------第二天