Activity工作流

 工作流模擬程序員面試過程情景如下:
      1.開發知識面試或者筆試
      2.人事面試

 

流程圖:

 

 

流程配置:

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="DeveloperWorkExam" name="DeveloperWorkExam">  
  4.     <startEvent id="startevent1" name="準備面試"></startEvent>  
  5.     <endEvent id="endevent1" name="面試通過"></endEvent>  
  6.     <receiveTask id="receivetask1" name="筆試以及面試通過">  
  7.       <extensionElements>  
  8.         <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener"></activiti:executionListener>  
  9.       </extensionElements>  
  10.     </receiveTask>  
  11.     <receiveTask id="receivetask2" name="人事面試">  
  12.       <extensionElements>  
  13.         <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.HumanResourceExamListener"></activiti:executionListener>  
  14.       </extensionElements>  
  15.     </receiveTask>  
  16.     <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="receivetask1"></sequenceFlow>  
  17.     <sequenceFlow id="flow2" name="" sourceRef="receivetask1" targetRef="receivetask2"></sequenceFlow>  
  18.     <sequenceFlow id="flow3" name="" sourceRef="receivetask2" targetRef="endevent1"></sequenceFlow>  
  19.     <sequenceFlow id="flow4" name="" sourceRef="receivetask1" targetRef="endevent1"></sequenceFlow>  
  20.   </process>  
  21. </definitions>  

spring配置application-context-standalone.xml如下:

 

Xml代碼   收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:tx="http://www.springframework.org/schema/tx"  
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.                            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  9.   <!-- 創建數據源 -->  
  10.   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  11.     <property name="driverClass" value="org.h2.Driver" />  
  12.     <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
  13.     <property name="username" value="sa" />  
  14.     <property name="password" value="" />  
  15.   </bean>  
  16.     
  17.   <!-- 創建事務管理器 -->  
  18.   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.     <property name="dataSource" ref="dataSource" />  
  20.   </bean>  
  21.   
  22. <!-- 創建流程引擎配置對象 -->  
  23.  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
  24.     <property name="dataSource" ref="dataSource" />  
  25.     <property name="transactionManager" ref="transactionManager" />  
  26.     <property name="databaseSchemaUpdate" value="true" />  
  27.     <property name="mailServerHost" value="localhost" />  
  28.     <property name="mailServerPort" value="5025" />  
  29.     <property name="jpaHandleTransaction" value="true" />  
  30.     <property name="jpaCloseEntityManager" value="true" />  
  31.     <property name="jobExecutorActivate" value="false" />  
  32.   </bean>  
  33.     
  34.   <!--  創建流程引擎對象-->  
  35.   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  36.     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  37.   </bean>  
  38.    <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />  
  39.     
  40.    <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />  
  41.     
  42.   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  43.   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  44.   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  45.   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  46.   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
  47.   
  48. </beans>  

 

application-context.xml

Xml代碼   收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:tx="http://www.springframework.org/schema/tx"  
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.                            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  9.   <!-- 創建數據源 -->  
  10.   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  11.     <property name="driverClass" value="org.h2.Driver" />  
  12.     <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
  13.     <property name="username" value="sa" />  
  14.     <property name="password" value="" />  
  15.   </bean>  
  16.     
  17.   <!-- 創建事務管理器 -->  
  18.   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.     <property name="dataSource" ref="dataSource" />  
  20.   </bean>  
  21.   
  22. <!-- 創建流程引擎配置對象 -->  
  23.  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
  24.     <property name="dataSource" ref="dataSource" />  
  25.     <property name="transactionManager" ref="transactionManager" />  
  26.     <property name="databaseSchemaUpdate" value="true" />  
  27.     <property name="mailServerHost" value="localhost" />  
  28.     <property name="mailServerPort" value="5025" />  
  29.     <property name="jpaHandleTransaction" value="true" />  
  30.     <property name="jpaCloseEntityManager" value="true" />  
  31.     <property name="jobExecutorActivate" value="false" />  
  32.     <!-- 使用spring的自動資源加載部署方式部署 -->  
  33.     <property name="deploymentResources" value="classpath*:diagrams/*.bpmn20.xml" />   
  34.       
  35.   </bean>  
  36.     
  37.   <!--  創建流程引擎對象-->  
  38.   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  39.     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  40.   </bean>  
  41.    <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />  
  42.     
  43.    <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />  
  44.     
  45.   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  46.   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  47.   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  48.   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  49.   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
  50.   
  51. </beans>  

 

