第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]

第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量

大家好,又见面了,我是你们的朋友全栈君。

上一章我们讲了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值:

第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]

然后执行设置变量方法,看看变量表会有对应的值:

第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]

执行获取变量表的结果也是一样的:

请假天数: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());
	}
	

先结束之前的未结束的任务。然后运行上面的方法就行了。

第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]

变量表有我们设进去的值。

还有一个是在人物结束时设置流程变量,这个是很常用的接口,比如请假时第一个节点,我们一般会在申请任务结束时设置申请的内容。

代码如下:

/**
	 * 完成任务时设置流程变量
	 */
	@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

这里我直接使用上面启动时设置变量执行后的结果,这里为了验证我们主要关注请假原因这个字段的值,执行前的值是:

第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]

下面我们执行上面的代码:

注意这里使用的id是任务id,不是对象id:

第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]

可以看出已经修改了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106143.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号