Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程。
新建一个普通类
package com.tgb.itoo.activiti.controller;
public class jy {
public static void SayHello(){
System.out.println("jingyuan88");
}
}新建一个监听类
package com.tgb.itoo.activiti.controller;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
public class jingyuanDelegate implements TaskListener{
@Override
public void notify(DelegateTask delegateTask) {
jy.SayHello();
}
}
类似的再建两个类(ghy,xs)与两个监听(guhaiyanDelegate,xvshuDelegate)
画如下流程图:
配置相应的属性
每个类上添加相应的监听 如图:
给每条流向添加变量 如图:
最终生成的配置文件 如下:
<?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="xvshu" name="xvshu">
<extensionElements>
<activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.xvshuDelegate"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="xvshu"></sequenceFlow>
<userTask id="jingyuan" name="jingyuan">
<extensionElements>
<activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.jingyuanDelegate"></activiti:taskListener>
</extensionElements>
</userTask>
<userTask id="usertask1" name="guhaiyan">
<extensionElements>
<activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.guhaiyanDelegate"></activiti:taskListener>
</extensionElements>
</userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow4" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
<exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
<sequenceFlow id="flow5" sourceRef="xvshu" targetRef="exclusivegateway1"></sequenceFlow>
<sequenceFlow id="flow6" name="同意" sourceRef="exclusivegateway1" targetRef="jingyuan">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow7" name="不同意" sourceRef="exclusivegateway1" targetRef="endevent1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${!result}]]></conditionExpression>
</sequenceFlow>
<exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway>
<sequenceFlow id="flow8" sourceRef="jingyuan" targetRef="exclusivegateway2"></sequenceFlow>
<sequenceFlow id="flow9" name="同意" sourceRef="exclusivegateway2" targetRef="usertask1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow10" name="不同意" sourceRef="exclusivegateway2" 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="70.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="xvshu" id="BPMNShape_xvshu">
<omgdc:Bounds height="55.0" width="105.0" x="150.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="jingyuan" id="BPMNShape_jingyuan">
<omgdc:Bounds height="55.0" width="105.0" x="410.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="710.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="745.0" y="270.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
<omgdc:Bounds height="40.0" width="40.0" x="300.0" y="147.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">
<omgdc:Bounds height="40.0" width="40.0" x="560.0" y="148.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="105.0" y="167.0"></omgdi:waypoint>
<omgdi:waypoint x="150.0" y="167.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="762.0" y="195.0"></omgdi:waypoint>
<omgdi:waypoint x="762.0" y="270.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="255.0" y="167.0"></omgdi:waypoint>
<omgdi:waypoint x="300.0" y="167.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="340.0" y="167.0"></omgdi:waypoint>
<omgdi:waypoint x="410.0" y="167.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="24.0" x="350.0" y="167.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="320.0" y="187.0"></omgdi:waypoint>
<omgdi:waypoint x="320.0" y="287.0"></omgdi:waypoint>
<omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="36.0" x="330.0" y="243.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="515.0" y="167.0"></omgdi:waypoint>
<omgdi:waypoint x="560.0" y="168.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
<omgdi:waypoint x="600.0" y="168.0"></omgdi:waypoint>
<omgdi:waypoint x="710.0" y="167.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="24.0" x="610.0" y="168.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
<omgdi:waypoint x="580.0" y="188.0"></omgdi:waypoint>
<omgdi:waypoint x="580.0" y="287.0"></omgdi:waypoint>
<omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="36.0" x="590.0" y="243.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;
public class testDemo {
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();
// 2、启动流程
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey("myProcess");
String end="1";//processInstance.getId() != null
while (end.equalsIgnoreCase("1")) {
// 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("任务已经完成");
}
}
}
}
执行结果:
总结:
以上简单的拖拽实现是依赖于监听实现,监听的实现就必须实现相应的接口,这样对我们的业务颗粒就有所要求,当然以上只是简单实现,而且实现的方式是一个类对应一个监听,接下来会继续完善,应用多个类对应一个监听来实现。
原文:http://blog.csdn.net/hejingyuan6/article/details/46288957