当前位置:网站首页>第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]
第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]
2022-08-05 10:05:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
上一章我们讲了taskService获取流程变量的过程,这里我们讲讲RuntimeService是怎么设置和获取的,其实过程跟taskService是差不多的。
- RuntimeService 与流程实例及执行对象相关,对于的表是:act_ru_execution
- TaskService 与任务相关 对应的表是act_ru_variable
代码如下:
/**
* RuntimeService设置流程变量数据
*/
@Test
public void setVariableRunTimeValue(){
// TaskService taskService=processEngine.getTaskService();//获取任务
RuntimeService runtimeService=processEngine.getRuntimeService();
String executionId="42501";//更加任务id知道是哪个人物,设置流程变量。可以更加查看任务方法查看任务的id,可以到数据库直接看
//下面设置任务的内容,比如请假流程,任务的第一节点也就是申请人要写请节哀的原因
runtimeService.setVariable(executionId, "days", 4);//请假天数
runtimeService.setVariable(executionId, "date", new Date());//请假日期
runtimeService.setVariable(executionId, "reason", "haha");//请假原因
//下面我们再测试一个额外的知识点,就是流程传输变量,这里我们再新建一个student对象,对象有id 和name两个属性,还有就是序列化传输
Student student=new Student();
student.setId(1);
student.setName("zhangsan");
runtimeService.setVariable(executionId, "student",student);//序列化对象
}
/**
* RunTimeService获取流程变量数据
*/
@Test
public void getVariableRunTimeValues(){
// TaskService taskService=processEngine.getTaskService();//获取任务
RuntimeService runtimeService=processEngine.getRuntimeService();
String executionId="42501";
Integer day=(Integer) runtimeService.getVariableLocal(executionId, "days");//获取请假天数
Date date=(Date) runtimeService.getVariable(executionId, "date");//请假日期
String reason=(String) runtimeService.getVariable(executionId, "reason");//请假原因
Student student2=(Student) runtimeService.getVariable(executionId, "student");//序列化对象
System.out.println("请假天数:"+day);
System.out.println("请假日期:"+date);
System.out.println("请假原因:"+reason);
System.err.println("请假对象:"+student2.getId()+",,,"+student2.getName());
}
执行过程是,start()启动一个流程实例,此时会在执行对象表act_ru_execution表生成数据,我们看看id值:
然后执行设置变量方法,看看变量表会有对应的值:
执行获取变量表的结果也是一样的:
请假天数:4
请假日期:Sun Apr 22 13:47:25 CST 2018
请假原因:haha
请假对象:1,,,zhangsan
对于设置多个流程变量和设置获取局部变量的方法是用跟taskService 是一样的,新读者可以参考博文:
https://blog.csdn.net/csdnliuxin123524/article/details/80037416
当然也可以直接在启动流程的时候创建流程变量:
/**
* 启动流程时就设置流程变量
*/
@Test
public void RunTimeServicestart(){
Student student=new Student();
student.setId(1);
student.setName("zhangsan");
Map<String, Object> variables=new HashMap<String,Object>();
variables.put("days", 66);
variables.put("date",new Date());
variables.put("reason", "faShao21111");
variables.put("student", student);
//定义流程表的KEY字段值,key值是我们前面定义好的key,可在act_re_procdef表中的key_字段中找到,
//运行启动流程的servicee
ProcessInstance pi=processEngine.getRuntimeService()
.startProcessInstanceByKey("studentLeaveProcess00",variables);
System.out.println(pi.getId());
System.out.println(pi.getProcessDefinitionId());
}
先结束之前的未结束的任务。然后运行上面的方法就行了。
变量表有我们设进去的值。
还有一个是在人物结束时设置流程变量,这个是很常用的接口,比如请假时第一个节点,我们一般会在申请任务结束时设置申请的内容。
代码如下:
/**
* 完成任务时设置流程变量
*/
@Test
public void completeTaskVariables(){
Student student=new Student();
student.setId(1);
student.setName("zhangsan");
Map<String, Object> variables=new HashMap<String,Object>();
variables.put("days", 66);
variables.put("date",new Date());
variables.put("reason", "enenene2222");
variables.put("student", student);
processEngine.getTaskService().complete("50002",variables);
}
zhe
这里我直接使用上面启动时设置变量执行后的结果,这里为了验证我们主要关注请假原因这个字段的值,执行前的值是:
下面我们执行上面的代码:
注意这里使用的id是任务id,不是对象id:
可以看出已经修改了。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106143.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
leetcode: 529. 扫雷游戏
Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
three objects are arranged in a spherical shape around the circumference
Advanced usage of C language
hcip BGP enhancement experiment
Open Source Summer | How OpenHarmony Query Device Type (eTS)
three.js调试工具dat.gui使用
three物体围绕一周呈球形排列
leetcode: 529. Minesweeper Game
随机推荐
mysql进阶(二十七)数据库索引原理
无题六
CPU的亲缘性affinity
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
5.部署web项目到云服务器
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
uniapp 连接ibeacon
无题一
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
5. Deploy the web project to the cloud server
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
hcip BGP 增强实验
攻防世界-PWN-new_easypwn
Pycharm 常用外部工具
正则表达式replaceFirst()方法具有什么功能呢?
首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
公众号如何运维?公众号运维专业团队
Voice conversion相关语音数据集综合汇总
MySQL之数据视图
无题十一