Activiti 5.6与Spring整合也比较简单,其基本思想就是,通过Spring的IOC容器来管理Activiti的流程引擎实例以及相关服务,可见,主要是基于Activiti在与Spring整合上努力上,做好配置即可。这里基于前面的<receiveTask>的例子来进行,可以参考:Activiti 5.6:流程活动自动与手工触发执行,简单的流程,如图所示:
Activiti 5.6与Spring整合,默认使用的配置文件为activiti-context.xml,当然可以在实际使用的时候覆盖掉默认的配置,或者增加自己的其他的Spring的配置。
我们也命名为activiti-context.xml,内容(安装Activiti 5.6的时候,实例工程中已经附带)如下所示:
<?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="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>
这里面,我把Activiti 5.6默认工程中有关JPA的部分配置删除了,其实通过这个就可以初始化Activiti引擎实例。为了测试方便,将获取服务的实现抽象出来,同时使用Spring自带的与JUnit4集成的工具(AbstractTransactionalJUnit4SpringContextTests)。我们的实现类为AbstractSpringTest,代码如下所示:
package org.shirdrn.workflow.activiti;
import java.util.logging.Logger;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
@ContextConfiguration("classpath:activiti-context.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;
}
上面,将classpath:activiti-context.xml在测试的时候进行加载,这样,在测试的子类中,只需要将其他的相关Spring配置单独加载即可,业务配置与流程配置分开,便于维护。
具体测试用例,这里实现了一个简单的Spring Bean,配置文件为mySpringContext.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="mySpringBean" class="org.shirdrn.workflow.activiti.spring.MySpringBean">
<property name="id" value="65536" />
<property name="name" value="shirdrn" />
</bean>
</beans>
Spring Bean的实现,代码如下所示:
package org.shirdrn.workflow.activiti.spring;
import java.io.Serializable;
public class MySpringBean implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MySpringBean[id="+ id + ",name=" + name + "]";
}
}
下面,看看我们具体的测试用例,实现代码如下所示:
package org.shirdrn.workflow.activiti.spring;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.shirdrn.workflow.activiti.AbstractSpringTest;
import org.shirdrn.workflow.activiti.subprocess.Merchant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration({
"classpath:org/shirdrn/workflow/activiti/spring/mySpringContext.xml"})
public class ActivitiWithSpringTest extends AbstractSpringTest {
@Autowired
private MySpringBean mySpringBean;
@Override
protected void beforeTest() throws Exception {
Deployment deployment = repositoryService
.createDeployment()
.addClasspathResource(
"diagrams/Task.ReceiveTask.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>();
Map<String, Object> subVariables = new HashMap<String, Object>();
variables.put("maxTransCount", 1000000);
variables.put("merchant", new Merchant("ICBC"));
variables.put("protocol", "UM32");
variables.put("repository", "10.10.38.99:/home/shirdrn/repository");
variables.put("in", subVariables);
variables.put("out", new HashMap<String, Object>());
// start process instance
ProcessInstance pi = runtimeService.startProcessInstanceByKey("MyReceiveTask", 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", mySpringBean.getName());
runtimeService.signal(execution.getId());
}
}
运行程序,结果信息如下所示:
011-3-23 18:21:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [activiti-context.xml]
2011-3-23 18:21:29 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [org/shirdrn/workflow/activiti/spring/mySpringContext.xml]
... ...
2011-3-23 18:21:33 org.shirdrn.workflow.activiti.task.CheckBankReceiveTask execute
信息: i am CheckBankReceiveTask.
in : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], maxTransCount=1000000, in={}, out={}}
2011-3-23 18:21:33 org.shirdrn.workflow.activiti.task.CheckMerchantReceiveTask execute
信息: i am CheckMerchantReceiveTask.
in : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], maxTransCount=1000000, type=receiveTask, in={}, out={}