Activiti6.0 (four) the use of process variables

table of Contents

I. Introduction

2. Introduction to process variables

1. Set process variables

2. Use process variables on the process line

3. Process variable storage

4. Summary of experience


I. Introduction

Process variables are also a very important thing in the use of processes, mainly responsible for the role of passing business parameters, and their scope is only valid in the current process instance, so they are often used to specify the task handler of a certain link. This article briefly introduces the setting, transfer, and acquisition of process variables. The process is still based on the series three leave process.

2. Introduction to process variables

First look at a process case code given below:

    @Test
    public void variableDemoTest() {
        // 发布流程
        Deployment deployment = repositoryService.createDeployment().name("请假小流程").addClasspathResource("processes/vocationDemo.bpmn20.xml").deploy();

        // 启动一个流程实例,同时设置流程变量,可以通过在启动或完成任务的时候传递变量,当然也可以单独设置变量
        Map<String, Object> variableMap = new HashMap<>();
        // 基本类型的变量在activiti中可直接存储
        variableMap.put("userId", "123");
        variableMap.put("leaveDays", 3);
        variableMap.put("leaveTime", new Date());
        // 对象以流的方式存储,包括javabean类型(必须实现序列化接口)
        variableMap.put("users", Lists.newArrayList("zhangsan"));
        variableMap.put("user", new User("李四"));
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("vocation", variableMap);

        // 查询所有任务
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();

        System.out.println("Task size:" + tasks.size());

        Task task = tasks.get(0);

        // 也可以为某个任务单独设置变量,单独设置变量在实际中其实不建议存储到activiti中,由业务方自行存储比较好
        taskService.setVariable(task.getId(), "当前任务处理人", task.getAssignee());

        // 获取变量
        System.out.println("直属经理任务处理人" + taskService.getVariable(task.getId(), "当前任务处理人"));
    }

1. Set process variables

There are roughly two types of setting process variables:

  • Basic type variable settings
  • javaBean variable settings

And process variables are generally used in the process start, flow or completion stage, in order to pass certain business parameters so that these variables can be obtained in the business listener to complete the business side's own needs, so set the process variables separately in Activiti In fact, it is not recommended, that is, the way to set variables using taskService is generally implemented by the business side.

2. Use process variables on the process line

Another use of process variables is to act on the process line. When there are multiple outgoing lines in a certain link, Activiti needs to know which line to go. At this time, process variables need to be used online to determine the flow direction. The following is for demonstration A new leave process will be built, and the flow chart drawn using Activiti-app is as follows:

It can be seen that there are two lines of approval for the direct manager’s approval process. When the leave is more than three days, the approval of the general manager is required, otherwise it can be directly ended. For this kind of multiple lines, we can set variable conditions on the process line to meet , Click the process line of asking for leave for more than three days, and then configure the corresponding execution conditions, as follows:

