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

 工作流模拟程序员面试过程情景如下:
      1.开发知识面试或者笔试
      2.人事面试

 

流程图:

 

 工作流Activiti的学习总结(十一)Activiti5.6和Spring3.03整合_第1张图片

流程配置:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <process id="DeveloperWorkExam" name="DeveloperWorkExam">
    <startEvent id="startevent1" name="准备面试"></startEvent>
    <endEvent id="endevent1" name="面试通过"></endEvent>
    <receiveTask id="receivetask1" name="笔试以及面试通过">
      <extensionElements>
        <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener"></activiti:executionListener>
      </extensionElements>
    </receiveTask>
    <receiveTask id="receivetask2" name="人事面试">
      <extensionElements>
        <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.HumanResourceExamListener"></activiti:executionListener>
      </extensionElements>
    </receiveTask>
    <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="receivetask1"></sequenceFlow>
    <sequenceFlow id="flow2" name="" sourceRef="receivetask1" targetRef="receivetask2"></sequenceFlow>
    <sequenceFlow id="flow3" name="" sourceRef="receivetask2" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow4" name="" sourceRef="receivetask1" targetRef="endevent1"></sequenceFlow>
  </process>
</definitions>

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

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  <!-- 创建数据源 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
    <property name="username" value="sa" />
    <property name="password" value="" />
  </bean>
  
  <!-- 创建事务管理器 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>

<!-- 创建流程引擎配置对象 -->
 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="mailServerHost" value="localhost" />
    <property name="mailServerPort" value="5025" />
    <property name="jpaHandleTransaction" value="true" />
    <property name="jpaCloseEntityManager" value="true" />
    <property name="jobExecutorActivate" value="false" />
  </bean>
  
  <!--  创建流程引擎对象-->
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  </bean>
   <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
  
   <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
  
  <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

</beans>

 

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  <!-- 创建数据源 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
    <property name="username" value="sa" />
    <property name="password" value="" />
  </bean>
  
  <!-- 创建事务管理器 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>

<!-- 创建流程引擎配置对象 -->
 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="mailServerHost" value="localhost" />
    <property name="mailServerPort" value="5025" />
    <property name="jpaHandleTransaction" value="true" />
    <property name="jpaCloseEntityManager" value="true" />
    <property name="jobExecutorActivate" value="false" />
    <!-- 使用spring的自动资源加载部署方式部署 -->
    <property name="deploymentResources" value="classpath*:diagrams/*.bpmn20.xml" /> 
    
  </bean>
  
  <!--  创建流程引擎对象-->
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  </bean>
   <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
  
   <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
  
  <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

</beans>

 

代码实现:

/**
package com.easyway.workflow.activiti.exam;

import java.util.Map;

/**
 * 
 * 工作流中配置如下:
 *     <receiveTask id="receivetask1" name="笔试以及面试通过">
      <extensionElements>
         <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener"/>
      </extensionElements>
    </receiveTask>
    
 * @author longgangbai
 * 
 * 2011-12-18  上午12:38:24
 */
public class DeveloperKnowledgeExamListener implements JavaDelegate {
   private Logger logger=Logger.getLogger(DeveloperKnowledgeExamListener.class.getName());
	/* (non-Javadoc)
	 * @see org.activiti.engine.delegate.JavaDelegate#execute(org.activiti.engine.delegate.DelegateExecution)
	 */
	@Override
	public void execute(DelegateExecution execute) throws Exception {
		// TODO Auto-generated method stub
		logger.info("开始开发知识面试了....");
		Map<String,Object>  variables= execute.getVariables();
		Set<Entry<String,Object>>  infos=variables.entrySet();
		for (Entry<String, Object> entry : infos) {
			logger.info(entry.getKey()+" "+entry.getValue());
		}
		logger.info("开始开发知识面试了....");
		execute.setVariable("result", "该考生开发知识面试通过了....");
		
	}

}

 

 

 

/**
package com.easyway.workflow.activiti.exam;

import java.util.Map;

/**
 * 
 * 
 * 工作流中配置如下:
 * 
 *     <receiveTask id="receivetask2" name="人事面试">
            <extensionElements>
         <activiti:executionListener event="start" class="com.easyway.workflow.activiti.exam.HumanResourceExamListener"/>
      </extensionElements>  
    </receiveTask>
    
 * @author longgangbai
 * 
 * 2011-12-18  上午12:37:01
 */
public class HumanResourceExamListener implements JavaDelegate {
	  private Logger logger=Logger.getLogger(HumanResourceExamListener.class.getName());

