Activiti7_流程實例


什么是流程部署?

參與者(可以是用戶也可以是程序)按照流程定義內容發起一個流程,這就是一個流程實例。是動態的。

流程定義和流程實例的圖解

 

 

 

流程部署

使用壓縮包的方式部署流程

 1 /**
 2      * 壓縮包的方式部署流程
 3      */
 4     @Test
 5     public void deploymentByZip(){
 6         //獲取processEngine對象
 7         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
 8         //獲取一個RepositoryService對象
 9         RepositoryService repositoryService = processEngine.getRepositoryService();
10         //定義一個輸入流,加載Zip文件
11         InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("flowchart/process.zip");
12         //定義一個ZIPInputStream對象
13         ZipInputStream zipInputStream=new ZipInputStream(inputStream);
14         //流程部署
15         Deployment deploy = repositoryService.createDeployment()
16                 .addZipInputStream(zipInputStream)
17                 .name("請求審批流程")
18                 .key("processKey")
19                 .deploy();
20         System.out.println("流程名稱:"+deploy.getName());
21 
22     }
壓縮包的方式部署流程

 

結果

 

 

 

流程定義的查看

流程定義信息的查看

 1    /**
 2      * 流程定義信息的查看
 3      */
 4     @Test
 5     public void getProDef(){
 6         //獲取processEngine對象
 7         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
 8         //獲取一個RepositoryService對象
 9         RepositoryService repositoryService = processEngine.getRepositoryService();
10         //得到流程定義查看對象ProcessDefinitionQuery
11         ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
12         //指定查看的流程   processDefinitionKey()指定流程定義的key值   orderByProcessDefinitionVersion根據流程定義版本號排序
13         List<ProcessDefinition> holiday = processDefinitionQuery.processDefinitionKey("myProcess").orderByProcessDefinitionVersion().desc().list();
14         for (ProcessDefinition proDef:holiday) {
15             System.out.println("流程定義ID:"+proDef.getId());
16             System.out.println("流程定義的名稱:"+proDef.getName());
17             System.out.println("流程定義的Key:"+proDef.getKey());
18             System.out.println("流程定義的版本號:"+proDef.getVersion());
19         }
20     }
流程定義信息的查看

 

結果

 

 

 

流程刪除

 

 

 

 1  /**
 2      * 刪除流程:
 3      * 假設在刪除時,當前正在有該流程實例執行,那么會導致刪除失敗
 4      * 如果強制要求,則可以使用repositoryService.deleteDeployment("1",true); true代表級聯刪除
 5      */
 6     @Test
 7     public void deleteDeployment(){
 8         //獲取processEngine對象
 9         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
10         //獲取一個RepositoryService對象
11         RepositoryService repositoryService = processEngine.getRepositoryService();
12         //刪除流程
13         repositoryService.deleteDeployment("1",true);
14     }
刪除流程

 

結果

 

 

 

 

 

獲取數據庫中流程資源信息

 

 1     /**
 2      * 從數據中讀取資源文件到本地
 3      */
 4     @Test
 5     public void getResource() throws IOException {
 6         //獲取processEngine對象
 7         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
 8         //獲取一個RepositoryService對象
 9         RepositoryService repositoryService = processEngine.getRepositoryService();
10         //獲取到流程查詢對象
11         ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
12         //獲取到流程資源
13         processDefinitionQuery.processDefinitionKey("myProcess");
14         //獲取單一結果
15         ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
16         //流程部署的ID
17         String deploymentId = processDefinition.getDeploymentId();
18         //獲取到bpmnResource
19         InputStream bpmnStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
20         //獲取到png
21         InputStream pngStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
22         //構建輸出流
23         OutputStream bpmnOut = new FileOutputStream("D:\\北大青鳥作業\\2020年的練習\\20200402\\" + processDefinition.getResourceName());
24         OutputStream pngOut = new FileOutputStream("D:\\北大青鳥作業\\2020年的練習\\20200402\\" + processDefinition.getDiagramResourceName());
25         //將數據輸出到磁盤當中
26         IOUtils.copy(bpmnStream,bpmnOut);
27         IOUtils.copy(pngStream,pngOut);
28         //關流
29         pngOut.close();
30         bpmnOut.close();
31         pngStream.close();
32         bpmnStream.close();
33     }
從數據中讀取資源文件到本地

 

