1. 程式人生 > >工作流Activiti的學習總結(八)Activiti自動執行的應用

工作流Activiti的學習總結(八)Activiti自動執行的應用

  工作流activiti自動執行任務的開發應用 

工作流模擬某公司請假流程情景如下:
   1.開發人員請假流程,如果開發人員請假,如果請假天數小於3天,組長批准,人事批准即可請假。
   2.如果請假大約三天,需要專案經理或者產品經理批准,並且專案總監批准
   
 模擬自動審批:

    只需要啟動一個流程例項,它會自動執行到結束。這種情況下,你不需要關注流
   程的執行進度,而只需要把精力集中在每個結點的處理邏輯(通常是簡單或者複雜的商業邏輯)上。

流程設計圖如下:

 

流程配置檔案AutoExecuteTask.bpmn20.xml如下:

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
     xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">  
  3.  <process id="AutoExecuteTask" name="AutoExecuteTask">  
  4.     <startEvent id="startevent1" name="Start">
    </startEvent>  
  5.     <parallelGateway id="parallelgateway1" name="多種請假方式"></parallelGateway>  
  6.     <serviceTask id="servicetask1" name="專案經理同意" activiti:class="com.easyway.workflow.activiti.gateway.DeveloperManagerServiceTask"></serviceTask>  
  7.     <serviceTask id="servicetask2" name="產品經理同意" activiti:class="com.easyway.workflow.activiti.gateway.ProductManagerServiceTask"></serviceTask>  
  8.     <serviceTask id="servicetask3" name="專案組長同意" activiti:class="com.easyway.workflow.activiti.gateway.TeamLeaderServiceTask"></serviceTask>  
  9.     <parallelGateway id="parallelgateway2" name="First Join"></parallelGateway>  
  10.     <serviceTask id="servicetask4" name="專案總監同意" activiti:class="com.easyway.workflow.activiti.gateway.ProductBossServiceTask"></serviceTask>  
  11.     <parallelGateway id="parallelgateway3" name="人事判斷"></parallelGateway>  
  12.     <serviceTask id="servicetask5" name="人事部門同意" activiti:class="com.easyway.workflow.activiti.gateway.HumanResouceServiceTask"></serviceTask>  
  13.     <endEvent id="endevent1" name="End"></endEvent>  
  14.     <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="parallelgateway1"></sequenceFlow>  
  15.     <sequenceFlow id="flow2" name="" sourceRef="parallelgateway1" targetRef="servicetask1"></sequenceFlow>  
  16.     <sequenceFlow id="flow3" name="" sourceRef="parallelgateway1" targetRef="servicetask2"></sequenceFlow>  
  17.     <sequenceFlow id="flow4" name="" sourceRef="parallelgateway1" targetRef="servicetask3"></sequenceFlow>  
  18.     <sequenceFlow id="flow5" name="" sourceRef="servicetask1" targetRef="parallelgateway2"></sequenceFlow>  
  19.     <sequenceFlow id="flow6" name="" sourceRef="servicetask2" targetRef="parallelgateway2"></sequenceFlow>  
  20.     <sequenceFlow id="flow7" name="" sourceRef="servicetask3" targetRef="parallelgateway3"></sequenceFlow>  
  21.     <sequenceFlow id="flow8" name="" sourceRef="parallelgateway2" targetRef="servicetask4"></sequenceFlow>  
  22.     <sequenceFlow id="flow9" name="" sourceRef="servicetask4" targetRef="parallelgateway3"></sequenceFlow>  
  23.     <sequenceFlow id="flow10" name="" sourceRef="parallelgateway3" targetRef="servicetask5"></sequenceFlow>  
  24.     <sequenceFlow id="flow11" name="" sourceRef="servicetask5" targetRef="endevent1"></sequenceFlow>  
  25.   </process>  
  26. </definitions>  
