"Activiti Workflow Framework" Topic (5)-Activiti Workflow Framework Process Variables

1. Process variable concept

Use process variables to transfer business data, such as the reason for leave, the number of days and other information.
Insert picture description here

2. Set process variables

2.1. Set when the process instance is started

When starting a process instance , you can add process variables. This is an opportunity to add process variables.

/**
 * 设置流程变量方式一:在启动流程实例时设置
 */
@Test
public void test1() {
    
    
	String processDefinitionKey = "HelloWorldKsy";
	Map<String, Object> variables = new HashMap<String, Object>();
	variables.put("key1", "value1");
	variables.put("key2", 200);
	ProcessInstance pi = pe.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables);
	System.out.println(pi.getId());
}

Description:

1)	在启动流程实例时,通过重载startProcessInstanceByKey的方法可以加载流程变量。
2)	第二个参数要求是Map<String ,Object>类型,意味着可以添加多个流程变量。
3)	当这段代码执行完以后,会在数据库表act_ru_variable中添加两行记录。

2.2. Set when handling tasks

When handling tasks, sometimes after the tasks are completed, some information must be transmitted to the system. At this time, you can use the TaskService class to add process instances.

/**
 * 设置流程变量方式二:在办理任务时设置
 */
@Test
public void test3() {
    
    
	String taskId = "50006";
	Map<String, Object> variables = new HashMap<String, Object>();
	variables.put("user", new User(1,"小王"));
	pe.getTaskService().complete(taskId, variables);
}

3. Types supported by process variables

Jdk provided data types ( String, , Integer, ...)List the custom entity classes (interfaces required to achieve the sequence ) as shown is the type of process variables listed in the Website: As can be seen from the figure, and includes most of the package type Date, String, and the types of classes that implement the Serializable interface.Map
Serializable

Insert picture description here

4. Get process variables

Use RuntimeService method to obtain

The process variables can be obtained through the runTimeService method. Note: These process variables are read from the act_ru_variable table.

/**
 * 获取流程变量方式一:使用RuntimeService的方法获取
 */
@Test
public void test6() {
    
    
	String executionId = "2501";
	Map<String, Object> variables = pe.getRuntimeService().getVariables(executionId);
	// System.out.println(variables);
	Set<String> set = variables.keySet();// key2 key1 user
	for (String key : set) {
    
    
		Object value = variables.get(key);
		System.out.println(key + " = " + value);
	}

	Object value = pe.getRuntimeService().getVariable(executionId, "user");
	System.out.println(value);
}

Guess you like

Origin blog.csdn.net/BruceLiu_code/article/details/113638496