Activiti流程变量的设置与获取(五)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QCIWYY/article/details/48052267

一.流程图


概念

流程变量在整个工作流中扮演很重要的作用。

例如:请假流程中有请假天数、请假原因等一些参数都为流程变量的范围。

流程变量的作用域范围是流程实例。也就是说各个流程实例的流程变量是不相互影响的。

流程实例结束完成以后流程变量还保存在数据库中。

public class VariableTest {
	ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义
	 */
	@Test
	public void test1() {
		DeploymentBuilder deploymentBuilder = pe.getRepositoryService()
				.createDeployment();
		deploymentBuilder
				.addClasspathResource("cn/variable/variable.bpmn");
		deploymentBuilder
				.addClasspathResource("cn/variable/variable.png");
		Deployment deployment = deploymentBuilder.deploy();
	}

	/**
	 * 设置流程变量方式一:在启动流程实例时设置
	 * 操作的表:act_ru_execution act_ru_task act_ru_variable
	 */
	@Test
	public void test2() {
		String processDefinitionKey = "variable";
		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());
	}

	/**
	 * 设置流程变量方式二:在办理任务时设置
	 */
	@Test
	public void test3() {
		String taskId = "1606";
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("user", new User(1,"小王"));
		pe.getTaskService().complete(taskId, variables);
		
		
		/*Task task = pe.getTaskService().createTaskQuery().taskId(taskId).singleResult();
		String processInstanceId = task.getProcessInstanceId();
		System.out.println(processInstanceId);*/
	}
	
	/**
	 * 设置流程变量方式三:使用RuntimeService的方法设置
	 */
	@Test
	public void test4() {
		String executionId = "1601";//流程实例id
		String variableName = "key3";
		Object value = "value3";
		pe.getRuntimeService().setVariable(executionId , variableName , value);
	}
	
	/**
	 * 设置流程变量方式四:使用TaskService的方法设置
	 */
	@Test
	public void test5() {
		String taskId = "1705";
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("key4", 400);
		variables.put("k5", "v5");
		pe.getTaskService().setVariables(taskId , variables);
	}
	
	/**
	 * 获取流程变量方式一:使用RuntimeService的方法获取
	 */
	@Test
	public void test6(){
		String executionId = "1601";
		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);
		
		Collection<String> variableNames = new ArrayList<String>();
		variableNames.add("key2");
		variableNames.add("user");
		Map<String, Object> map = pe.getRuntimeService().getVariables(executionId, variableNames);
		System.out.println(map);
	}
	
	/**
	 * 获取流程变量方式二:使用TaskService的方法获取
	 */
	@Test
	public void test7(){
		String taskId = "1705";
		Map<String, Object> variables = pe.getTaskService().getVariables(taskId );
		System.out.println(variables);
	}
}


展开阅读全文

没有更多推荐了,返回首页