公共spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <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="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="org.h2.Driver" /> <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="bean1" class="com.easyway.workflow.activiti.SentenceGenerator" /> <!-- 流程引擎配置对象 --> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> <property name="jobExecutorActivate" value="false" /> <!-- 数据库的表结构的创建方式 --> <property name="databaseSchemaUpdate" value="create-drop" /> <!-- 采用自动部署工作流activiti的方式 --> <property name="deploymentResources" value="classpath*:diagrams/*.bpmn20.xml" /> <!-- 设置需要暴露的bean对象 --> <property name="beans"> <map> <entry key="bean1" value-ref="bean1" /> </map> </property> </bean> <!-- 流程引擎对象的创建 --> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration" /> </bean> <!-- 创建各种流程引擎的服务 --> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" /> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /> <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" /> </beans>
package com.easyway.workflow.activiti; /** * * 工作流中的activiti中的UEL的两种用法要求: * 1.UEL使用的bean必须为spring暴露给activiti的bean。 * 2.如果采用方法必须为public * <!--访问方法表达式 --> * <serviceTask id="serviceTask" activiti:expression="${bean1.getSentence()}" /> * 3.如果采用属性方位则必须为属性提供getter/setter方法 * <!-- 访问属性的表达方式的 --> * <serviceTask id="serviceTask2" activiti:expression="${bean1.level}" /> * * * @author longgangbai * * 2011-12-18 下午09:48:36 */ public class SentenceGenerator { private String level; /** * @return the level */ public String getLevel() { return level; } /** * @param level the level to set */ public void setLevel(String level) { this.level = level; } public String getSentence() { return "Activiti BPMN 2.0 process engine"; } }
实例一:
配置:
<?xml version="1.0" encoding="UTF-8"?> <definitions id="definitions" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="bankService"> <!-- 全局共享变量是设置 --> <process id="springexclusiveGateway"> <startEvent id="theStart" /> <!-- 共享通道 --> <exclusiveGateway id="fork" name="abc"> <documentation> 可以理解为多种方式都可以达到目的地,但是适合自己的只有一条</documentation> </exclusiveGateway> <exclusiveGateway id="join" name="abc" /> <sequenceFlow id="flow1" sourceRef="theStart" targetRef="fork" /> <sequenceFlow id="flow2" sourceRef="fork" targetRef="receivePayment" > <!-- 使用xsi之前必须在命名空间中导入 xsi --> <conditionExpression xsi:type="tFormalExpression">${paymentReceived == false}</conditionExpression> </sequenceFlow> <sequenceFlow id="flow3" sourceRef="fork" targetRef="shipOrder" > <conditionExpression xsi:type="tFormalExpression">${shipOrder == true}</conditionExpression> </sequenceFlow> <userTask id="receivePayment" name="Receive Payment" /> <sequenceFlow id="flow4" sourceRef="receivePayment" targetRef="join" /> <userTask id="shipOrder" name="Ship Order" /> <sequenceFlow id="flow5" sourceRef="shipOrder" targetRef="join" /> <sequenceFlow id="flow6" sourceRef="join" targetRef="archiveOrder" /> <!-- 指定特定用户组中的特定的用户处理任务 --> <userTask id="archiveOrder" name="Archive Order" > <potentialOwner> <resourceAssignmentExpression> <formalExpression>user(kermit), group(management)</formalExpression> </resourceAssignmentExpression> </potentialOwner> </userTask> <sequenceFlow sourceRef="archiveOrder" targetRef="theEnd" /> <endEvent id="theEnd" /> </process> </definitions>
代码:
package com.easyway.workflow.activiti; import java.util.HashMap; /** * * <exclusiveGateway id="fork" name="abc"/> * 可以理解为多种方式都可以达到目的地,但是适合自己的只有一条 * * <!-- 指定特定用户组中的特定的用户处理任务 --> <userTask id="archiveOrder" name="Archive Order" > <potentialOwner> <resourceAssignmentExpression> <formalExpression>user(kermit), group(management)</formalExpression> </resourceAssignmentExpression> </potentialOwner> * * * 全局属性的使用 * * * @author longgangbai * * 2011-12-18 下午10:33:15 */ public class SpringExclusiveGateway { public static void main(String[] args) { //加载spring配置和工作流流程处理配置 ApplicationContext ctx=new ClassPathXmlApplicationContext("application-context-expression.xml"); //工作流相关的各种服务 HistoryService historyService=(HistoryService)ctx.getBean("historyService"); RuntimeService runtimeService=(RuntimeService)ctx.getBean("runtimeService"); TaskService taskService=(TaskService)ctx.getBean("taskService"); //设置调用流程过程中使用的参数 HashMap<String, Object> variableMap = new HashMap<String, Object>(); variableMap.put("paymentReceived", true); variableMap.put("shipOrder", true); //流程实例对象 ProcessInstance pi = runtimeService.startProcessInstanceByKey("springexclusiveGateway",variableMap); TaskQuery query = taskService.createTaskQuery() .processInstanceId(pi.getId()) .orderByTaskName() .asc(); List<Task> tasks = query.list(); System.out.println("tasksize ="+tasks.size()); Task task = tasks.get(0); System.out.println("Ship Order ="+task.getName()); } }
实例二:
配置:
<?xml version="1.0" encoding="UTF-8"?> <definitions id="definitions" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Expressions"> <process id="limitedExpressionProcess"> <startEvent id="start" /> <sequenceFlow id="flow1" sourceRef="start" targetRef="serviceTask" /> <!--访问方法表达式 --> <serviceTask id="serviceTask" name="定期会议开始" activiti:expression="${bean1.getSentence()}" > </serviceTask> <sequenceFlow id="flow2" sourceRef="serviceTask" targetRef="userTask" /> <userTask id="userTask" name="会议中....." /> <sequenceFlow id="flow3" sourceRef="userTask" targetRef="serviceTask2" /> <!-- Using bean that is available in app-context, but not mentioned in beans --> <!-- 访问属性的表达方式的 --> <serviceTask id="serviceTask2" name="定期会议结束...." activiti:expression="${bean1.level}" /> <sequenceFlow id="flow4" sourceRef="serviceTask2" targetRef="end" /> <endEvent id="end" /> </process> </definitions>
代码:
/** package com.easyway.workflow.activiti; import java.util.List; /** * 测试工作流中的UEL表达式的使用 * * @author longgangbai * * 2011-12-18 下午07:35:29 */ public class SpringLimitedExpressionActiviti { public static void main(String[] args) throws InterruptedException { //加载spring配置和工作流流程处理配置 ApplicationContext ctx=new ClassPathXmlApplicationContext("application-context-expression.xml"); //工作流相关的各种服务 HistoryService historyService=(HistoryService)ctx.getBean("historyService"); RuntimeService runtimeService=(RuntimeService)ctx.getBean("runtimeService"); TaskService taskService=(TaskService)ctx.getBean("taskService"); //流程实例对象 ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("limitedExpressionProcess"); //查询当前任务数据 System.out.println("处理任务之前:"+runtimeService.createProcessInstanceQuery().count()); List<Task> taskList=taskService.createTaskQuery().list(); //手动分配代办任务 for (Task task : taskList) { taskService.claim(task.getId(), "kermit"); } //手动处理完成任务 for (Task task : taskList) { taskService.complete(task.getId()); } //检查剩余任务数 System.out.println("处理任务之前:"+taskService.createTaskQuery().count()); // verify that the process is actually finished //验证流程是否处理完毕 HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getProcessInstanceId()).singleResult(); System.out.println("Process instance end time: " + historicProcessInstance.getEndTime()); //6分钟时间等待第二次执行 Thread.sleep(6*60*1000); } }
实例三
配置:
<?xml version="1.0" encoding="UTF-8"?> <definitions id="definitions" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="bankService"> <process id="springparallelGateway"> <startEvent id="theStart" /> <sequenceFlow id="flow1" sourceRef="theStart" targetRef="fork" /> <parallelGateway id="fork" > <documentation> 理解为多种方式都可以到达目的地,并行的互不干涉,进度</documentation> </parallelGateway> <sequenceFlow sourceRef="fork" targetRef="receivePayment" /> <sequenceFlow sourceRef="fork" targetRef="shipOrder" /> <!-- 给特定用户分配任务 --> <userTask id="receivePayment" name="Receive Payment" > <humanPerformer> <resourceAssignmentExpression> <formalExpression>kermit</formalExpression> </resourceAssignmentExpression> </humanPerformer> </userTask> <sequenceFlow sourceRef="receivePayment" targetRef="join" /> <userTask id="shipOrder" name="Ship Order" /> <sequenceFlow sourceRef="shipOrder" targetRef="join" /> <parallelGateway id="join" /> <sequenceFlow sourceRef="join" targetRef="archiveOrder" /> <userTask id="archiveOrder" name="Archive Order" /> <sequenceFlow sourceRef="archiveOrder" targetRef="theEnd" /> <endEvent id="theEnd" /> </process> </definitions>
代码:
/** package com.easyway.workflow.activiti; import java.util.List; /** * 测试工作流中的UEL表达式的使用 * * <parallelGateway id="fork" /> * 理解为多种方式都可以到达目的地,并行的互不干涉,进度 * * <!-- 给特定用户分配任务 --> <userTask id="receivePayment" name="Receive Payment" > <humanPerformer> <resourceAssignmentExpression> <formalExpression>kermit</formalExpression> </resourceAssignmentExpression> </humanPerformer> </userTask> * * @author longgangbai * * 2011-12-18 下午07:35:29 */ public class SpringParallelExpressionActiviti { public static void main(String[] args) throws InterruptedException { //加载spring配置和工作流流程处理配置 ApplicationContext ctx=new ClassPathXmlApplicationContext("application-context-expression.xml"); //工作流相关的各种服务 HistoryService historyService=(HistoryService)ctx.getBean("historyService"); RuntimeService runtimeService=(RuntimeService)ctx.getBean("runtimeService"); TaskService taskService=(TaskService)ctx.getBean("taskService"); //流程实例对象 ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("springparallelGateway"); TaskQuery query = taskService.createTaskQuery() .processInstanceId(processInstance.getId()) .orderByTaskName() .asc(); List<Task> tasks = query.list(); System.out.println(tasks.size()); Task task1 = tasks.get(0); System.out.println("Receive Payment ="+ task1.getName()); Task task2 = tasks.get(1); System.out.println("Ship Order"+task2.getName()); //查看指定人的任务 List<Task> kermittasks = taskService.createTaskQuery().taskAssignee("kermit").list(); System.out.println("kermit task number ="+ kermittasks.size()); } }