代碼實現:

Java代碼   收藏代碼
  1. /** 
  2. package com.easyway.workflow.activiti.exam; 
  3.  
  4. import java.util.Map; 
  5.  
  6. /** 
  7.  *  
  8.  * 工作流中配置如下: 
  9.  *     <receiveTask id="receivetask1" name="筆試以及面試通過"> 
  10.       <extensionElements> 
  11.          <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener"/> 
  12.       </extensionElements> 
  13.     </receiveTask> 
  14.      
  15.  * @author longgangbai 
  16.  *  
  17.  * 2011-12-18  上午12:38:24 
  18.  */  
  19. public class DeveloperKnowledgeExamListener implements JavaDelegate {  
  20.    private Logger logger=Logger.getLogger(DeveloperKnowledgeExamListener.class.getName());  
  21.     /* (non-Javadoc) 
  22.      * @see org.activiti.engine.delegate.JavaDelegate#execute(org.activiti.engine.delegate.DelegateExecution) 
  23.      */  
  24.     @Override  
  25.     public void execute(DelegateExecution execute) throws Exception {  
  26.         // TODO Auto-generated method stub  
  27.         logger.info("開始開發知識面試了....");  
  28.         Map<String,Object>  variables= execute.getVariables();  
  29.         Set<Entry<String,Object>>  infos=variables.entrySet();  
  30.         for (Entry<String, Object> entry : infos) {  
  31.             logger.info(entry.getKey()+" "+entry.getValue());  
  32.         }  
  33.         logger.info("開始開發知識面試了....");  
  34.         execute.setVariable("result""該考生開發知識面試通過了....");  
  35.           
  36.     }  
  37.   
  38. }  

 

 

 

Java代碼   收藏代碼
  1. /** 
  2. package com.easyway.workflow.activiti.exam; 
  3.  
  4. import java.util.Map; 
  5.  
  6. /** 
  7.  *  
  8.  *  
  9.  * 工作流中配置如下: 
  10.  *  
  11.  *     <receiveTask id="receivetask2" name="人事面試"> 
  12.             <extensionElements> 
  13.          <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.HumanResourceExamListener"/> 
  14.       </extensionElements>   
  15.     </receiveTask> 
  16.      
  17.  * @author longgangbai 
  18.  *  
  19.  * 2011-12-18  上午12:37:01 
  20.  */  
  21. public class HumanResourceExamListener implements JavaDelegate {  
  22.       private Logger logger=Logger.getLogger(HumanResourceExamListener.class.getName());  
  23.   
  24.     /* (non-Javadoc) 
  25.      * @see org.activiti.engine.delegate.JavaDelegate#execute(org.activiti.engine.delegate.DelegateExecution) 
  26.      */  
  27.     @Override  
  28.     public void execute(DelegateExecution execute) throws Exception {  
  29.         // TODO Auto-generated method stub  
  30.         // TODO Auto-generated method stub  
  31.         logger.info("檢查該考試是否通過開發知識考試....");  
  32.         Map<String,Object>  variables= execute.getVariables();  
  33.         String reuslt=variables.get("result").toString();  
  34.         logger.info("開發知識面試結果"+reuslt);  
  35.         logger.info("開始人事面試了....");  
  36.         execute.setVariable("result""該考生開發知識面試通過了....");  
  37.         logger.info("人事面試完畢....等候通知....");  
  38.     }  
  39.   
  40. }  

 

 

 

