Activiti在代码中的流程
1.RepositoryService发布流程定义-》2.RuntimeService启动流程定义(并将基础信息保存到流程里)-》3.相关人员审批(根据业务决定 这里是审批人通过或不通过 )-》4.通过后完成回调方法
相关代码
1.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1519292565334" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="test" isClosed="false" isExecutable="true" name="测试" processType="None">
<documentation id="test_D_1"><![CDATA[测试]]></documentation>
<extensionElements>
<activiti:executionListener delegateExpression="${testExecutionListener}" event="end"/>
</extensionElements>
<startEvent activiti:initiator="startUserId" id="startevent1" name="流程开始"/>
<endEvent id="endevent1" name="流程结束"/>
<userTask activiti:assignee="#{userId}" activiti:exclusive="true" id="usertask1" name="选择审批人"/>
<receiveTask id="waitState" name="wait" />
<exclusiveGateway gatewayDirection="Unspecified" id="exclusivegateway1" name="审批人审批">
<extensionElements>
<activiti:executionListener delegateExpression="${testAuditListener}" event="start"/>
</extensionElements>
</exclusiveGateway>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"/>
<sequenceFlow id="flow5" sourceRef="usertask1" targetRef="waitState"/>
<sequenceFlow id="flow2" sourceRef="waitState" targetRef="exclusivegateway1"/>
<sequenceFlow id="flow3" name="批准" sourceRef="exclusivegateway1" targetRef="endevent1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[#{approval=='approve'}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4" name="驳回" sourceRef="exclusivegateway1" targetRef="usertask1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[#{approval=='reject'}]]></conditionExpression>
</sequenceFlow>
</process>
<bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
<bpmndi:BPMNPlane bpmnElement="test">
<bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="Shape-exclusivegateway1" isMarkerVisible="false">
<omgdc:Bounds height="32.0" width="32.0" x="125.0" y="300.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="Shape-usertask1">
<omgdc:Bounds height="55.0" width="85.0" x="100.0" y="195.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="Shape-endevent1">
<omgdc:Bounds height="32.0" width="32.0" x="125.0" y="435.5"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="startevent1" id="Shape-startevent1">
<omgdc:Bounds height="32.0" width="32.0" x="125.0" y="70.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1" sourceElement="startevent1" targetElement="usertask1">
<omgdi:waypoint x="141.0" y="102.0"/>
<omgdi:waypoint x="141.0" y="196.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2" sourceElement="usertask1" targetElement="exclusivegateway1">
<omgdi:waypoint x="141.0" y="251.0"/>
<omgdi:waypoint x="141.0" y="300.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3" sourceElement="exclusivegateway1" targetElement="endevent1">
<omgdi:waypoint x="141.0" y="332.0"/>
<omgdi:waypoint x="141.0" y="436.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="1.0" width="0.0" x="0.0" y="4.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4" sourceElement="exclusivegateway1" targetElement="usertask1">
<omgdi:waypoint x="141.0" y="300.0"/>
<omgdi:waypoint x="141.0" y="251.0"/>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="5.0" width="0.0" x="0.0" y="-4.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
2.选择审批人进行审批 通过后结束 不通过打回
controller
@RequestMapping(value = "startProcess", method = RequestMethod.GET)
public String startProcess(@RequestParam("userId") String userId, ModelMap modelMap) {
workFlowService.startProcess(userId, "2");
return "index";
}
service
@Override public void startProcess(String userId,String auditUserId) { //设置流程变量 Map<String, Object> variables = new HashMap<>(); //审批人 variables.put("userId", auditUserId); //用来设置启动流程的人员ID,引擎会自动把用户ID保存到activiti:initiator中 identityService.setAuthenticatedUserId(userId); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("test", userId, variables); //定义流程名称 runtimeService.setProcessInstanceName(processInstance.getProcessInstanceId(), "test"); //启动工作流 org.activiti.engine.task.Task task = actTaskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).singleResult(); actTaskService.complete(task.getId()); }
3.
controller
@RequestMapping(value = "processState", method = RequestMethod.GET) public String processState(@RequestParam("userId") String userId, ModelMap modelMap) { List<HistoricTaskInstance> tasks = workFlowService.getHistoryService().createHistoricTaskInstanceQuery().taskAssignee(userId).processUnfinished().list(); tasks.forEach(p -> { workFlowService.processState(p.getProcessInstanceId()); }); return "index"; }
service
public void processState(String processId) { Execution execution1 = runtimeService .createExecutionQuery() .processInstanceId(String.valueOf(processId))//流程实例ID .singleResult(); runtimeService.setVariable(execution1.getId(),ProcessKey.APPROVAL, "approve"); runtimeService.signal(execution1.getId()); }
4.
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class TestExecutionListener implements ExecutionListener {
@Resource
private ITestService testService;
@Override
public void notify(DelegateExecution execution) throws Exception {
String eventName = execution.getEventName();
if (EVENTNAME_END.equals(eventName)) {
int startUserId = Integer.parseInt(execution.getProcessBusinessKey());
// 完成后回调方法
testService.completeCreateInitiative(startUserId);
}
}
}