The process instance

ProcessInstance represents the execution instance of the process definition. A process instance contains all running nodes. We can use this object to learn information such as the progress of the current process instance. For example, a process instance is when a user or program initiates a process based on the process definition

Diagram of process definitions and process instances:

Start the process instance and add a BusinessKey

Once a process definition is deployed in Activiti, the execution of the process can be managed from within the system through Activiti, and the execution process represents an execution of the process

For example, if a user wants to apply for a business trip, you need to execute this process. If another user also wants to apply for a business trip, you need to execute this process. Each execution does not affect each other, and each execution is an independent process instance

When the process instance is started, the businesskey specified is stored in the execution table of the ACT_RU_execution process instance

Businesskey: a business identifier, usually the primary key of a business table, that corresponds to a process instance. The service ID is from the service system. Storage service ids are used to query service system data associated with service ids

For example, if a flow instance is started, the id of the travel order can be stored in Activiti as a service id. In the future, the id of the travel order can be obtained by querying the flow instance information of Activiti, and the travel order information can be obtained by associated query of the service system database

/** * Add a business key to Activiti's table */
@Test
public void addBusinessKey(a) {
    // 1. Obtain the flow engine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // 2. Obtain RuntimeService
    RuntimeService runtimeService = processEngine.getRuntimeService();
    // 3. Start the process and add BusinessKey
    ProcessInstance instance = runtimeService.startProcessInstanceByKey("evection"."1001");
    / / 4. Output
    System.out.println("businessKey: " + instance.getBusinessKey());
}
Copy the code

Other operations defined by the process

1. Process definition query

Query information about a process, including the process definition, process deployment, and process definition version

public void queryProcessDefinition(a) {
    // 1. Obtain the flow engine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // 2. Get RepositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    // 3. Query all current flow definitions and return a collection of flow definition information
    ProcessDefinitionQuery definitionQuery = repositoryService.createProcessDefinitionQuery();
    List<ProcessDefinition> definitions = definitionQuery.processDefinitionKey("evection")
        .orderByProcessDefinitionVersion()  // Sort
        .desc() / / reverse
        .list();
    // 4. Output information
    for (ProcessDefinition definition : definitions) {
        System.out.println("Process definition ID =" + definition.getId());
        System.out.println("Process definition name =" + definition.getName());
        System.out.println("Process definition key =" + definition.getKey());
        System.out.println("Process definition version ="+ definition.getVersion()); }}Copy the code

2. The process definition is deleted

public void deleteDeployment(a) {
    // 1. Obtain the flow engine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // 2. Get RepositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    // 3. Delete the process deployment information based on the deployment ID. If the process defines an existing process instance, the deletion fails
    String deploymentId = "2501";
    repositoryService.deleteDeployment(deploymentId);
    // Set true to cascade the process definition, which can be deleted even if a process instance is started. Set false to non-level deletion
    // repositoryService.deleteDeployment(deploymentId, true);
}
Copy the code

3. Download process resources

We upload the process resource files to the database, and other users can download the resource files from the database if they want to view them

/** * Download the resource file, use the API provided by Activiti to download the resource file, save to the file directory */
public void getDeployment(a) throws IOException {
    // 1. Obtain the flow engine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // 2. Get RepositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    // 3. Get ProcessDefinitionQuery, query process definition information
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
        .processDefinitionKey("evection")
        .singleResult();
    // 4. Obtain the deployment ID based on the flow definition information
    String deploymentId = processDefinition.getDeploymentId();
    // 5. Use RepositoryService to transfer the deployment ID parameter and read the resource information
    InputStream bpmnInput = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
    // 6. Save the resource file
    File file = new File("g:/evectionFlow.bpmn");
    FileOutputStream outputStream = new FileOutputStream(file);
    IOUtils.copy(bpmnInput, outputStream);
    bpmnInput.close();
    outputStream.close();
}
Copy the code

4. Query historical records

Even if the process definition has been removed, the history of the process execution is still stored in activiti’s act_hi_* related table, and we can query the history of the process execution

public void findHistoryInfo(a) {
    // 1. Obtain the flow engine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // 2. Obtain HistoryService
    HistoryService historyService = processEngine.getHistoryService();
    // 3. Obtain the query object of the Actinst table
    HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery();
    // 4. Query the actinst table
    instanceQuery.processInstanceId("5001");
    instanceQuery.orderByHistoricActivityInstanceStartTime();
    // 5. Query all contents
    List<HistoricActivityInstance> activityInstanceList = instanceQuery.list();
    / / 6. The output
    for (HistoricActivityInstance hi : activityInstanceList) {
        System.out.println(hi.getActivityId());
        System.out.println(hi.getActivityName());
        System.out.println(hi.getProcessDefinitionId());
        System.out.println(hi.getProcessInstanceId());
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); }}Copy the code

5. Process suspension and activation

In some cases, a currently running process may need to be paused rather than deleted due to a process change, and the process will not continue after being paused.

/** * Suspend and activate all process instances */
@Test
public void suspendAllProcessInstance(a) {
    // 1. Obtain the flow engine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // 2. Get RepositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    // 3. Query the process definition
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("evection").singleResult();
    // 4. Get whether all instances of the current process definition are in the pending state
    boolean suspended = processDefinition.isSuspended();
    // 5. Obtain the process definition ID
    String definitionId = processDefinition.getId();
    // 6. If the status is suspended, change the status to active. If the status is active, change the status to suspended
    if (suspended) {
        repositoryService.activateProcessDefinitionById(definitionId, true.null);
    } else {
        repositoryService.suspendProcessDefinitionById(definitionId, true.null); }}Copy the code
/** * Suspends or activates a single process instance */
@Test
public void suspendSingleProcessInstance(a) {
    // 1. Obtain the flow engine
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // 2. Obtain RuntimeService
    RuntimeService runtimeService = processEngine.getRuntimeService();
    // 3. Get the paused state of the current process instance
    ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId("27501").singleResult();
    boolean suspended = instance.isSuspended();
    // 4. Obtain the process instance ID
    String instanceId = instance.getId();
    // 5. If the status is suspended, change the status to active. If the status is active, change the status to suspended
    if (suspended) {
        runtimeService.activateProcessInstanceById(instanceId);
    } else{ runtimeService.suspendProcessInstanceById(instanceId); }}Copy the code