Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程,此次的监听我们应用抽象的监听来实现,也就是说所有的普通业务类均应用此抽象监听,而不需要每一个类一个监听的来操作。
新建两个普通类:
package com.tgb.itoo.activiti.controller; public class milaoshi { public static void SayHello(){ System.out.println("milaoshi--sayHello--"); } /* public static void SayHi(){ System.out.println("milaoshi--sayHi--"); }*/ } package com.tgb.itoo.activiti.controller; public class tanghuan { public static void SayHello(){ System.out.println("tanghuan--sayHello--"); } public static void SayHi(){ System.out.println("tanghuan--sayHi--"); } }
抽象监听类:
package com.tgb.itoo.activiti.controller; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import org.activiti.engine.delegate.DelegateTask; import org.activiti.engine.delegate.TaskListener; public class CommonDelegate implements TaskListener{ static Map<Object,Object> map; public CommonDelegate(){} public CommonDelegate(Map<Object,Object> map){ this.map=map; } @Override public void notify(DelegateTask delegateTask) { Object obj=map.get("object"); Class clazz=obj.getClass(); Method[] methods=clazz.getDeclaredMethods(); for(int i=0;i<methods.length;i++ ){ try { Method beforMehod = clazz.getMethod(methods[i].getName()); beforMehod.invoke(obj); } catch (NoSuchMethodException | SecurityException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
业务流程图 如图(tanghuan类和milaoshi类均绑定同一个监听):
流程变量 如图(不同意如图,同意为${result}):
最终生成的配置文件 如下:
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="myProcess" name="My process" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <userTask id="usertask1" name="tanghuan"> <extensionElements> <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.CommonDelegate"></activiti:taskListener> </extensionElements> </userTask> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow> <userTask id="usertask2" name="milaoshi"> <extensionElements> <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.CommonDelegate"></activiti:taskListener> </extensionElements> </userTask> <sequenceFlow id="flow3" name="同意" sourceRef="exclusivegateway1" targetRef="usertask2"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression> </sequenceFlow> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow4" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow> <sequenceFlow id="flow5" name="不同意" sourceRef="exclusivegateway1" targetRef="endevent1"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!result}]]></conditionExpression> </sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess"> <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="139.0" y="102.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> <omgdc:Bounds height="55.0" width="105.0" x="219.0" y="92.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1"> <omgdc:Bounds height="40.0" width="40.0" x="369.0" y="100.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"> <omgdc:Bounds height="55.0" width="105.0" x="454.0" y="93.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="489.0" y="222.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="174.0" y="119.0"></omgdi:waypoint> <omgdi:waypoint x="219.0" y="119.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="324.0" y="119.0"></omgdi:waypoint> <omgdi:waypoint x="369.0" y="120.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="409.0" y="120.0"></omgdi:waypoint> <omgdi:waypoint x="454.0" y="120.0"></omgdi:waypoint> <bpmndi:BPMNLabel> <omgdc:Bounds height="14.0" width="100.0" x="419.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> <omgdi:waypoint x="506.0" y="148.0"></omgdi:waypoint> <omgdi:waypoint x="506.0" y="222.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"> <omgdi:waypoint x="389.0" y="140.0"></omgdi:waypoint> <omgdi:waypoint x="389.0" y="239.0"></omgdi:waypoint> <omgdi:waypoint x="489.0" y="239.0"></omgdi:waypoint> <bpmndi:BPMNLabel> <omgdc:Bounds height="14.0" width="100.0" x="399.0" y="201.0"></omgdc:Bounds> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
启动流程对应的代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import com.tgb.itoo.activiti.controller.CommonDelegate; import com.tgb.itoo.activiti.controller.milaoshi; import com.tgb.itoo.activiti.controller.tanghuan; public class MapDemo { private static String readDataFromConsole(String prompt) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; try { System.out.print(prompt); str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public static void main(String[] args) { String str = readDataFromConsole("Please input string:"); System.out.println("The information from console: " + str); // 创建流程引擎 ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration .createStandaloneProcessEngineConfiguration(); // 连接数据库的配置 processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver"); processEngineConfiguration .setJdbcUrl("jdbc:mysql://localhost:3306/activitiexam?useUnicode=true&characterEncoding=utf8"); processEngineConfiguration.setJdbcUsername("root"); processEngineConfiguration.setJdbcPassword("root"); ProcessEngine processEngine = processEngineConfiguration .buildProcessEngine(); System.out.println(processEngine); // 获取流程存储服务组件 RepositoryService repositoryService = processEngine .getRepositoryService(); // 获取运行时服务组件 RuntimeService runtimeService = processEngine.getRuntimeService(); // 获取流程任务组件 TaskService taskService = processEngine.getTaskService(); // 1、部署流程文件 repositoryService.createDeployment().name("MyProcess") .addClasspathResource("diagrams/" + str + ".bpmn").deploy(); Map<Object,Object> map=new HashMap<Object,Object>(); tanghuan th=new tanghuan(); milaoshi mxj=new milaoshi(); //向map里面扔对象 map.put("object", th); /*map.put("milaoshi", mxj);*/ CommonDelegate common=new CommonDelegate(map); // 2、启动流程 ProcessInstance processInstance = runtimeService .startProcessInstanceByKey("myProcess"); String end="1";//processInstance.getId() != null while (end.equalsIgnoreCase("1")) { map.remove("object"); map.put("object", mxj); // 3、查询第一个任务 Task task = taskService.createTaskQuery() .processInstanceId(processInstance.getId()).singleResult(); ; if (task != null) { System.out.println("============" + task.getId() + "============" + task.getName() + "============"); str = readDataFromConsole("Please input result:"); Map<String, Object> variables = new HashMap<String, Object>(); Boolean result; if (str.equalsIgnoreCase("true")) { result = true; } else { result = false; } variables.put("result", result); taskService.complete(task.getId(), variables); // 完成任务 }else { end="0"; System.out.println("任务已经完成"); } } } }
执行结果:
总结:
以上这种方式主要是应用了反射技术实现,我们在客户端动态的向监听内扔对象,根据扔的对象来去执行对应的方法,减少了我们编写监听的个数。