Java程式碼  收藏程式碼
  1. package com.easyway.workflow.activiti.gateway;  
  2. import java.util.logging.Logger;  
  3. /** 
  4.  * execute方法的引數DelegateExecution execution可以在流程中各個結點之間傳遞流程變數。  
  5.  *通過基於Activiti 5.6的<parallelGateway>、<serviceTask>、<receiveTask>、<userTask>元素。 
  6.  * 
  7.  *自動執行  
  8. <serviceTask>元素,可以實現自動活動,語法如下所示:  
  9. <serviceTask id="serviceTaskId" name="serviceTaskName"  
  10. activiti:class="org.shirdrn.workflow.activiti.gateway.ServiceTaskClass"/>  
  11. 其中,activiti:class屬性為該結點對應的處理類,該類要求實現org.activiti.engine.delegate.JavaDelegate介面 
  12.  * 
  13.  * <serviceTask id="servicetask1" name="專案經理同意" activiti:class="com.easyway.workflow.activiti.gateway.DeveloperManagerServiceTask"></serviceTask> 
  14.  * 專案經理審批過程 
  15.  * @author longgangbai 
  16.  *  
  17.  *  
  18.  * 2011-12-17  下午07:45:47 
  19.  */  
  20. public class DeveloperManagerServiceTask implements JavaDelegate {  
  21.     private final Logger log = Logger.getLogger(DeveloperManagerServiceTask.class.getName());  
  22.     @Override  
  23.     public void execute(DelegateExecution execution) throws Exception {  
  24.         Thread.sleep(10000);  
  25.         log.info("variavles=" + execution.getVariables());  
  26.         execution.setVariable("專案經理""請假天數大約3天,同意請假。");  
  27.         log.info("專案經理,請假天數大約3天,同意請假。.");         
  28.     }  
  29. }  
Java程式碼  收藏程式碼
  1. package com.easyway.workflow.activiti.gateway;  
  2. import java.util.logging.Logger;  
  3. /** 
  4.   * execute方法的引數DelegateExecution execution可以在流程中各個結點之間傳遞流程變數。  
  5.  * 
  6.  * 
  7.  * <serviceTask id="servicetask2" name="產品經理同意" activiti:class="com.easyway.workflow.activiti.gateway.ProductManagerServiceTask"></serviceTask> 
  8.  * 產品經理審批過程 
  9.  * @author longgangbai 
  10.  *  
  11.  * 2011-12-17  下午07:45:47 
  12.  */  
  13. public class ProductManagerServiceTask implements JavaDelegate {  
  14.     private final Logger log = Logger.getLogger(ProductManagerServiceTask.class.getName());  
  15.     @Override  
  16.     public void execute(DelegateExecution execution) throws Exception {  
  17.         Thread.sleep(10000);  
  18.         log.info("variavles=" + execution.getVariables());  
  19.         execution.setVariable("產品經理""請假天數大約3天,同意請假。");  
  20.         log.info("產品經理,請假天數大約3天,同意請假。");              
  21.     }  
  22. }  
Java程式碼  收藏程式碼
  1. package com.easyway.workflow.activiti.gateway;  
  2. import java.util.logging.Logger;  
  3. /** 
  4.  * 專案組長批准請假 
  5.  *    模擬當開發人員請假小於3天,組長比準 
  6.  *     
  7.  *     
  8.  *     <serviceTask id="servicetask3" name="專案組長同意" activiti:class="com.easyway.workflow.activiti.gateway.TeamLeaderServiceTask"></serviceTask> 
  9.  * @author longgangbai 
  10.  *  
  11.  * 2011-12-17  上午09:07:37 
  12.  */  
  13. public class TeamLeaderServiceTask implements JavaDelegate {  
  14.     private final Logger log = Logger.getLogger(TeamLeaderServiceTask.class.getName());  
  15.     @Override  
  16.     public void execute(DelegateExecution execution) throws Exception {  
  17.         Thread.sleep(10000);  
  18.         log.info("variavles=" + execution.getVariables());  
  19.         execution.setVariable("專案組長""請假天數小於3天,同意請假。");  
  20.         log.info("專案組長,請假天數小於3天,同意請假。");  
  21.     }  
  22. }  
Java程式碼  收藏程式碼
  1. package