${} is a specific format, the content in the middle should use a boolean type expression to determine the connection to be executed, nextBranch is the name of the specified process variable. Then save the current process and export the xml file. For your convenience, the xml file is given as follows:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">
  <process id="vocation" name="多分支请假流程" isExecutable="true">
    <startEvent id="startEvent1"></startEvent>
    <userTask id="sid-042D76FB-6812-4099-A8E1-3CCC4A8D1896" name="直属经理审批" activiti:candidateUsers="test">
      <extensionElements>
        <modeler:user-info-firstname-test xmlns:modeler="http://activiti.com/modeler"><![CDATA[test]]></modeler:user-info-firstname-test>
        <modeler:activiti-idm-candidate-user xmlns:modeler="http://activiti.com/modeler"><![CDATA[true]]></modeler:activiti-idm-candidate-user>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-D3C4F10F-0829-4168-8AF4-40B7FF375217" sourceRef="startEvent1" targetRef="sid-042D76FB-6812-4099-A8E1-3CCC4A8D1896"></sequenceFlow>
    <userTask id="sid-32EDE0A5-D28C-474B-86F0-E0221DD5D26E" name="总经理审批" activiti:candidateUsers="test">
      <extensionElements>
        <modeler:user-info-firstname-test xmlns:modeler="http://activiti.com/modeler"><![CDATA[test]]></modeler:user-info-firstname-test>
        <modeler:activiti-idm-candidate-user xmlns:modeler="http://activiti.com/modeler"><![CDATA[true]]></modeler:activiti-idm-candidate-user>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <endEvent id="sid-5879817C-644B-4A5E-86B8-16ED025C91AF"></endEvent>
    <sequenceFlow id="sid-FFBFCF5B-5B4D-420A-8D82-06506D546993" sourceRef="sid-32EDE0A5-D28C-474B-86F0-E0221DD5D26E" targetRef="sid-5879817C-644B-4A5E-86B8-16ED025C91AF"></sequenceFlow>
    <sequenceFlow id="sid-A37138F4-6D40-4EB7-961A-0242D63D8F9D" name="请假超过三天" sourceRef="sid-042D76FB-6812-4099-A8E1-3CCC4A8D1896" targetRef="sid-32EDE0A5-D28C-474B-86F0-E0221DD5D26E">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${nextBranch=='超过三天'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-D371A73D-E97E-45D3-ABEE-48221B30BBC0" name="请假天数较短" sourceRef="sid-042D76FB-6812-4099-A8E1-3CCC4A8D1896" targetRef="sid-5879817C-644B-4A5E-86B8-16ED025C91AF">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${nextBranch=='三天以内'}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_vocation">
    <bpmndi:BPMNPlane bpmnElement="vocation" id="BPMNPlane_vocation">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="120.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-042D76FB-6812-4099-A8E1-3CCC4A8D1896" id="BPMNShape_sid-042D76FB-6812-4099-A8E1-3CCC4A8D1896">
        <omgdc:Bounds height="80.0" width="100.0" x="240.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-32EDE0A5-D28C-474B-86F0-E0221DD5D26E" id="BPMNShape_sid-32EDE0A5-D28C-474B-86F0-E0221DD5D26E">
        <omgdc:Bounds height="80.0" width="100.0" x="405.0" y="30.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-5879817C-644B-4A5E-86B8-16ED025C91AF" id="BPMNShape_sid-5879817C-644B-4A5E-86B8-16ED025C91AF">
        <omgdc:Bounds height="28.0" width="28.0" x="585.0" y="164.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-D3C4F10F-0829-4168-8AF4-40B7FF375217" id="BPMNEdge_sid-D3C4F10F-0829-4168-8AF4-40B7FF375217">
        <omgdi:waypoint x="149.99875145663177" y="178.19353227685977"></omgdi:waypoint>
        <omgdi:waypoint x="240.0" y="179.35483870967744"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-D371A73D-E97E-45D3-ABEE-48221B30BBC0" id="BPMNEdge_sid-D371A73D-E97E-45D3-ABEE-48221B30BBC0">
        <omgdi:waypoint x="340.0" y="179.67637540453075"></omgdi:waypoint>
        <omgdi:waypoint x="585.000293242847" y="178.09061298871944"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-FFBFCF5B-5B4D-420A-8D82-06506D546993" id="BPMNEdge_sid-FFBFCF5B-5B4D-420A-8D82-06506D546993">
        <omgdi:waypoint x="505.0" y="107.5"></omgdi:waypoint>
        <omgdi:waypoint x="587.8" y="169.6"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-A37138F4-6D40-4EB7-961A-0242D63D8F9D" id="BPMNEdge_sid-A37138F4-6D40-4EB7-961A-0242D63D8F9D">
        <omgdi:waypoint x="340.0" y="146.66666666666666"></omgdi:waypoint>
        <omgdi:waypoint x="405.0" y="103.33333333333333"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Then, like the previous code, assign different values ​​to the nextBranch variable to see the effect:

@Test
    public void multiBranchDemoTest() {
        // 1、发布流程
        Deployment deployment = repositoryService.createDeployment().name("多分支请假流程").addClasspathResource("processes/vocationDemo2.bpmn20.xml").deploy();

        // 2、启动一个流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("vocation");

        // 3、查询所有任务
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();

        Task task = tasks.get(0);
        // 4、完成直属经理审批环节任务的同时设置流程变量,来告诉Activiti该走哪条线
        Map<String, Object> variables = new HashMap<>();
        variables.put("nextBranch", "超过三天");
        taskService.complete(task.getId(), variables);
    }

You can see that the current process instance in the running task table act_ru_task is automatically transferred to the general manager's approval link.

Change the value of nextBranch to "within three days" and the direct circulation ends at this time.

When the value of the process variable cannot match the value defined on all process lines, an error will be thrown. Therefore, in actual business use, the value is usually set as the id of the process line. Of course, the configuration of the process line also supports the selection of whether it is the default line. That is, the process line is executed when no conditions are met, but it is generally not set except for judging multiple outgoing lines in different links of the gateway.

 

3. Process variable storage

Activiti's process variables are basically stored in the act_ru_variable table. The above six variables are stored in the table as follows:

It can be seen that the variable types of the basic types are retained, and the javaBean types are stored in a stream, so the javeBean must implement the serialization interface, and the specific data is stored in the act_ge_bytearray in a binary manner, and the following 7509 and 7512 are used as associations. Field, you can see that the contents of the act_ge_bytearray table are as follows:

If you need to set variables during the flow of the process, it is recommended to store the variables in the business side's own table to distinguish it from Activiti. For example, there is a need to set variables on the process line.

4. Summary of experience

It can be found from the above that for those javaBean variables, Activiti is stored in the act_ge_bytearray table, so in the process of using process variables, please do not store too many large variables in Activiti, otherwise, day and night, Activiti is querying The performance of this table will be greatly affected, and the entire business side will also be affected.

Guess you like

Origin blog.csdn.net/m0_38001814/article/details/104179729