写在前面
对于工作流,我们使用最多的是用户任务节点
,用户任务节点就是给用户来生成任务的,需要人来手动的处理,而与之对应的还有服务任务节点
,这种类型的节点不需要人手动的参与而是程序来执行,即执行某个类的某个方法
,这个类一般是org.activiti.engine.delegate.JavaDelegate
的子类。下面我们一起来看下如何使用。
1:测试
1.1:设计流程
- 效果图
其中服务节点执行的类设置如下:
类定义如下:
package com.jh.activiti;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class MyServiceTaskDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("服务节点对应的类自动执行了!!!");
}
}
- xml
<?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="csdnServiceTask" name="csdnServiceTask" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask1" name="服务任务节点" activiti:class="com.jh.activiti.MyServiceTaskDelegate"></serviceTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_csdnServiceTask">
<bpmndi:BPMNPlane bpmnElement="csdnServiceTask" id="BPMNPlane_csdnServiceTask">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="190.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
<omgdc:Bounds height="55.0" width="105.0" x="410.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="720.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="225.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="410.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="515.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="720.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
1.2:部署流程
@Test
public void deploy() {
Deployment deployment = repositoryService.createDeployment() // 创建部署
.addClasspathResource("com/jh/activiti/csdnServiceTask.bpmn20.xml") // 加载流程资源文件
.name("csdn服务节点测试111") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
1.3:启动实例
@Test
public void startInstance() {
runtimeService.startProcessInstanceByKey("csdnServiceTask");
}
因为是自动执行,所以,启动实例后就会执行了,输出如下: