【五】流程变量

一、流程图

二、流程

1、部署

2、启动

3、设置流程变量

4、获取流程变量

5、查询历史的流程变量

三、总结

流程变量


一、流程图

流程变量(key ---value)在整个工作流中扮演很重要的作用。例如:请假流程中有请假天数、请假原因等一些参数都为流程变量的范围。流程变量的作用域范围是只对应一个流程实例。也就是说各个流程实例的流程变量是不相互影响的。流程实例结束完成以后流程变量还保存在数据库中(存放到流程变量的历史表中)。

二、流程

1、部署

/**
 * 部署流程使用classpath
 */
@Test
public void deployProcess() {
	// 得到流程部署的service
	RepositoryService repositoryService = this.processEngine.getRepositoryService();
	Deployment deploy = repositoryService.createDeployment().name("请假流程001").addClasspathResource("HelloWorld.bpmn")
			.addClasspathResource("HelloWorld.png").deploy();
	System.out.println("部署成功:流程部署ID:" + deploy.getId());
}

2、启动

/**
 * 启动流程
 */
@Test
public void startProcess() {
	RuntimeService runtimeService = this.processEngine.getRuntimeService();
	String processDefinitionKey = "HelloWorld";
//		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);
	//创建流程变量对象
	Map<String,Object> variables=new HashMap<>();
	variables.put("请假天数", 5);//int
	variables.put("请假原因", "约会");
	variables.put("请假时间", new Date());
	ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
	System.out.println("流程启动成功:" + processInstance.getId() + "   " + processInstance.getProcessDefinitionId() + "  "
			+ processInstance.getProcessInstanceId());

}

3、设置流程变量

流程变量的支持的类型

/**
 * 设置流程变量1
 */
@Test
public void setVariables() {
	RuntimeService runtimeService = this.processEngine.getRuntimeService();
	String executionId="2501";
	//runtimeService.setVariable(executionId, "请假人", "小明");
	Map<String,Object> variables=new HashMap<>();
	variables.put("请假天数", 6);//int
	variables.put("请假原因", "约会妹子");
	variables.put("请假时间", new Date());
//		variables.put("用户对象", new User(1,"小明"));  // 对象要继承Serializable
	runtimeService.setVariables(executionId, variables);
	System.out.println("流程变量设置成功");
	
	
}


/**
 * 设置流程变量2
 */
@Test
public void setVariables2() {
	TaskService taskService = this.processEngine.getTaskService();
	
	String taskId="2507";
	//runtimeService.setVariable(executionId, "请假人", "小明");
	Map<String,Object> variables=new HashMap<>();
	variables.put("任务ID设置的", 9);//int
//		taskService.setVariable(taskId, variableName, value);
	taskService.setVariables(taskId, variables);
	System.out.println("流程变量设置成功");
}

4、获取流程变量

/**
 * 获取流程变量
 */
@Test
public void getVariables() {
	RuntimeService runtimeService = this.processEngine.getRuntimeService();
	String executionId="2501";
	Integer days=(Integer) runtimeService.getVariable(executionId, "请假天数");
	Date date=(Date) runtimeService.getVariable(executionId, "请假时间");
	User user=(User) runtimeService.getVariable(executionId, "用户对象");
	System.out.println(days);
	System.out.println(date.toLocaleString());
	System.out.println(user.getId()+"  "+user.getName());
}

5、查询历史的流程变量

@Test
public void getHistoryVariables() {
	HistoryService historyService = this.processEngine.getHistoryService();

	/*HistoricVariableInstance singleResult = historyService.createHistoricVariableInstanceQuery().id("2503").singleResult();;
	System.out.println(singleResult.getId());
	System.out.println(singleResult.getValue());
	System.out.println(singleResult.getVariableName());
	System.out.println(singleResult.getVariableTypeName());*/
	String processInstanceId="2501";
	List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();

	for (HistoricVariableInstance hvs : list) {
		System.out.println("ID"+hvs.getId());
		System.out.println("变量值"+hvs.getValue());
		System.out.println("变量名"+hvs.getVariableName());
		System.out.println("变量类型"+hvs.getVariableTypeName());
		System.out.println("#####################");
	}
}

三、总结

流程变量

在流程执行或者任务执行的过程中,用于设置和获取变量,使用流程变量在流程传递的过程中传递业务参数。

对应的表:

act_ru_variable:正在执行的流程变量表

act_hi_varinst:流程变量历史表

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值