当前位置:网站首页>第四章: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
边栏推荐
- 蚁剑webshell动态加密连接分析与实践
- [Strong Net Cup 2022] WP-UM
- 微服务 技术栈
- Microservice Technology Stack
- 力扣(LeetCode)216. 组合总和 III(2022.08.04)
- 21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
- 无题十四
- 你最隐秘的性格在哪?
- matcher中find,matches,lookingAt匹配字符串的不同之处说明
- 电竞、便捷、高效、安全,盘点OriginOS功能的关键词
猜你喜欢

Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g

JS逆向入门学习之回收商网,手机号码简易加密解析

微服务 技术栈

Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)

egg框架使用(二)

three物体围绕一周呈球形排列

Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need

three objects are arranged in a spherical shape around the circumference

19. Server-side session technology Session

Microservice Technology Stack
随机推荐
2022华数杯数学建模思路分析交流
Brief Analysis of WSGI Protocol
mysql索引
STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
What is CRM Decision Analysis Management?
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
matcher中find,matches,lookingAt匹配字符串的不同之处说明
无题九
js hijacks the array push method
我们的Web3创业项目,黄了
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
静态链接和动态链接
PAT Class B-B1019 Digital Black Hole (20)
【Office】Microsoft Office下载地址合集(微软官方原版离线安装下载)
Advanced usage of C language
IDEA执行Test操作导致数据插入时出现了重复数据
19. Server-side session technology Session
Imitation SBUS fixed with serial data conversion
ffmpeg drawtext 添加文本水印
Getting started with Polkadot parachain development, this article is enough