1. 程式人生 > >activity啟動流程例項+設定流程變數+獲取流程變數+向後執行一步

activity啟動流程例項+設定流程變數+獲取流程變數+向後執行一步

ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();

/**部署流程定義(從inputStream)*/
@Test
public void deploymentProcess_inpustream(){
InputStream  inputBpmn=this.getClass().getResourceAsStream("receiveTask.bpmn");
InputStream  inputPng=this.getClass().getResourceAsStream("receiveTask.png");
Deployment deployment=processEngine.getRepositoryService()
             .createDeployment()
             .name("部署名稱")
             .addInputStream("receiveTask.bpmn", inputBpmn)   // 部署ID:3801
             .addInputStream("receiveTask.png", inputPng)     // 部署名稱:部署名稱
             .deploy();
System.out.println("部署ID:"+deployment.getId());
System.out.println("部署名稱:"+deployment.getName());
}

/**啟動流程例項+設定流程變數+獲取流程變數+向後執行一步*/
@Test
public void startProcessIntance(){
// 流程例項key
String processKey="receiveTask";
ProcessInstance pi=processEngine.getRuntimeService()
          .startProcessInstanceByKey(processKey);
System.out.println("流程例項ID:"+pi.getId());     // 流程例項ID:4101
System.out.println("流程定義ID:"+pi.getProcessDefinitionId()); // 流程定義ID:receiveTask:1:3804

// 查詢 任務中的 流程
ProcessInstance  pit=processEngine.getRuntimeService()
            .createProcessInstanceQuery()
            .processInstanceId(pi.getId())
            .singleResult();
/*if(pit==null){
HistoricProcessInstance hpi=processEngine.getHistoryService()
                   .createHistoricProcessInstanceQuery()
                   .processInstanceId(pi.getId())
                   .singleResult();
System.out.println(" "+hpi.getId()+" "+hpi.getProcessDefinitionId()+" "+hpi.getStartTime()+" "+hpi.getEndTime());
}*/

// 執行流程物件
Execution execution1=processEngine.getRuntimeService()
            .createExecutionQuery()
            .processInstanceId(pi.getId())
            .activityId("receivetask1")
            .singleResult();
/**使用流程變數設定當日銷售額,用來傳遞業務引數*/             
processEngine.getRuntimeService().setVariable(execution1.getId(), "彙總當日銷售額", 2000);

/**向後執行一步,如果流程處於等待狀態,使得流程繼續執行*/
processEngine.getRuntimeService().signal(execution1.getId());
/**查詢執行物件ID*/
Execution execution2=processEngine.getRuntimeService()
            .createExecutionQuery()
            .processInstanceId(pi.getId())
            .activityId("receivetask2")
            .singleResult();
/**從流程變數中獲取彙總當日銷售額的值*/
Integer value=(Integer)processEngine.getRuntimeService().getVariable(execution2.getId(), "彙總當日銷售額");
System.out.println("給老闆傳送簡訊:金額是:"+value);   //給老闆傳送簡訊:金額是:2000
/**向後執行一步,如果流程處於等待狀態,使得流程繼續執行*/
processEngine.getRuntimeService().signal(execution2.getId());
}