注意:IOUtils.copy()方法是import org.apache.commons.io.IOUtils;下的方法不是import sun.misc.IOUtils;

結果

 

 

 

業務系統和流程系統進行關聯

通過buessinessKey字段標識業務系統的主鍵數據ID,進而表名當前業務執行到了哪一個流程

 1    /**
 2      * 啟動流程實例和業務系統關聯
 3      * 正常應該先添加數據到業務表當中,拿到當前添加數據的主鍵ID,通過啟動流程實例,將主鍵ID交給BuessniessKkey
 4      * 代表和流程系統進行關聯
 5      */
 6     @Test
 7     public void startInstance(){
 8         /**
 9          * 讓張三在頁面提交,然后拿到ID
10          */
11 
12         /**
13          * 創建張三請假的實例流程
14          */
15         //獲取processEngine對象
16         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
17         RuntimeService runtimeService = processEngine.getRuntimeService();
18         //生成流程
19         ProcessInstance holiday = runtimeService.startProcessInstanceByKey("myProcess","2");
20 
21         /**
22          * 張三提交請假審批
23          */
24         //獲取一個TaskService對象
25         TaskService taskService = processEngine.getTaskService();
26         //獲取張三的任務
27         Task task = taskService.createTaskQuery().taskAssignee("zhangsan").processDefinitionKey("myProcess").singleResult();
28         //任務審批
29         taskService.complete(task.getId());
30     }
啟動流程實例和業務系統關聯

 

結果:

 

 

 

流程激活和掛起

所有流程定義的掛起

 1 /**
 2      * 所有流程定義的掛起
 3      */
 4     @Test
 5     public void allSuspend(){
 6         //獲取processEngine對象
 7         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
 8         //獲取repositoryService對象
 9         RepositoryService repositoryService = processEngine.getRepositoryService();
10         //獲取流程定義
11         ProcessDefinition holiday = repositoryService.createProcessDefinitionQuery().processDefinitionKey("myProcess").singleResult();
12         //獲取到當前流程定義是否為暫停狀態   suspended方法為true代表為暫停   false就是運行的
13         boolean suspended = holiday.isSuspended();
14         if(suspended){
15             repositoryService.activateProcessDefinitionById(holiday.getId(),true,null);
16             System.out.println("該流程定義激活");
17         }else{
18             repositoryService.suspendProcessDefinitionById(holiday.getId(),true,null);
19             System.out.println("該流程定義暫停");
20         }
21     }
所有流程定義的掛起

 

第一次運行結果

 

第二次運行結果

 

 

 

單個流程實例的掛起

 

 1 /**
 2      * 單個流程實例的掛起
 3      */
 4     @Test
 5     public void singleSuspend(){
 6         //獲取processEngine對象
 7         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
 8         //獲取RuntimeService對象
 9         RuntimeService runtimeService = processEngine.getRuntimeService();
10         ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("5001").singleResult();
11 
12         //獲取到當前流程定義是否為暫停狀態   suspended方法為true代表為暫停   false就是運行的
13         boolean suspended = processInstance.isSuspended();
14         if(suspended){
15             runtimeService.activateProcessInstanceById("5001");
16             System.out.println("xx的請假流程激活");
17         }else{
18             runtimeService.suspendProcessInstanceById("5001");
19             System.out.println("xx的請假流程掛起");
20         }
21     }
單個流程實例的掛起

 

第一次運行結果

 

第二次運行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM