Activiti process instances and task allocation

Article directory

  • 1. Process examples
    • 1.1. What is a process instance?
    • 1.2. Associate the actual business with the activiti table (BusinessKey)
    • 1.3. Suspend and activate process instance
      • 1.3.1. All process instances hang
      • 1.3.2. A single process instance hangs
  • 2. Task allocation
    • 2.1. Fixed allocation
    • 2.2. UEL expression distribution
      • 2.2.1. UEL-value method
      • 2.2.2, UEL-method method
    • 2.3. Listener allocation

1. Process example

1.1. What is a process instance

Process definition ProcessDefinition and process instance ProcessInstance are important concepts in Activiti, similar to the relationship between Java class and Java instance

Starting a process instance means starting a business process. For example, the employee leave process is deployed. If Zhang San wants to ask for leave, he can start a process instance. If Li Si wants to ask for leave, he can also start a process instance. The execution of the two processes does not affect each other. , just like defining a java class and instantiating two objects, the deployment process is like a java class, and starting a process instance is like new a java object

1.2. Associate the actual business with the activiti table (BusinessKey)

For example, when we fill out a leave request form, there must be a unique identifier for the leave request form. We usually use this identifier to associate with Activiti. This identifier is called businesskey in Activiti.

BusinessKey: Business identifier, usually the primary key of the business. Business identifiers correspond to process identifiers one-to-one. Business identifiers come from the business system. Storing business identifiers means correlating and querying data in the business system based on the business identifiers.

For example: When the leave process starts a process instance, the ID of the leave request can be stored in Activiti as a business identifier. In the future, by querying the process instance information of Activiti, you can obtain the ID of the leave request and then query the business system database to obtain the leave request information.

/**
 * Start the process instance and add businessKey
 */
@Test
public void startUpProcessAddBusinessKey(){<!-- -->
    String businessKey = "1";
    // Start the process instance and specify the business ID businessKey, which is the leave application ID
    ProcessInstance processInstance = runtimeService.
            startProcessInstanceByKey("qingjia",businessKey);
    //output
    System.out.println("Business id:" + processInstance.getBusinessKey());
}

1.3. Suspend, activate process instance

In some cases, due to process changes, the currently running process may need to be paused instead of deleted directly. The process will not be executed after it is paused;

1.3.1. All process instances are suspended

The operation process is defined as suspended state, and all process instances below the process definition are suspended:
If the process definition is in a suspended state, the process definition will not allow new process instances to be started, and all process instances under the process definition will be suspended.

@Test
public void suspendProcessInstance() {<!-- -->
    ProcessDefinition qingjia = repositoryService.createProcessDefinitionQuery().processDefinitionKey("qingjia").singleResult();
    // Get whether the current process definition is in a suspended state. If the suspended method is true, it is suspended, and if the suspended method is false, it is running.
    boolean suspended = qingjia.isSuspended();
    if (suspended) {<!-- -->
        // Tentative, then you can activate it
        // Parameter 1: ID of process definition Parameter 2: Whether to activate Parameter 3: Time point
        repositoryService.activateProcessDefinitionById(qingjia.getId(), true, null);
        System.out.println("Process definition:" + qingjia.getId() + "Activate");
    } else {<!-- -->
        repositoryService.suspendProcessDefinitionById(qingjia.getId(), true, null);
        System.out.println("Process definition:" + qingjia.getId() + "suspended");
    }
}

1.3.2. A single process instance hangs

If an instance under an operation process definition is suspended separately, all query, completion and other operations under the instance will be suspended.

@Test
public void SingleSuspendProcessInstance() {<!-- -->
    String processInstanceId = "8bdff984-ab53-11ed-9b17-f8e43b734677";
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    //Get whether the current process definition is in a suspended state. If the suspended method is true, it means it is suspended. If it is false, it is running.
    boolean suspended = processInstance.isSuspended();
    if (suspended) {<!-- -->
        runtimeService.activateProcessInstanceById(processInstanceId);
        System.out.println("Process instance:" + processInstanceId + "Activate");
    } else {<!-- -->
        runtimeService.suspendProcessInstanceById(processInstanceId);
        System.out.println("Process instance:" + processInstanceId + "Pending");
    }
}

2. Task allocation

There are three ways to assign tasks

  1. fixed allocation
  2. UEL expression assignment
  3. Listener assignment

2.1, Fixed allocation


2.2, UEL expression allocation

2.2.1, UEL-value method

New: an overtime process

As shown in the picture:

assignee1 This variable is a process variable of activiti

We start the process instance. The method of starting the instance is basically the same as the previous method. The only difference is that a parameter is added at startup.

@Test
public void deployProcess01() {<!-- -->
    // Process deployment
    Deployment deploy = repositoryService.createDeployment()
        .addClasspathResource("process/jiaban01.bpmn20.xml")
        .name("overtime application process")
        .deploy();
    System.out.println(deploy.getId());
    System.out.println(deploy.getName());
}

/**
 * Start process instance
 */
@Test
public void startUpProcess01() {<!-- -->
    Map<String, Object> variables = new HashMap<>();
    variables.put("assignee1","zhangsan");
    variables.put("assignee2","lisi");
    //To create a process instance, we need to know the key of the process definition
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia01", variables);
    //Output information about the instance
    System.out.println("Process definition id: " + processInstance.getProcessDefinitionId());
    System.out.println("Process instance id: " + processInstance.getId());
}

2.2.2, UEL-method


userBean is a bean in the spring container, which means calling the getUsername(int id) method of the bean.

Manager approval: ${userBean.getUsername(1)}

Personnel approval: ${userBean.getUsername(2)}

@Component
public class UserBean {<!-- -->

    public String getUsername(int id) {<!-- -->
        if(id == 1) {<!-- -->
            return "zhangsan";
        }
        if(id == 2) {<!-- -->
            return "lisi";
        }
        return "admin";
    }
}

Deployment and startup

@Test
public void deployProcess02() {<!-- -->
    // Process deployment
    Deployment deploy = repositoryService.createDeployment()
        .addClasspathResource("process/jiaban02.bpmn20.xml")
        .name("overtime application process")
        .deploy();
    System.out.println(deploy.getId());
    System.out.println(deploy.getName());
}

/**
 * Start process instance
 */
@Test
public void startUpProcess02() {<!-- -->
    //To create a process instance, we need to know the key of the process definition
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia02");
    //Output relevant information about the instance
    System.out.println("Process definition id: " + processInstance.getProcessDefinitionId());
    System.out.println("Process instance id: " + processInstance.getId());
}

When the process instance is started, the bean method will be called with the parameter: 1. After the manager approves it, the bean method will be called with the parameter: 2

2.3. Listener allocation

If you use a listener to specify the person in charge, there is no need to specify an assignee when designing the process.
Task listeners execute custom java logic or expressions when corresponding task-related events occur.

Event options include:

Create: Triggered after task creation
Assignment: Triggered after task assignment
Delete: triggered after the task is completed
All: Triggered when all events occur

Define a task listening class, and the class must implement the org.activiti.engine.delegate.TaskListener interface

public class MyTaskListener implements TaskListener {<!-- -->

    @Override
    public void notify(DelegateTask delegateTask) {<!-- -->
        if(delegateTask.getName().equals("Manager Approval")){<!-- -->
            //Specify the task leader here
            delegateTask.setAssignee("zhangsan");
        } else if(delegateTask.getName().equals("Personnel Approval")){<!-- -->
            //Specify the task leader here
            delegateTask.setAssignee("lisi");
        }
    }
}

Configure listener


/**
 * Start process instance
 */
@Test
public void startUpProcess03() {<!-- -->
    //To create a process instance, we need to know the key of the process definition
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia03");
    //Output information about the instance
    System.out.println("Process definition id: " + processInstance.getProcessDefinitionId());
    System.out.println("Process instance id: " + processInstance.getId());
}