跳转至

1.流程变量

  1. 流程变量在整个工作流中扮演很重要的作用 .
  2. 流程变流的作用域范围是只对应一个流程实例 . 也就是说各个流程实力之间的流程变量是互不影响的 . 流程实例结束完成后流程变量还保存在数据库中(存放在历史表中) .
  3. 设置流程变量的值
  4. 存在形式 key_value
  5. 对应的表
  6. act_ru_variable 正在执行的流程变量表
  7. act_hi_vannst 流程变量历史表
  8. 扩展知识 : setVariablesetVariableLocal的区别
  9. setVariable : 设置流程变量的时候,流程变量名称相同的时候,后一次的值替换前一次的值,而且可以看到Task_id的字段不会存放任何值
  10. setVariableLocal
    • 设置流程变量的时候,针对当前活动的节点设置流程变量,如果一个流程变量存在两个活动节点,对每个活动节点都设置流程变量,就是流程变量的名称相同,后一次的版本值也不会覆盖前一次的值,他会使用不同的任务id作为标识,存放两个流程变量,而且可以看到Task_id字段会存放任务id的值.
    • 使用setVariableLocal说明流程变量绑定了当前的任务,当流程继续执行时,下一个任务获取不到这个流程变量(因为正在执行的流程变量中没有这个数据),所有查询正在执行的任务时不能查询到我们需要的数据,此时需要查询历史流程变量.
  11. 流程变量可以通过EL表达式的方式,决定流程的走向

2.部署流程定义

    /**
     * 部署流程
     */
    @Test
    public void deployProcess() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().name("请假流程").addClasspathResource("HelloWorld.bpmn")
                .addClasspathResource("HelloWorld.png").deploy();
        System.out.println("流程部署ID=" + deploy.getId());
    }

3.启动流程设置流程变量

    /**
     * 启动流程设置流程变量
     */
    @Test
    public void startProess() {
        RuntimeService runtimeService = this.processEngine.getRuntimeService();

        String processDefinitionKey = "level";
        Map<String, Object> variables = new HashMap<>();
        variables.put("请假天数", 5);
        variables.put("请假原因", "约会");
        variables.put("请假时间", new Date());

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
        System.out.println("流程启动成功...");
        System.out.println("流程执行ID=" + processInstance.getId());
        System.out.println("流程定义ID=" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例ID=" + processInstance.getProcessInstanceId());
    }

4.设置流程变量

1.方式一 runtimeService

    /**
     * 设置流程变量
     */
    @Test
    public void setVariables() {
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        String executionId ="2501";
        String variableName ="请假人";
        String value ="小明";
//      runtimeService.setVariable(executionId, variableName, value);
        Map<String, Object> variables = new HashMap<>();
        variables.put("请假天数", 5);
        variables.put("请假原因", "约会");
        variables.put("请假时间", new Date());
        variables.put("用户", new User());
        runtimeService.setVariables(executionId, variables);
    }

其中User类,必须实现序列化**Serializable**

public class User implements Serializable{
    ....
}

2.方式二 taskService

    /**
     * 设置流程变量2
     */
    @Test
    public void setVariables2() {
        TaskService taskService = this.processEngine.getTaskService();
        String taskId ="2507";
        String variableName ="请假人2";
        String value ="小明";
        taskService.setVariable(taskId, variableName, value);
    }

5.获取流程变量

  • 如果存入和取出的Object发生了改变,会抛出异常
    /**
     * 获取流程变量
     */
    @Test
    public void getVariables() {
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        String executionId = "2501";
        Object variable1 = runtimeService.getVariable(executionId, "用户");
        Object variable2 = runtimeService.getVariable(executionId, "请假人");
        System.out.println(variable1);
        System.out.println(variable2);
    }

6.查询历史流程变量

    /**
     * 查询历史流程变量
     */
    @Test
    public void getHistoryVariables() {
        HistoryService historyService = this.processEngine.getHistoryService();
        // 指定变量id获取唯一的
        HistoricVariableInstance singleResult = historyService.createHistoricVariableInstanceQuery().id("2503")
                .singleResult();

        System.out.println(singleResult);
        // 指定流程实例id获取所有的
        List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
                .processInstanceId("2501").list();
        for (HistoricVariableInstance historicVariableInstance : list) {
            System.out.println(historicVariableInstance);
        }
    }

7.支持的流程变量类型

  • string
  • integer
  • short
  • long
  • double
  • boolean
  • date
  • binary
  • serializable