1. 程式人生 > >第四章: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值:


然後執行設定變數方法,看看變量表會有對應的值:


執行獲取變量表的結果也是一樣的:

請假天數: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());
	}
	

先結束之前的未結束的任務。然後執行上面的方法就行了。


變量表有我們設進去的值。

還有一個是在人物結束時設定流程變數,這個是很常用的介面,比如請假時第一個節點,我們一般會在申請任務結束時設定申請的內容。

程式碼如下:

/**
	 * 完成任務時設定流程變數
	 */
	@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

這裡我直接使用上面啟動時設定變數執行後的結果,這裡為了驗證我們主要關注請假原因這個欄位的值,執行前的值是:


下面我們執行上面的程式碼:

注意這裡使用的id是任務id,不是物件id:


可以看出已經修改了。