	/* (non-Javadoc)
	 * @see org.activiti.engine.delegate.JavaDelegate#execute(org.activiti.engine.delegate.DelegateExecution)
	 */
	@Override
	public void execute(DelegateExecution execute) throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		logger.info("检查该考试是否通过开发知识考试....");
		Map<String,Object>  variables= execute.getVariables();
		String reuslt=variables.get("result").toString();
		logger.info("开发知识面试结果"+reuslt);
		logger.info("开始人事面试了....");
		execute.setVariable("result", "该考生开发知识面试通过了....");
		logger.info("人事面试完毕....等候通知....");
	}

}

 

 

 

/**
package com.easyway.workflow.activiti.exam;

import java.util.logging.Logger;
/**
 * 
 * 工作流模拟程序员面试过程情景如下:
 *     1.开发知识面试或者笔试
 *     2.人事面试
 * 
 * 在spring3.0.3和activiti5.6整合时候,建议采用activiti-spring-examples中的jar文件。
 * 如果么有完整 的jar文件,可以参考{activiti_home}/setup/files/dependencies/libs.spring.runtime.txt文件
 * 。(C:\mash_activiti-5.6\setup\files\dependencies)
 * 
 * 之所以要采用封装的原因,spring配置文件和activiti的配置文件分开发布部署。
 * 
 * @author longgangbai
 * 
 * 2011-12-18  上午01:32:17
 */
@ContextConfiguration("classpath:application-context-standalone.xml")
public abstract class AbstractSpringTest extends AbstractTransactionalJUnit4SpringContextTests {

	@SuppressWarnings("unused")
	private final Logger log = Logger.getLogger(AbstractSpringTest.class.getName());
	
	@SuppressWarnings("unused")
	@Autowired
	private ProcessEngine processEngine;
	@Autowired
	protected RepositoryService repositoryService;
	@Autowired
	protected RuntimeService runtimeService;
	@Autowired
	protected TaskService taskService;
	@Autowired
	protected HistoryService historyService;
	@Autowired
	protected ManagementService managementService;
	
	protected String deploymentId;
	
	public AbstractSpringTest() {
		super();
	}
	
	@Before
	public void initialize() throws Exception {
		beforeTest();
	}
	
	@After
	public void clean() throws Exception {
		afterTest();
	}
	
	protected abstract void beforeTest() throws Exception;
	
	protected abstract void afterTest() throws Exception;
}

 

 

/**
package com.easyway.workflow.activiti.exam;

import java.util.HashMap;
/**
 * 我把Activiti 5.6默认工程中有关JPA的部分配置删除了,其实通过这个就可以初始化Activiti引擎实例。
 * 为了测试方便,将获取服务的实现抽象出来,同时使用Spring自带的与JUnit4集成的工具(
 * AbstractTransactionalJUnit4SpringContextTests)。
 * 
 * 将classpath:activiti-context.xml在测试的时候进行加载,这样,在测试的子类中,只需要将其他的相
 * 关Spring配置单独加载即可,业务配置与流程配置分开,便于维护。 
 * @author longgangbai
 * 
 * 2011-12-18  上午01:37:14
 */
@ContextConfiguration("classpath:application-context-standalone.xml")
public class ActivitiWithSpringStandaloneTest extends AbstractSpringTest {

	@Override
	protected void beforeTest() throws Exception {
		Deployment deployment = repositoryService
		.createDeployment()
		.addClasspathResource(
				"diagrams/SprintActiviti56.bpmn20.xml")
		.deploy();	
		deploymentId = deployment.getId();
	}

	@Override
	protected void afterTest() throws Exception {
		repositoryService.deleteDeployment(deploymentId, true);	
	}
	
	@Test
	public void triggerMyProcess() {
		// prepare data packet
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("姓名", "程序员");
		variables.put("职务", "高级软件工程师");
		variables.put("语言", "Java/C#");
		variables.put("操作系统", "Window,Linux,unix,Aix");
		variables.put("工作地点","苏州高新技术软件园");
		
		// start process instance
		ProcessInstance pi = runtimeService.startProcessInstanceByKey("DeveloperWorkExam", variables);
		assert (pi!=null);
		
		List<Execution> executions = runtimeService.createExecutionQuery().list();
		assert (executions.size()==1);
		
		Execution execution = runtimeService.createExecutionQuery().singleResult();
		runtimeService.setVariable(execution.getId(), "type", "receiveTask");
		runtimeService.signal(execution.getId());
		
		executions = runtimeService.createExecutionQuery().list();
		assert (executions.size()==1);
		
		execution = executions.get(0);
		runtimeService.setVariable(execution.getId(), "oper", "录用此人....");
		runtimeService.signal(execution.getId());
	}
}

 

 

 

自动部署测试:

