Activiti學習筆記第十二篇:接收活動(receiveTask,即等待活動)

. 接收任務是一個簡單任務,它會等待對應消息的到達。 當前,官方只實現了這個任務的java語義。 當流程達到接收任務,流程狀態會保存到數據庫中。
  在任務創建後,意味着流程會進入等待狀態, 直到引擎接收了一個特定的消息, 這會觸發流程穿過接收任務繼續執行。

1. 流程圖

在這裏插入圖片描述

2. 部署流程定義+啓動流程實例

	/**
	 * ReceiceTask任務,機器自動完成的任務
	 * 只會在act_ru_execution表中產生一條數據
	 * @throws Exception
	 */
	@Test
	public void testExecution() throws Exception {
		// 1 發佈流程
		InputStream inputStreamBpmn = this.getClass().getResourceAsStream("receiveTask.bpmn");
		InputStream inputStreamPng = this.getClass().getResourceAsStream("receiveTask.png");
		processEngine.getRepositoryService()//
						.createDeployment()//
						.addInputStream("receiveTask.bpmn", inputStreamBpmn)//
						.addInputStream("receiveTask.png", inputStreamPng)//
						.deploy();
		
		// 2 啓動流程
		ProcessInstance pi = processEngine.getRuntimeService()//
							.startProcessInstanceByKey("receiveTaskDemo");
		System.out.println("pid:" + pi.getId());
		String pid = pi.getId();
		
		// 3查詢是否有一個執行對象在描述”彙總當日銷售額“
		Execution e1 = processEngine.getRuntimeService()//
						.createExecutionQuery()//
						.processInstanceId(pid)//
						.activityId("彙總當日銷售額")//
						.singleResult();

		// 4執行一堆邏輯,並設置流程變量
		Map<String,Object> vars = new HashMap<String, Object>();
		vars.put("當日銷售額", 10000);
		//  5流程向後執行一步:往後推移e1,使用signal給流程引擎信號,告訴他當前任務已經完成了,可以往後執行
		processEngine.getRuntimeService()
				.signal(e1.getId(),vars);
		
		// 6判斷當前流程是否在”給老闆發短信“節點
		Execution e2 = processEngine.getRuntimeService()//
						.createExecutionQuery()//
						.processInstanceId(pid)//
						.activityId("給總經理髮短信")//
						.singleResult();
		
		// 7獲取流程變量
		Integer money = (Integer) processEngine.getRuntimeService()//
								.getVariable(e2.getId(), "當日銷售額");
		System.out.println("老闆,今天賺了" +money);
		// 8向後執行一步:任務完成,往後推移”給老闆發短信“任務
		processEngine.getRuntimeService()//
				.signal(e2.getId());
		
		
		// 9查詢流程狀態
	    pi = processEngine.getRuntimeService()//
	    				.createProcessInstanceQuery()//
	    				.processInstanceId(pid)//
	    				.singleResult();
	    if(pi==null){
	    	    System.out.println("流程正常執行!!!,已經結束了");
	    }
	}

說明:
1) 當前任務(一般指機器自動完成,但需要耗費一定時間的工作)完成後,向後推移流程,可以調用runtimeService.signal(executionId),傳遞接收執行對象的id。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章