(九)Activitivi5之使用 RuntimeService 设置和获取流程变量
一、案例
/**
* 设置流程变量数据 */ @Test public void setVariableValues(){ RuntimeService runtimeService=processEngine.getRuntimeService(); // 任务Service String executionId="90001"; runtimeService.setVariable(executionId, "days", 2); runtimeService.setVariable(executionId, "date", new Date()); runtimeService.setVariable(executionId, "reason", "发烧"); Student student=new Student(); student.setId(1); student.setName("张三"); runtimeService.setVariable(executionId, "student", student); // 存序列化对象 } /** * 获取流程变量数据 */ @Test public void getVariableValues(){ RuntimeService runtimeService=processEngine.getRuntimeService(); // 任务Service String executionId="102501"; Integer days=(Integer) runtimeService.getVariable(executionId, "days"); Date date=(Date) runtimeService.getVariable(executionId, "date"); String reason=(String) runtimeService.getVariable(executionId, "reason"); Student student=(Student) runtimeService.getVariable(executionId, "student"); System.out.println("请假天数:"+days); System.out.println("请假日期:"+date); System.out.println("请假原因:"+reason); System.out.println("请假对象:"+student.getId()+","+student.getName()); }
- 用RuntimeService 设置/获取变量的方法跟(八)Activiti之流程变量和局部流程变量不同的是,流程变量绑定的是任务ID,如下图
-
而RuntimeService绑定的是act_ru_execution表的executionId。任务ID随着任务节点的变化而变化,而executionId一般不会改变。
- 同样,用RuntimeService设置的值同样在接下来的流程都可以获取,知道流程结束