当前位置:网站首页>第四章: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
边栏推荐
- ffmpeg drawtext add text watermark
- 无题五
- Open Source Summer | How OpenHarmony Query Device Type (eTS)
- dotnet OpenXML 解析 PPT 图表 面积图入门
- 2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
- Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
- hcip BGP 增强实验
- 企业的数字化转型到底是否可以买来?
- mysql进阶(二十七)数据库索引原理
- 开发常用手册链接分享
猜你喜欢

shell脚本实例

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

电竞、便捷、高效、安全,盘点OriginOS功能的关键词

语音社交软件开发——充分发挥其价值

微服务 技术栈

七夕浪漫约会不加班,RPA机器人帮你搞定工作

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

Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?

Introduction to SD NAND Flash!

dotnet OpenXML 解析 PPT 图表 面积图入门
随机推荐
正则表达式replaceAll()方法具有什么功能呢?
正则表达式replaceFirst()方法具有什么功能呢?
hcip BGP 增强实验
还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
韦东山 数码相框 项目学习(六)tslib的移植
SD NAND Flash简介!
数据中台建设(十):数据安全管理
Where is your most secretive personality?
PAT Class B-B1019 Digital Black Hole (20)
七夕浪漫约会不加班,RPA机器人帮你搞定工作
uniapp 连接ibeacon
[Strong Net Cup 2022] WP-UM
Jenkins manual (2) - software configuration
Keil升级到AC6后,到底有哪些变化?
语音社交软件开发——充分发挥其价值
[Unity] [UGUI] [Display text on the screen]
Brief Analysis of WSGI Protocol
新白娘子传奇系列
PHP 操作mangoDb