当前位置:网站首页>第四章: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
边栏推荐
- 微服务 技术栈
- NowCoderTOP35-40 - continuous update ing
- MySQL事务
- 无题四
- 阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
- MySQL之数据视图
- matcher中find,matches,lookingAt匹配字符串的不同之处说明
- Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
- 我们的Web3创业项目,黄了
- Custom filters and interceptors implement ThreadLocal thread closure
猜你喜欢

2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享

After Keil upgrades to AC6, what changes?

数据中台建设(十):数据安全管理

2022.8.3

什么是CRM决策分析管理?

气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)

How to realize the short press and long press detection of the button?

Pycharm 常用外部工具

还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!

The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf
随机推荐
手把手教你纯c实现异常捕获try-catch组件
【Unity】【UGUI】【在屏幕上显示文本】
入门 Polkadot 平行链开发,看这一篇就够了
2022.8.3
How does the official account operate and maintain?Public account operation and maintenance professional team
SD NAND Flash简介!
hcip BGP enhancement experiment
static linking and dynamic linking
three物体围绕一周呈球形排列
【温度预警程序de开发】事件驱动模型实例运用
技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)
无题七
MySQL data view
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
IDEA performs the Test operation, resulting in duplicate data when data is inserted
MySQL advanced (twenty-seven) database index principle
After Keil upgrades to AC6, what changes?
自定义过滤器和拦截器实现ThreadLocal线程封闭