【应用篇】Activiti监听与业务颗粒结合的简单应用(一)

本文通过实例演示了如何使用Activiti流程引擎实现简单的业务流程管理。通过定义流程、配置监听器及用户任务,实现了流程审批的功能,并展示了启动流程的具体代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程。

 

新建一个普通类

[java]  view plain copy print ?
  1. package com.tgb.itoo.activiti.controller;  
  2.   
  3. public class jy {  
  4.   
  5.     public static void SayHello(){  
  6.         System.out.println("jingyuan88");  
  7.     }  
  8. }  

新建一个监听类

[java]  view plain copy print ?
  1. package com.tgb.itoo.activiti.controller;  
  2.   
  3. import org.activiti.engine.delegate.DelegateTask;  
  4. import org.activiti.engine.delegate.TaskListener;  
  5.   
  6. public class jingyuanDelegate implements TaskListener{  
  7.       
  8.     @Override  
  9.     public void notify(DelegateTask delegateTask) {       
  10.         jy.SayHello();  
  11.     }  
  12.       
  13. }  


类似的再建两个类(ghy,xs)与两个监听(guhaiyanDelegate,xvshuDelegate)

 

画如下流程图:


配置相应的属性

 

每个类上添加相应的监听 如图:



给每条流向添加变量 如图:



最终生成的配置文件 如下:


