activiti学习之变量

写在前面

Activiti中变量类型分为流程变量和任务变量,变量的设置又分基本类型设置,JavaBean类型设置,下面将分别讲解。

1:测试使用的流程

activiti学习之变量

xml:

<?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/test">
    <process id="csdnvariable" name="csdnvariable" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <endEvent id="endevent1" name="End"></endEvent>
        <userTask id="usertask1" name="用户任务" activiti:assignee="张三">
            <extensionElements>
                <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">false</modeler:initiator-can-complete>
            </extensionElements>
        </userTask>
        <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
        <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
            <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="230.0" y="260.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="650.0" y="260.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0" x="400.0" y="250.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="265.0" y="277.0"></omgdi:waypoint>
                <omgdi:waypoint x="400.0" y="277.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="505.0" y="277.0"></omgdi:waypoint>
                <omgdi:waypoint x="650.0" y="277.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

2:测试

2.1:部署流程

@Test
public void deploy() {
    Deployment deployment = repositoryService.createDeployment() // 创建部署
            .addClasspathResource("com/jh/activiti/csdn-variable.bpmn20.xml") // 加载流程资源文件
            .name("csdn流程变量测试") // 流程名称
            .deploy(); // 部署
    /*
    流程部署ID:57501
    流程部署Name:helloworld流程
    */
    System.out.println("流程部署ID:" + deployment.getId());
    System.out.println("流程部署Name:" + deployment.getName());
}

部署成功后:

activiti学习之变量

activiti学习之变量

2.2:启动流程实例

@Test
public void start() {
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("csdnvariable"); // 流程定义表的KEY字段值
    System.out.println("流程实例ID:" + pi.getId());
    System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}

启动后:

实例:

activiti学习之变量

任务:

activiti学习之变量

2.3:直接设置和获取任务变量

  • 设置变量
// 存储到表act_ru_variable,ru是running的简称,代表的是正在执行中的流程实例
// 存储到表act_hi_varinst,hi是history的简称,代表的是所有的执行中和执行完毕的流程实例变量信息
@Test
public void setVariable1() {
    String taskId = "747507";
    // 基本类型,这里变量的数据范围是当前任务id所属的流程实例
    taskService.setVariable(taskId, "days", 5);
    // 这里变量的数据范围是当前任务id所属的流程实例
    taskService.setVariable(taskId, "reason", "回家结婚");
    // setVariableLocal设置局部变量,只能在当前任务中得到,变量的范围是流程实例的当前任务
    taskService.setVariableLocal(taskId, "date", new Date());

    // 序列化对象
    Leave leave = new Leave();
    leave.setDays(5);
    leave.setReason("相亲");
    taskService.setVariable(taskId, "leave", leave);
}

设置后从运行时表act_ru_variable表查看:

activiti学习之变量

从历史变量表act_hi_varinst中查看:

activiti学习之变量

  • 获取变量
@Test
public void getVariable1() {
    String taskId = "747507";
    // 获取基本类型
    Integer days = (Integer) taskService.getVariable(taskId, "days");
    Date date = (Date) taskService.getVariableLocal(taskId, "date");
    String reason = (String) taskService.getVariable(taskId, "reason");
    // 获取对象类型
    Leave leave = (Leave) taskService.getVariable(taskId, "leave");

    System.out.println("天数:" + days);
    System.out.println("日期:" + date);
    System.out.println("原因:" + reason);
    System.out.println("对象:" + leave.getDays() + ", " + leave.getReason());
}
天数:5
日期:Sat Oct 09 18:13:24 CST 2021
原因:回家结婚
对象:5, 相亲

2.4:设置和获取Map变量

  • 设置变量
/**
 * 设置流程变量--MAP方式
 */
@Test
public void setVariableToProcessInstance1() {
    String executionId = "757502";
    Leave leave = new Leave();
    leave.setDays(5);
    leave.setReason("相亲");
    // 填充map
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("exe_days", 5);
    variables.put("exe_reason", "相亲");
    variables.put("exe_date", new Date());
    variables.put("exe_leave", leave);

    runtimeService.setVariables(executionId, variables);
}

设置后查看:

activiti学习之变量

  • 获取变量
/**
 * 获取流程变量--MAP方式
 */
@Test
public void getVariableToProcessInstance1() {
    String executionId = "757502";
    // 获取变量
    Map<String, Object> variables = runtimeService.getVariables(executionId);
    Integer days = (Integer) variables.get("exe_days");
    String reason = (String) variables.get("exe_reason");
    Date date = (Date) variables.get("exe_date");
    Leave leave = (Leave) variables.get("exe_leave");

    System.out.println("天数:" + days);
    System.out.println("日期:" + date);
    System.out.println("原因:" + reason);
    System.out.println("对象:" + leave.getDays() + "," + leave.getReason());
}
天数:5
日期:Sun Oct 10 08:50:18 CST 2021
原因:相亲
对象:5,相亲

2.5:启动流程市就设置流程变量

可应用于启动流程实例时输入一些信息的场景。

  • 设置变量
// 启动流程市就设置流程变量(如发起流程的人有一些提示性质信息,给后续执行流程的人来看)
@Test
public void startProcInstanceWithInitVairable() {
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("var_start", "var_start");
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("helloworld", variables); // 流程定义表的KEY字段值
    System.out.println("流程实例ID:" + pi.getId());
    System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}

直接存储到act_hi_varinst,历史变量表,不会存储在act_ru_variable表中,效果等同于,设置任务变量,然后直接完成任务,可以认为是针对开始节点设置的变量,注意是认为。

activiti学习之变量

  • 获取变量
/**
 * 获取流程变量
 */
// 获取在启动流程实例时就设置的属性信息
// 直接存储到act_hi_varinst,历史变量表,不会存储在act_ru_variable表中,效果等同于,设置任务变量,然后直接完成任务
@Test
public void getStartProcInstanceWithInitVairable() {
    String executionId = "762502";
    String params = (String) runtimeService.getVariable(executionId, "var_start");

    System.out.println("启动流程变量:" + params);
}
启动流程变量:var_start

2.6:完成任务时设置变量

  • 设置变量
// 完成任务时设置变量
@Test
public void setVariableWhenComplateTask() {
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("var_complete", "var_complete");
    taskService.complete("757506", variables);
}

  • 获取变量
@Test
public void getVariableWhenComplateTask() {
    List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
            .executionId("757502")
            .variableName("var_complete")
            .list();

    if (list != null && list.size() > 0) {
        for (HistoricVariableInstance hiv : list) {
            System.out.println(hiv.getVariableName() + "=" + hiv.getValue());
        }
    }
}
上一篇:FastAPI(64)- Settings and Environment Variables 配置项和环境变量


下一篇:kswapd线程的前世今生