/**
package com.easyway.workflow.activiti.exam;


import java.util.HashMap;
/**
 * 
 * Activiti 5.6与Spring3.0.3整合也比较简单,其基本思想就是,通过Spring的IOC容器来管理Activiti的流程引擎
 * 实例以及相关服务,可见,主要是基于Activiti在与Spring整合上努力上,做好配置即可。这里基于前面的
 * <receiveTask>的例子来进行.
 * 
 * 
 * 为了测试方便,将获取服务的实现抽象出来,同时使用Spring自带的与JUnit4集成的工具(
 * AbstractTransactionalJUnit4SpringContextTests)。我们的实现类为AbstractSpringTest,
 * 
 * 
 * 本文采用activiti和spring整合中自动部署资源的功能配置如下:
 * <!-- 创建流程引擎配置对象 -->
 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="mailServerHost" value="localhost" />
    <property name="mailServerPort" value="5025" />
    <property name="jpaHandleTransaction" value="true" />
    <property name="jpaCloseEntityManager" value="true" />
    <property name="jobExecutorActivate" value="false" />
    <!-- 使用spring的自动资源加载部署方式部署 -->
    <property name="deploymentResources" value="classpath*:diagrams/*.bpmn20.xml" /> 
    
  </bean>
 * @author longgangbai
 * 
 * 2011-12-18  上午12:58:31
 */
@ContextConfiguration("classpath:application-context.xml")
public class ActivitiWithSpringTest extends  AbstractTransactionalJUnit4SpringContextTests{

		
        @Autowired
		private RuntimeService runtimeService;
	
        @Autowired
		private TaskService taskService;
        @Autowired
		private ManagementService managerService;
        @Autowired
		private IdentityService identityService;
        @Autowired
		private FormService formService;
        
        
        
        /**
         * 测试方法
         */
		@Test
		public void triggerMyProcess() {
			// 面试题目和答案
			Map<String, Object> variables = new HashMap<String, Object>();
			variables.put("姓名", "程序员");
			variables.put("职务", "高级软件工程师");
			variables.put("语言", "Java/C#");
			variables.put("操作系统", "Window,Linux,unix,Aix");
			variables.put("工作地点","苏州高新技术软件园");
			
			// start process instance
			//获取创建一个实例
			ProcessInstance pi = runtimeService.startProcessInstanceByKey("DeveloperWorkExam", variables);
			assert (pi!=null);
			
			List<Execution> executions = runtimeService.createExecutionQuery().list();
			assert (executions.size()==1);
			//执行开发技术知识面试业务
			Execution execution = runtimeService.createExecutionQuery().singleResult();
			runtimeService.setVariable(execution.getId(), "type", "receiveTask");
			runtimeService.signal(execution.getId());
			
			executions = runtimeService.createExecutionQuery().list();
			assert (executions.size()==1);
			//执行人事面试业务
			execution = executions.get(0);
			runtimeService.setVariable(execution.getId(), "oper", "录用此人....");
			runtimeService.signal(execution.getId());
		}
}

 

 

运行结果:

2011-12-18 13:30:01 org.activiti.engine.impl.db.DbSqlSession executeSchemaResource
信息: performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
2011-12-18 13:30:01 org.activiti.engine.impl.db.DbSqlSession executeSchemaResource
信息: performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
2011-12-18 13:30:01 org.activiti.engine.impl.db.DbSqlSession executeSchemaResource
信息: performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
2011-12-18 13:30:02 org.activiti.engine.impl.ProcessEngineImpl <init>
信息: ProcessEngine default created
2011-12-18 13:30:02 org.activiti.engine.impl.bpmn.deployer.BpmnDeployer deploy
信息: Processing resource diagrams/SprintActiviti56.bpmn20.xml
2011-12-18 13:30:02 org.activiti.engine.impl.bpmn.parser.BpmnParse parseDefinitionsAttributes
信息: XMLSchema currently not supported as typeLanguage
2011-12-18 13:30:02 org.activiti.engine.impl.bpmn.parser.BpmnParse parseDefinitionsAttributes
信息: XPath currently not supported as expressionLanguage
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener execute
信息: 开始开发知识面试了....
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener execute
信息: 姓名 程序员
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener execute
信息: 语言 Java/C#
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener execute
信息: 职务 高级软件工程师
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener execute
信息: 工作地点 苏州高新技术软件园
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener execute
信息: 操作系统 Window,Linux,unix,Aix
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.DeveloperKnowledgeExamListener execute
信息: 开始开发知识面试了....
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.HumanResourceExamListener execute
信息: 检查该考试是否通过开发知识考试....
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.HumanResourceExamListener execute
信息: 开发知识面试结果该考生开发知识面试通过了....
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.HumanResourceExamListener execute
信息: 开始人事面试了....
2011-12-18 13:30:02 com.easyway.workflow.activiti.exam.HumanResourceExamListener execute
信息: 人事面试完毕....等候通知....

 

你可能感兴趣的:(activiti5)