Java代碼   收藏代碼
  1. /** 
  2. package com.easyway.workflow.activiti.exam; 
  3.  
  4. import java.util.logging.Logger; 
  5. /** 
  6.  *  
  7.  * 工作流模擬程序員面試過程情景如下: 
  8.  *     1.開發知識面試或者筆試 
  9.  *     2.人事面試 
  10.  *  
  11.  * 在spring3.0.3和activiti5.6整合時候,建議採用activiti-spring-examples中的jar文件。 
  12.  * 如果麼有完整 的jar文件,可以參考{activiti_home}/setup/files/dependencies/libs.spring.runtime.txt文件 
  13.  * 。(C:\mash_activiti-5.6\setup\files\dependencies) 
  14.  *  
  15.  * 之所以要採用封裝的原因,spring配置文件和activiti的配置文件分開發布部署。 
  16.  *  
  17.  * @author longgangbai 
  18.  *  
  19.  * 2011-12-18  上午01:32:17 
  20.  */  
  21. @ContextConfiguration("classpath:application-context-standalone.xml")  
  22. public abstract class AbstractSpringTest extends AbstractTransactionalJUnit4SpringContextTests {  
  23.   
  24.     @SuppressWarnings("unused")  
  25.     private final Logger log = Logger.getLogger(AbstractSpringTest.class.getName());  
  26.       
  27.     @SuppressWarnings("unused")  
  28.     @Autowired  
  29.     private ProcessEngine processEngine;  
  30.     @Autowired  
  31.     protected RepositoryService repositoryService;  
  32.     @Autowired  
  33.     protected RuntimeService runtimeService;  
  34.     @Autowired  
  35.     protected TaskService taskService;  
  36.     @Autowired  
  37.     protected HistoryService historyService;  
  38.     @Autowired  
  39.     protected ManagementService managementService;  
  40.       
  41.     protected String deploymentId;  
  42.       
  43.     public AbstractSpringTest() {  
  44.         super();  
  45.     }  
  46.       
  47.     @Before  
  48.     public void initialize() throws Exception {  
  49.         beforeTest();  
  50.     }  
  51.       
  52.     @After  
  53.     public void clean() throws Exception {  
  54.         afterTest();  
  55.     }  
  56.       
  57.     protected abstract void beforeTest() throws Exception;  
  58.       
  59.     protected abstract void afterTest() throws Exception;  
  60. }  

 

 

Java代碼   收藏代碼
  1. /** 
  2. package com.easyway.workflow.activiti.exam; 
  3.  
  4. import java.util.HashMap; 
  5. /** 
  6.  * 我把Activiti 5.6默認工程中有關JPA的部分配置刪除了,其實通過這個就可以初始化Activiti引擎實例。 
  7.  * 爲了測試方便,將獲取服務的實現抽象出來,同時使用Spring自帶的與JUnit4集成的工具( 
  8.  * AbstractTransactionalJUnit4SpringContextTests)。 
  9.  *  
  10.  * 將classpath:activiti-context.xml在測試的時候進行加載,這樣,在測試的子類中,只需要將其他的相 
  11.  * 關Spring配置單獨加載即可,業務配置與流程配置分開,便於維護。  
  12.  * @author longgangbai 
  13.  *  
  14.  * 2011-12-18  上午01:37:14 
  15.  */  
  16. @ContextConfiguration("classpath:application-context-standalone.xml")  
  17. public class ActivitiWithSpringStandaloneTest extends AbstractSpringTest {  
  18.   
  19.     @Override  
  20.     protected void beforeTest() throws Exception {  
  21.         Deployment deployment = repositoryService  
  22.         .createDeployment()  
  23.         .addClasspathResource(  
  24.                 "diagrams/SprintActiviti56.bpmn20.xml")  
  25.         .deploy();    
  26.         deploymentId = deployment.getId();  
  27.     }  
  28.   
  29.     @Override  
  30.     protected void afterTest() throws Exception {  
  31.         repositoryService.deleteDeployment(deploymentId, true);   
  32.     }  
  33.       
  34.     @Test  
  35.     public void triggerMyProcess() {  
  36.         // prepare data packet  
  37.         Map<String, Object> variables = new HashMap<String, Object>();  
  38.         variables.put("姓名""程序員");  
  39.         variables.put("職務""高級軟件工程師");  
  40.         variables.put("語言""Java/C#");  
  41.         variables.put("操作系統""Window,Linux,unix,Aix");  
  42.         variables.put("工作地點","蘇州高新技術軟件園");  
  43.           
  44.         // start process instance  
  45.         ProcessInstance pi = runtimeService.startProcessInstanceByKey("DeveloperWorkExam", variables);  
  46.         assert (pi!=null);  
  47.           
  48.         List<Execution> executions = runtimeService.createExecutionQuery().list();  
  49.         assert (executions.size()==1);  
  50.           
  51.         Execution execution = runtimeService.createExecutionQuery().singleResult();  
  52.         runtimeService.setVariable(execution.getId(), "type""receiveTask");  
  53.         runtimeService.signal(execution.getId());  
  54.           
  55.         executions = runtimeService.createExecutionQuery().list();  
  56.         assert (executions.size()==1);  
  57.           
  58.         execution = executions.get(0);  
  59.         runtimeService.setVariable(execution.getId(), "oper""錄用此人....");  
  60.         runtimeService.signal(execution.getId());  
  61.     }  
  62. }  

 

 

 

