Activiti5 学习笔记(二)—— 流程变量

什么是流程变量?

简单的说就是字面上的意思:变量;作用于整个流程实例(所有节点都可以查看到该变量的值),也可以单独作用于某个节点(只有该节点才可以看到变量的值);各个流程实例之间的流程变量互不影响。

先来创建一个流程图

示例代码

1. 设置流程变量(前面的部署流程定义、启动流程实例假设都已完成)

TaskService taskService = processEngine.getTaskService();  //获取TaskService
String taskId = "2504"; //查询张晓晓的任务并获取任务ID,这里我就直接从数据库拿任务id了
taskService.setVariable(taskId, "variable1", "variable1"); //设置流程变量,该变量在整个流程实例中都可见
taskService.setVariableLocal(taskId, "variableLocal", "variableLocal");  //设置lcoal流程变量,该变量只有在该任务id中可见

Map<String, Object> variables = new HashMap<>();
variables.put("variables", "variables");
taskService.setVariables(taskId, variables); //设置多个流程变量

//taskService.setVariablesLocal(taskId, variables); 设置多个local流程变量

Person Person = new Person();
Person.setName("张翼德");
taskService.setVariable(taskId, "persion", Person); //设置javaBean类型的流程变量,Persion类需要实现Serializable

System.out.println("设置流程变量成功!");

2. 获取流程变量

TaskService taskService = processEngine.getTaskService();
String taskId = "2504";
String variable1 = (String) taskService.getVariable(taskId, "variable1");
String variableLocal = (String) taskService.getVariableLocal(taskId, "variableLocal");
System.out.println("variable1="+variable1);
System.out.println("variableLocal="+variableLocal);
//其他变量的输出就不再演示了
variable1=variable1
variableLocal=variableLocal

3. 张晓晓完成任务

String taskId = "2504";  
processEngine.getTaskService().complete(taskId);  
System.out.println("完成任务:任务ID:"+taskId);

4. 获取流程变量

TaskService taskService = processEngine.getTaskService();
String taskId = "10002"; //换成李大大的任务id了
String variable1 = (String) taskService.getVariable(taskId, "variable1");
String variableLocal = (String) taskService.getVariableLocal(taskId, "variableLocal");
System.out.println("variable1="+variable1);
System.out.println("variableLocal="+variableLocal);
variable1=variable1
variableLocal=null    //当前taskid是没有variableLocal的

5.更改流程变量

TaskService taskService = processEngine.getTaskService();  //获取TaskService
String taskId = "10002"; 
taskService.setVariable(taskId, "variable1", "variable111"); //更改变量的值
taskService.removeVariable(taskId, "variables"); //移除变量
System.out.println("设置流程变量成功!");

6. 完成任务,流程结束

String taskId = "7502";  
processEngine.getTaskService().complete(taskId);  
System.out.println("完成任务:任务ID:"+taskId);

7. 查询流程变量的历史

List<HistoricVariableInstance> list = processEngine.getHistoryService()//
	.createHistoricVariableInstanceQuery()//创建一个历史的流程变量查询对象
	.variableName("variable1")
	.list();
if(list!=null && list.size()>0){
	for(HistoricVariableInstance hvi:list){
		System.out.println(hvi.getId()+"   "+hvi.getProcessInstanceId()+"   "+hvi.getVariableName()+"   "+hvi.getVariableTypeName()+"    "+hvi.getValue());
		System.out.println("###############################################");
	}
}

还有哪些地方可以操作流程变量? 

//runtimeService对象可以设置和获取流程变量		
RuntimeService runtimeService = processEngine.getRuntimeService();
runtimeService.setVariable(executionId, variableName, value);
runtimeService.getVariable(executionId, variableName);

//taskService对象可以设置和获取流程变量	
TaskService taskService = processEngine.getTaskService();
taskService.setVariable(taskId, variableName, value);
taskService.getVariable(taskId, variableName);

//启动流程,可以设置流程变量
processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey,variables);

//给信号,receiveTask节点中使用的
processEngine.getRuntimeService().signal(executionId, processVariables);

//完成任务可以设置流程变量
processEngine.getTaskService().complete(taskId,variables); 

关于map variables 

//map的条数和数据表的条数是一样的
Map<String, Object> variables = new HashMap<>();
variables.put("variables1", "variables1");
variables.put("variables2", "variables2");
taskService.setVariables(taskId, variables); 

流程变量可以设置的类型 

Type nameDescription

string

Value is threaded as a java.lang.String. Raw JSON-text value is used when writing a variable.

integer

Value is threaded as a java.lang.Integer. When writing, JSON number value is used as base for conversion, falls back to JSON text.

short

Value is threaded as a java.lang.Short. When writing, JSON number value is used as base for conversion, falls back to JSON text.

long

Value is threaded as a java.lang.Long. When writing, JSON number value is used as base for conversion, falls back to JSON text.

double

Value is threaded as a java.lang.Double. When writing, JSON number value is used as base for conversion, falls back to JSON text.

boolean

Value is threaded as a java.lang.Boolean. When writing, JSON boolean value is used for conversion.

date

Value is treated as a java.util.Date. When writing, the JSON text will be converted using ISO-8601 date format.

binary

Binary variable, treated as an array of bytes. The value attribute is null, the valueUrl contains an URL pointing to the raw binary stream.

serializable

Serialized representation of a Serializable Java-object. As with the binary type, the value attribute is null, the valueUrl contains an URL pointing to the raw binary stream. All serializable variables (which are not of any of the above types) will be exposed as a variable of this type.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值