[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <process id="myProcess" name="My process" isExecutable="true">  
  4.     <startEvent id="startevent1" name="Start"></startEvent>  
  5.     <userTask id="xvshu" name="xvshu">  
  6.       <extensionElements>  
  7.         <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.xvshuDelegate"></activiti:taskListener>  
  8.       </extensionElements>  
  9.     </userTask>  
  10.     <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="xvshu"></sequenceFlow>  
  11.     <userTask id="jingyuan" name="jingyuan">  
  12.       <extensionElements>  
  13.         <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.jingyuanDelegate"></activiti:taskListener>  
  14.       </extensionElements>  
  15.     </userTask>  
  16.     <userTask id="usertask1" name="guhaiyan">  
  17.       <extensionElements>  
  18.         <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.guhaiyanDelegate"></activiti:taskListener>  
  19.       </extensionElements>  
  20.     </userTask>  
  21.     <endEvent id="endevent1" name="End"></endEvent>  
  22.     <sequenceFlow id="flow4" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>  
  23.     <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>  
  24.     <sequenceFlow id="flow5" sourceRef="xvshu" targetRef="exclusivegateway1"></sequenceFlow>  
  25.     <sequenceFlow id="flow6" name="同意" sourceRef="exclusivegateway1" targetRef="jingyuan">  
  26.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression>  
  27.     </sequenceFlow>  
  28.     <sequenceFlow id="flow7" name="不同意" sourceRef="exclusivegateway1" targetRef="endevent1">  
  29.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!result}]]></conditionExpression>  
  30.     </sequenceFlow>  
  31.     <exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway>  
  32.     <sequenceFlow id="flow8" sourceRef="jingyuan" targetRef="exclusivegateway2"></sequenceFlow>  
  33.     <sequenceFlow id="flow9" name="同意" sourceRef="exclusivegateway2" targetRef="usertask1">  
  34.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression>  
  35.     </sequenceFlow>  
  36.     <sequenceFlow id="flow10" name="不同意" sourceRef="exclusivegateway2" targetRef="endevent1">  
  37.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!result}]]></conditionExpression>  
  38.     </sequenceFlow>  
  39.   </process>  
  40.   <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">  
  41.     <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">  
  42.       <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">  
  43.         <omgdc:Bounds height="35.0" width="35.0" x="70.0" y="150.0"></omgdc:Bounds>  
  44.       </bpmndi:BPMNShape>  
  45.       <bpmndi:BPMNShape bpmnElement="xvshu" id="BPMNShape_xvshu">  
  46.         <omgdc:Bounds height="55.0" width="105.0" x="150.0" y="140.0"></omgdc:Bounds>  
  47.       </bpmndi:BPMNShape>  
  48.       <bpmndi:BPMNShape bpmnElement="jingyuan" id="BPMNShape_jingyuan">  
  49.         <omgdc:Bounds height="55.0" width="105.0" x="410.0" y="140.0"></omgdc:Bounds>  
  50.       </bpmndi:BPMNShape>  
  51.       <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">  
  52.         <omgdc:Bounds height="55.0" width="105.0" x="710.0" y="140.0"></omgdc:Bounds>  
  53.       </bpmndi:BPMNShape>  
  54.       <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">  
  55.         <omgdc:Bounds height="35.0" width="35.0" x="745.0" y="270.0"></omgdc:Bounds>  
  56.       </bpmndi:BPMNShape>  
  57.       <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">  
  58.         <omgdc:Bounds height="40.0" width="40.0" x="300.0" y="147.0"></omgdc:Bounds>  
  59.       </bpmndi:BPMNShape>  
  60.       <bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">  
  61.         <omgdc:Bounds height="40.0" width="40.0" x="560.0" y="148.0"></omgdc:Bounds>  
  62.       </bpmndi:BPMNShape>  
  63.       <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">  
  64.         <omgdi:waypoint x="105.0" y="167.0"></omgdi:waypoint>  
  65.         <omgdi:waypoint x="150.0" y="167.0"></omgdi:waypoint>  
  66.       </bpmndi:BPMNEdge>  
  67.       <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">  
  68.         <omgdi:waypoint x="762.0" y="195.0"></omgdi:waypoint>  
  69.         <omgdi:waypoint x="762.0" y="270.0"></omgdi:waypoint>  
  70.       </bpmndi:BPMNEdge>  
  71.       <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">  
  72.         <omgdi:waypoint x="255.0" y="167.0"></omgdi:waypoint>  
  73.         <omgdi:waypoint x="300.0" y="167.0"></omgdi:waypoint>  
  74.       </bpmndi:BPMNEdge>  
  75.       <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">  
  76.         <omgdi:waypoint x="340.0" y="167.0"></omgdi:waypoint>  
  77.         <omgdi:waypoint x="410.0" y="167.0"></omgdi:waypoint>  
  78.         <bpmndi:BPMNLabel>  
  79.           <omgdc:Bounds height="14.0" width="24.0" x="350.0" y="167.0"></omgdc:Bounds>  
  80.         </bpmndi:BPMNLabel>  
  81.       </bpmndi:BPMNEdge>  
  82.       <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">  
  83.         <omgdi:waypoint x="320.0" y="187.0"></omgdi:waypoint>  
  84.         <omgdi:waypoint x="320.0" y="287.0"></omgdi:waypoint>  
  85.         <omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>  
  86.         <bpmndi:BPMNLabel>  
  87.           <omgdc:Bounds height="14.0" width="36.0" x="330.0" y="243.0"></omgdc:Bounds>  
  88.         </bpmndi:BPMNLabel>  
  89.       </bpmndi:BPMNEdge>  
  90.       <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">  
  91.         <omgdi:waypoint x="515.0" y="167.0"></omgdi:waypoint>  
  92.         <omgdi:waypoint x="560.0" y="168.0"></omgdi:waypoint>  
  93.       </bpmndi:BPMNEdge>  
  94.       <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">  
  95.         <omgdi:waypoint x="600.0" y="168.0"></omgdi:waypoint>  
  96.         <omgdi:waypoint x="710.0" y="167.0"></omgdi:waypoint>  
  97.         <bpmndi:BPMNLabel>  
  98.           <omgdc:Bounds height="14.0" width="24.0" x="610.0" y="168.0"></omgdc:Bounds>  
  99.         </bpmndi:BPMNLabel>  
  100.       </bpmndi:BPMNEdge>  
  101.       <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">  
  102.         <omgdi:waypoint x="580.0" y="188.0"></omgdi:waypoint>  
  103.         <omgdi:waypoint x="580.0" y="287.0"></omgdi:waypoint>  
  104.         <omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>  
  105.         <bpmndi:BPMNLabel>  
  106.           <omgdc:Bounds height="14.0" width="36.0" x="590.0" y="243.0"></omgdc:Bounds>  
  107.         </bpmndi:BPMNLabel>  
  108.       </bpmndi:BPMNEdge>  
  109.     </bpmndi:BPMNPlane>  
  110.   </bpmndi:BPMNDiagram>  
  111. </definitions>  

