activity工作流

工作流Activiti的学习总结(十一)Activiti5.6和Spring3.03整合
 

 工作流模拟程序员面试过程情景如下:
      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.       
  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.  *      
  10.        
  11.           
  12.        
  13.      
  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  variables= execute.getVariables();  
  29.         Set>  infos=variables.entrySet();  
  30.         for (Entry 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.  *      
  12.              
  13.           
  14.          
  15.      
  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  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 variables = new HashMap();  
  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 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.  * 的例子来进行. 
  11.  *  
  12.  *  
  13.  * 为了测试方便,将获取服务的实现抽象出来,同时使用Spring自带的与JUnit4集成的工具( 
  14.  * AbstractTransactionalJUnit4SpringContextTests)。我们的实现类为AbstractSpringTest, 
  15.  *  
  16.  *  
  17.  * 本文采用activiti和spring整合中自动部署资源的功能配置如下: 
  18.  *  
  19.   
  20.      
  21.      
  22.      
  23.      
  24.      
  25.      
  26.      
  27.      
  28.      
  29.       
  30.      
  31.    
  32.  * @author longgangbai 
  33.  *  
  34.  * 2011-12-18  上午12:58:31 
  35.  */  
  36. @ContextConfiguration("classpath:application-context.xml")  
  37. public class ActivitiWithSpringTest extends  AbstractTransactionalJUnit4SpringContextTests{  
  38.   
  39.           
  40.         @Autowired  
  41.         private RuntimeService runtimeService;  
  42.       
  43.         @Autowired  
  44.         private TaskService taskService;  
  45.         @Autowired  
  46.         private ManagementService managerService;  
  47.         @Autowired  
  48.         private IdentityService identityService;  
  49.         @Autowired  
  50.         private FormService formService;  
  51.           
  52.           
  53.           
  54.         /** 
  55.          * 测试方法 
  56.          */  
  57.         @Test  
  58.         public void triggerMyProcess() {  
  59.             // 面试题目和答案  
  60.             Map variables = new HashMap();  
  61.             variables.put("姓名""程序员");  
  62.             variables.put("职务""高级软件工程师");  
  63.             variables.put("语言""Java/C#");  
  64.             variables.put("操作系统""Window,Linux,unix,Aix");  
  65.             variables.put("工作地点","苏州高新技术软件园");  
  66.               
  67.             // start process instance  
  68.             //获取创建一个实例  
  69.             ProcessInstance pi = runtimeService.startProcessInstanceByKey("DeveloperWorkExam", variables);  
  70.             assert (pi!=null);  
  71.               
  72.             List executions = runtimeService.createExecutionQuery().list();  
  73.             assert (executions.size()==1);  
  74.             //执行开发技术知识面试业务  
  75.             Execution execution = runtimeService.createExecutionQuery().singleResult();  
  76.             runtimeService.setVariable(execution.getId(), "type""receiveTask");  
  77.             runtimeService.signal(execution.getId());  
  78.               
  79.             executions = runtimeService.createExecutionQuery().list();  
  80.             assert (executions.size()==1);  
  81.             //执行人事面试业务  
  82.             execution = executions.get(0);  
  83.             runtimeService.setVariable(execution.getId(), "oper""录用此人....");  
  84.             runtimeService.signal(execution.getId());  
  85.         }  
  86. }  

 

 

运行结果:

Java代码   收藏代码

你可能感兴趣的:(Java)