自動部署測試:

Java代碼   收藏代碼
  1. /** 
  2. package com.easyway.workflow.activiti.exam; 
  3.  
  4.  
  5. import java.util.HashMap; 
  6. /** 
  7.  *  
  8.  * Activiti 5.6與Spring3.0.3整合也比較簡單,其基本思想就是,通過Spring的IOC容器來管理Activiti的流程引擎 
  9.  * 實例以及相關服務,可見,主要是基於Activiti在與Spring整合上努力上,做好配置即可。這裏基於前面的 
  10.  * <receiveTask>的例子來進行. 
  11.  *  
  12.  *  
  13.  * 爲了測試方便,將獲取服務的實現抽象出來,同時使用Spring自帶的與JUnit4集成的工具( 
  14.  * AbstractTransactionalJUnit4SpringContextTests)。我們的實現類爲AbstractSpringTest, 
  15.  *  
  16.  *  
  17.  * 本文采用activiti和spring整合中自動部署資源的功能配置如下: 
  18.  * <!-- 創建流程引擎配置對象 --> 
  19.  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> 
  20.     <property name="dataSource" ref="dataSource" /> 
  21.     <property name="transactionManager" ref="transactionManager" /> 
  22.  *  
  23.  * 爲了測試方便,將獲取服務的實現抽象出來,同時使用Spring自帶的與JUnit4集成的工具( 
  24.  * AbstractTransactionalJUnit4SpringContextTests)。我們的實現類爲AbstractSpringTest, 
  25.  *  
  26.  *  
  27.  * 本文采用activiti和spring整合中自動部署資源的功能配置如下: 
  28.  * <!-- 創建流程引擎配置對象 --> 
  29.  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> 
  30.     <property name="dataSource" ref="dataSource" /> 
  31.     <property name="transactionManager" ref="transactionManager" /> 
  32.     <property name="databaseSchemaUpdate" value="true" /> 
  33.     <property name="mailServerHost" value="localhost" /> 
  34.     <property name="mailServerPort" value="5025" /> 
  35.     <property name="jpaHandleTransaction" value="true" /> 
  36.     <property name="jpaCloseEntityManager" value="true" /> 
  37.     <property name="jobExecutorActivate" value="false" /> 
  38.     <!-- 使用spring的自動資源加載部署方式部署 -->ot; value="true" /> 
  39.     <property name="mailServerHost" value="localhost" /> 
  40.     <property name="mailServerPort" value="5025" /> 
  41.     <property name="jpaHandleTransaction" value="true" /> 
  42.     <property name="jpaCloseEntityManager" value="true" /> 
  43.     <property name="jobExecutorActivate" value="false" /> 
  44.     <!-- 使用spring的自動資源加載部署方式部署 --> 
  45.     <property name="deploymentResources" value="classpath*:diagrams/*.bpmn20.xml" />  
  46.      
  47.   </bean> 
  48.  * @author longgangbai 
  49.  *  
  50.  * 2011-12-18  上午12:58:31 
  51.  */  
  52. @ContextConfiguration("classpath:application-context.xml")  
  53.     <property name="jpaCloseEntityManager" value="true" /> 
相關文章
相關標籤/搜索