当前位置:网站首页>第四章:activiti流程中,变量的传递和获取流程变量 ,设置和获取多个流程变量,设置和获取局部流程变量「建议收藏」
第四章:activiti流程中,变量的传递和获取流程变量 ,设置和获取多个流程变量,设置和获取局部流程变量「建议收藏」
2022-08-05 10:05:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
上一章我们介绍了部署流程实例,启动流程,查看任务,完成任务的service和实例,下面我们介绍下怎么获取流程中需要传递的变量。
开始前,先撸一遍流程的任务节点名和任务委派人:
流程图如上,三个任务节点名分别是leave001,leave002,leave003,因为我的Navicat不能看流程中的汉字所以可以简单理解为三个请假流程,请假001,请假002,请假003.
然后看任务委派人:
分别是xiaoliu001,xiaoliu002,xiaoliu003;
好了,下面开始介绍流程变量的传递了:
承接上一章:
/**
* 设置流程变量数据
*/
@Test
public void setVariableValues(){
TaskService taskService=processEngine.getTaskService();//获取任务
String taskId="";//更加任务id知道是哪个人物,设置流程变量。可以更加查看任务方法查看任务的id,可以到数据库直接看
//下面设置任务的内容,比如请假流程,任务的第一节点也就是申请人要写请节哀的原因
taskService.setVariable(taskId, "days", 2);//请假天数
taskService.setVariable(taskId, "date", new Date());//请假日期
taskService.setVariable(taskId, "reason", "发烧");//请假原因
//下面我们再测试一个额外的知识点,就是流程传输变量,这里我们再新建一个student对象,对象有id 和name两个属性,还有就是序列化传输
Student student=new Student();
student.setId(1);
student.setName("zhangsan");
taskService.setVariable(taskId, "student",student);//序列化对象
}上面是设置流程变量。
下面获取流程变量:
/**
* 获取流程变量数据
*/
@Test
public void getVariableValues(){
TaskService taskService=processEngine.getTaskService();//获取任务
String taskId="";
Integer day=(Integer) taskService.getVariable(taskId, "days");//获取请假天数
Date date=(Date) taskService.getVariable(taskId, "date");//请假日期
String reason=(String) taskService.getVariable(taskId, "reason");//请假原因
Student student2=(Student) taskService.getVariable(taskId, "student");//序列化对象
System.out.println("请假天数:"+day);
System.out.println("请假日期:"+date);
System.out.println("请假原因:"+reason);
System.err.println("请假对象:"+student2.getId()+",,,"+student2.getName());
}然后,就启动流程了,启动前我们先打开数据库,可以先看看act_ru_task任务表,此时没有任务,这个表应该是空的。
下面启动流程:
这运行start方法:
此时任务表有了一条数据,
然后执行查看任务方法,看看任务id:
结果如下:
任务ID:12504
任务名称:leave001
任务创建时间:Sun Apr 22 12:40:52 CST 2018
任务委派人:xiaoliu001
任务流程实例Id:12501也可看任务表的ID_字段值。
然后把这个值放到刚才我们写的设置流程变量的方法中:
、
执行上面的方法,成功后我们看看数据库的act_ru_variable表会有我们设置的几个变量:
然后我们继续往下走,执行完成任务方法,注意修改任务id哦
完成后任务到了leave002节点,
这里我们直接看数据的任务id是多少:
17502,然后我们那这个任务id去执行我们刚才写的获取流程变量的方法:
执行结果是:
请假天数:2
请假日期:Sun Apr 22 12:47:58 CST 2018
请假原因:faShao
请假对象:1,,,zhangsan结果是我们设进去的值。说明没问题。
然后再执行完成方法,流程到leave003任务节点,然后再获取流程变量数据。
结果:
请假天数:2
请假日期:Sun Apr 22 12:47:58 CST 2018
请假原因:faShao
请假对象:1,,,zhangsan也是同样能获得的。
最后再complete完成方法,执行完流程,流程结束:
任务表清空。
下面再介绍一种可以设置多个变量的方法:
/**
* 设置多个流程变量数据
*/
@Test
public void setVariableValue1(){
TaskService taskService=processEngine.getTaskService();//获取任务
String taskId="25004";//更加任务id知道是哪个人物,设置流程变量。可以更加查看任务方法查看任务的id,可以到数据库直接看
//下面设置任务的内容,比如请假流程,任务的第一节点也就是申请人要写请节哀的原因
// taskService.setVariable(taskId, "days", 2);//请假天数
// taskService.setVariable(taskId, "date", new Date());//请假日期
// taskService.setVariable(taskId, "reason", "faShao");//请假原因
//下面我们再测试一个额外的知识点,就是流程传输变量,这里我们再新建一个student对象,对象有id 和name两个属性,还有就是序列化传输
Student student2=new Student();
student2.setId(1);
student2.setName("zhangsan");
taskService.setVariable(taskId, "student",student2);//序列化对象
Map<String, Object> variables=new HashMap<String,Object>();
variables.put("days", 3);
variables.put("date",new Date());
variables.put("reason", "faShao2");
variables.put("student", student2);
taskService.setVariables(taskId, variables);
}
/**
* 获取多个流程变量数据
*/
@Test
public void getVariableValue(){
TaskService taskService=processEngine.getTaskService();//获取任务
String taskId="25004";
Map<String, Object> variables=taskService.getVariables(taskId);
Integer day=(Integer) variables.get("days");//获取请假天数
Date date=(Date) variables.get("date");//请假日期
String reason=(String) variables.get("reason");//请假原因
Student student2=(Student) variables.get("student");//序列化对象
System.out.println("请假天数:"+day);
System.out.println("请假日期:"+date);
System.out.println("请假原因:"+reason);
System.err.println("请假对象:"+student2.getId()+",,,"+student2.getName());
}测试过程跟之前的一样,最后获取的结果是:
请假天数:3
请假日期:Sun Apr 22 13:09:22 CST 2018
请假原因:faShao2
请假对象:1,,,zhangsan跟设置的一样。
下面再介绍一个局部变量,就是设置的变量值只在当前节点有效,当流程走到下一个节点时,是获取不到这个值的。
/**
* 设置流程局部变量数据
*/
@Test
public void setVariableLocalValue(){
TaskService taskService=processEngine.getTaskService();//获取任务
String taskId="25004";//更加任务id知道是哪个人物,设置流程变量。可以更加查看任务方法查看任务的id,可以到数据库直接看
//下面设置任务的内容,比如请假流程,任务的第一节点也就是申请人要写请节哀的原因
taskService.setVariableLocal(taskId, "days", 2);//请假天数
taskService.setVariable(taskId, "date", new Date());//请假日期
taskService.setVariable(taskId, "reason", "faShao");//请假原因
//下面我们再测试一个额外的知识点,就是流程传输变量,这里我们再新建一个student对象,对象有id 和name两个属性,还有就是序列化传输
Student student=new Student();
student.setId(1);
student.setName("zhangsan");
taskService.setVariable(taskId, "student",student);//序列化对象
}
/**
* 获取流程变量数据
*/
@Test
public void getVariableLocalValues(){
TaskService taskService=processEngine.getTaskService();//获取任务
String taskId="32502";
Integer day=(Integer) taskService.getVariableLocal(taskId, "days");//获取请假天数
Date date=(Date) taskService.getVariable(taskId, "date");//请假日期
String reason=(String) taskService.getVariable(taskId, "reason");//请假原因
Student student2=(Student) taskService.getVariable(taskId, "student");//序列化对象
System.out.println("请假天数:"+day);
System.out.println("请假日期:"+date);
System.out.println("请假原因:"+reason);
System.err.println("请假对象:"+student2.getId()+",,,"+student2.getName());
}这里代码值改了请假天数的值,并使用了setVariablesLocal()方法。先运行设置局部流程变量方法,发现数据库的变量表会多出一个请假天数的数据:
然后执行获取方法:
请假对象:1,,,zhangsan
请假天数:2
请假日期:Sun Apr 22 13:24:02 CST 2018
请假原因:faShao请假的天数是2覆盖了数据库中另一个请假天数的值。然后执行complete()方法完成任务。
再次执行获取方法,结果:
请假天数:null
请假日期:Sun Apr 22 13:24:02 CST 2018
请假原因:faShao
请假对象:1,,,zhangsan请假天数变成了空。
数据库的变量表也少了一条数据。
好了,这就是流程局部变量了。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106144.html原文链接:https://javaforall.cn
边栏推荐
- 韦东山 数码相框 项目学习(六)tslib的移植
- What is CRM Decision Analysis Management?
- MySQL使用聚合函数可以不搭配GROUP BY分组吗?
- Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
- 企业的数字化转型到底是否可以买来?
- 无题十二
- Imitation SBUS fixed with serial data conversion
- 19.服务器端会话技术Session
- NowCoderTOP35-40——持续更新ing
- What is the function of the regular expression replaceFirst() method?
猜你喜欢

CPU的亲缘性affinity

High-quality DeFi application building guide to help developers enjoy DeFi Summer

皕杰报表的下拉框联动

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

【AGC】增长服务1-远程配置示例

手把手教你纯c实现异常捕获try-catch组件

MySQL事务

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

21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)

dotnet OpenXML parsing PPT charts Getting started with area charts
随机推荐
茄子科技CEO仇俊:以用户为中心,做用户真正需要的产品
入门 Polkadot 平行链开发,看这一篇就够了
Brief Analysis of WSGI Protocol
蚁剑webshell动态加密连接分析与实践
无题四
华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
正则表达式replaceFirst()方法具有什么功能呢?
无题七
阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
three.js debugging tool dat.gui use
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
leetcode: 529. 扫雷游戏
three.js调试工具dat.gui使用
three objects are arranged in a spherical shape around the circumference
js劫持数组push方法
PHP 操作mangoDb
leetcode: 529. Minesweeper Game
无题十三
公众号如何运维?公众号运维专业团队