启动流程对应的代码:


[java]  view plain copy print ?
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import org.activiti.engine.ProcessEngine;  
  8. import org.activiti.engine.ProcessEngineConfiguration;  
  9. import org.activiti.engine.RepositoryService;  
  10. import org.activiti.engine.RuntimeService;  
  11. import org.activiti.engine.TaskService;  
  12. import org.activiti.engine.runtime.ProcessInstance;  
  13. import org.activiti.engine.task.Task;  
  14.   
  15. public class testDemo {  
  16.   
  17.     private static String readDataFromConsole(String prompt) {  
  18.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  19.         String str = null;  
  20.         try {  
  21.             System.out.print(prompt);  
  22.             str = br.readLine();  
  23.   
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.         return str;  
  28.     }  
  29.   
  30.     public static void main(String[] args) {  
  31.   
  32.         String str = readDataFromConsole("Please input string:");  
  33.         System.out.println("The information from console: " + str);  
  34.         // 创建流程引擎         
  35.         ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration  
  36.                 .createStandaloneProcessEngineConfiguration();  
  37.         // 连接数据库的配置  
  38.         processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");  
  39.         processEngineConfiguration  
  40.                 .setJdbcUrl("jdbc:mysql://localhost:3306/activitiexam?useUnicode=true&characterEncoding=utf8");  
  41.         processEngineConfiguration.setJdbcUsername("root");  
  42.         processEngineConfiguration.setJdbcPassword("root");  
  43.         ProcessEngine processEngine = processEngineConfiguration  
  44.                 .buildProcessEngine();  
  45.   
  46.         System.out.println(processEngine);  
  47.         // 获取流程存储服务组件  
  48.         RepositoryService repositoryService = processEngine  
  49.                 .getRepositoryService();  
  50.   
  51.         // 获取运行时服务组件  
  52.         RuntimeService runtimeService = processEngine.getRuntimeService();  
  53.   
  54.         // 获取流程任务组件  
  55.         TaskService taskService = processEngine.getTaskService();  
  56.   
  57.         // 1、部署流程文件  
  58.         repositoryService.createDeployment().name("MyProcess")  
  59.                 .addClasspathResource("diagrams/" + str + ".bpmn").deploy();  
  60.   
  61.         // 2、启动流程  
  62.         ProcessInstance processInstance = runtimeService  
  63.                 .startProcessInstanceByKey("myProcess");  
  64.         String end="1";//processInstance.getId() != null  
  65.         while (end.equalsIgnoreCase("1")) {  
  66.             // 3、查询第一个任务  
  67.             Task task = taskService.createTaskQuery()  
  68.                     .processInstanceId(processInstance.getId()).singleResult();  
  69.             ;  
  70.             if (task != null) {  
  71.                 System.out.println("============" + task.getId()  
  72.                         + "============" + task.getName() + "============");  
  73.                 str = readDataFromConsole("Please input result:");  
  74.                 Map<String, Object> variables = new HashMap<String, Object>();  
  75.                 Boolean result;  
  76.                 if (str.equalsIgnoreCase("true")) {  
  77.                     result = true;  
  78.                 } else {  
  79.                     result = false;  
  80.   
  81.                 }  
  82.                 variables.put("result", result);  
  83.                 taskService.complete(task.getId(), variables); // 完成任务  
  84.   
  85.             }else {  
  86.                 end="0";  
  87.                 System.out.println("任务已经完成");  
  88.             }   
  89.   
  90.         }  
  91.   
  92.     }  
  93.   
  94. }  


执行结果:




总结:

 

以上简单的拖拽实现是依赖于监听实现,监听的实现就必须实现相应的接口,这样对我们的业务颗粒就有所要求,当然以上只是简单实现,而且实现的方式是一个类对应一个监听,接下来会继续完善,应用多个类对应一个监听来实现。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值