(3) suspension and activation of activiti process, activiti Process
Sometimes, we need to pause an executed process instead of deleting it. At this time, we need to call the activiti pause and activation APIs to operate on them.
Each time a process instance is started, a corresponding process task is generated under the process instance, which is in the relationship of more than 1 *. Each process instance runs under a node, one or more tasks will be generated in the current task table act_ru_task (generally one task with multiple branches ),
After a task is completed, we can see that the currently running task table act_ru_task will delete the task. In the history task table act_hi_taskinst (historyService), we can query all the tasks under the process instance,
From the above observations, we can conclude that the current task can be obtained from both taskService and historyService, but the current task is queried from taskService, because the task will be deleted after execution, with less data and fast query speed. In the same way,
We can query process instances from runtimeService and historyService, but runtimeService is preferred unless we query Historical Process instances.
(1) perform the following steps to test the pause process instance:
1. Start a process instance using the key or id defined in the process.
2. The process instance is suspended based on the id of the Process instance.
3. Obtain the id of the task corresponding to the next node. Call taskService to complete the task observation.
4. reactivate the process instance.
5. Continue to complete the process.
Run the following code:
1. The process instance is started. We have already demonstrated it in the previous code.
2. Use the id of the Process instance initiated above to suspend the process instance.
/*** Test to suspend a process instance */@ Test public void testSuspendProcessInstance () {RuntimeService runtimeService = engine. getRuntimeService (); String processInstanceId = "1801"; // The runtimeService of the process instance is suspended based on the id of the Process instance. suspendProcessInstanceById (processInstanceId );}
3. The next handler of the task completes the instance.
// Complete a process @ Test public void completeProcessInstance () {// task id. After integration, tasks of the user are queried as the current logon user, then obtain the id String taskId = "1804"; TaskService taskService = engine. getTaskService (); // complete taskService by task id. complete (taskId );}
An error is reported after execution:
The preceding information indicates that a suspended task cannot be completed.
4. Activate the process instance.
/*** Activate a process instance */@ Test public void testActivateProcessInstance () {RuntimeService runtimeService = engine. getRuntimeService (); String processInstanceId = "1801"; runtimeService. activateProcessInstanceById (processInstanceId );}
5. Complete the task again and execute OK.
(2) The execution steps for testing the pause process definition are as follows:
In a program, we need to pause a process definition, stop all process instances under the process definition, and do not allow the process instances that initiate the process definition, then we need to suspend the process definition.
1. Start a process instance (before the process definition is suspended)
2. Suspend the process definition corresponding to the above process instance
3. Complete the next task node of the above process instance (observe whether the effect is the same as that of the Process instance suspension)
Run the following code:
1. Start a process instance
2. Suspend the process definition corresponding to the process instance (here we can use the key defined by the process and the id defined by the process. Here we only use the key defined by the process for demonstration)
@ Test public void testSuspendProcessDefinition () {RepositoryService repositoryService = engine. getRepositoryService (); String processDefinitionKey = "purchasingflow"; // define a repositoryService according to the key defined in the process. suspendProcessDefinitionByKey (processDefinitionKey );}
3. Complete the next node of the Process instance and use taskService to end the next task node.
At this time, we found that this process instance can continue to be executed and can be executed until the end. With this question, let's start another process instance.
4. restart the process instance defined in this process.
An error is reported indicating that the process instance defined by the suspended process cannot be started.
What's going on ??
We carefully read the pending api defined in the process and found that there is another api
void suspendProcessDefinitionById(String processDefinitionId, boolean suspendProcessInstances, Date suspensionDate);
void suspendProcessDefinitionByKey(String processDefinitionKey, boolean suspendProcessInstances, Date suspensionDate);
The parameters are processDefinitionId (processDefinitionKey): id (key) of the process definition)
SuspendProcessInstances: Indicates whether to cascade the process instances defined in the process.
SuspensionDate: set the time when the process definition is suspended. If this parameter is not set, it is immediately suspended.
Here we know why the process definition is suspended and why the process instance under the process definition can continue to run.
Next we will suspend this process definition and cascade the process instances defined in this process.
@ Test public void testActivateProcessDefinitionCascade () {RepositoryService repositoryService = engine. getRepositoryService (); String processDefinitionKey = "purchasingflow"; // pause a process definition based on the key defined in the process, and cascade the repositoryService of the process instance defined in the process definition. suspendProcessDefinitionByKey (processDefinitionKey, true, null );}
At this time, we continue to run the initiated process instance and find that it is no longer running !!!