An overview,
Introduction: After we get the process instance, we need to advance the process, that is, to move the execution flow forward. This article describes how to automate processes in different situations. There are three main types:
- (1) ReceiveTask is executed after receiving signals.
- (2) The intermediate signal capture event is executed after receiving the signal;
- ③ The intermediate message capture event is executed after receiving the message.
2. ReceiveTask Receives signals
- The BPMN file is as follows:
- The encoding is as follows:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("receiveTask.bpmn").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
// Get the process instance ID, which is the id of the primary execution flow
System.out.println("Process instance ID :" + processInstance.getId());
// Get the execution flow
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).onlyChildExecutions().singleResult();
// Use ACT_ID_ to identify the execution flow, which corresponds to the ID of the receiveTask label in the BPMN file
System.out.println("Current execution flow:" + execution.getActivityId());
Thread.sleep(10000);
// Call the method to continue the process. The id of the execution flow is required to trigger the process. (Here BPMN uses ReceiveTask to receive the trigger.
runtimeService.trigger(execution.getId());
// Query the current execution flow again
execution = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).onlyChildExecutions().singleResult();
// Use ACT_ID_ to identify the execution flow
System.out.println("Current execution flow:" + execution.getActivityId());
/ / close
processEngine.close();
System.exit(0);
Copy the code
- Print the results, the specific database can view.
Intermediate signal capture event
- BPMN is as follows:
- The XML needs to add a signal definition so that the event can be captured after the signal is sent.
<! -- Signal definition -->
<signal id="testSignal" name="testSignal2"></signal>
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_2" name="StartEvent"/>
<intermediateCatchEvent id="IntermediateCatchingEvent" name="IntermediateCatchingEvent" >
<! -- Signal definition reference -->
<signalEventDefinition signalRef="testSignal" ></signalEventDefinition>
</intermediateCatchEvent>
<userTask activiti:exclusive="true" id="UserTask" name="UserTask"/>
<endEvent id="_5" name="EndEvent"/>
<sequenceFlow id="_6" sourceRef="_2" targetRef="IntermediateCatchingEvent"/>
<sequenceFlow id="_7" sourceRef="IntermediateCatchingEvent" targetRef="UserTask"/>
<sequenceFlow id="_8" sourceRef="UserTask" targetRef="_5"/>
</process>
Copy the code
- The encoding is as follows:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("SingleEvent.bpmn").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
System.out.println("Primary flow ID:" + processInstance.getId());
SingleResult (); // List ()
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).onlyChildExecutions().singleResult();
System.out.println("Current execution flow :" + execution.getActivityId());
// Send a signal to continue the execution of the flow (without specifying the flow ID), after testing: the signal name here is the same as the name value defined in the BPMN file
runtimeService.signalEventReceived("testSignal2");
// runtimeService.signalEventReceived("testSignal2",execution.getId());
// Get the current subexecution flow
execution = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).onlyChildExecutions().singleResult();
System.out.println("Current execution flow :" + execution.getActivityId());
processEngine.close();
System.exit(0);
Copy the code
- The results are as follows:
Intermediate message capture events
- BPMN files
- The XML needs to add a message definition so that the message can be captured after it is sent
<! -- Message definition -->
<message id="messageId" name="messageName"></message>
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_2" name="StartEvent"/>
<intermediateCatchEvent id="IntermediateCatchingEvent" name="IntermediateCatchingEvent">
<! -- Message definition reference -->
<messageEventDefinition messageRef="messageId"></messageEventDefinition>
</intermediateCatchEvent>
<userTask activiti:exclusive="true" id="UserTask" name="UserTask"/>
<endEvent id="_5" name="EndEvent"/>
<sequenceFlow id="_6" sourceRef="_2" targetRef="IntermediateCatchingEvent"/>
<sequenceFlow id="_7" sourceRef="IntermediateCatchingEvent" targetRef="UserTask"/>
<sequenceFlow id="_8" sourceRef="UserTask" targetRef="_5"/>
</process>
Copy the code
- The encoding is as follows:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("MessageEvent.bpmn").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
System.out.println("Primary flow ID:" + processInstance.getId());
// Use the task method to obtain the execution stream
// Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
// Execution execution = runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult();
SingleResult (); // List ()
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).onlyChildExecutions().singleResult();
System.out.println("Current flow ID:" + execution.getId() + " 执行流名称:" + execution.getActivityId());
// Send a message to continue the execution flow with the same name as the name tag in the BPMN file message definition,
// The difference between a message intermediate event and a signal intermediate event is that a signal may not specify an execution flow ID, but a message must execute an execution flow ID. Just like the signal can be broadcast processing, the message is one-to-one.
runtimeService.messageEventReceived("messageName",execution.getId());
// Get the current execution flow
execution = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).onlyChildExecutions().singleResult();
System.out.println("Current flow ID:" + execution.getId() + " 执行流名称:" + execution.getActivityId());
processEngine.close();
System.exit(0);
Copy the code
- The results of
Five, the summary
- The intermediate signal capture event and the intermediate message capture event in the BPMN file use the same control, the difference is that one uses the signal definition, the other is the message definition.
- In the act_ru_execution database table for the execution flow, the execution flow uses the same execution flow ID as before, but the version has changed (in connection with the intermediate message capture event above).
- The intermediate signal capture event sends the signal to make the execution flow continue to execute (can not specify the execution flow ID), and the intermediate message capture event sends the message must specify the execution flow ID, thus: the signal can be broadcast processing, the message is one-to-one.