一、概述
Activiti相对Jbpm来说,与Spring整合更加完美,具体可见本文的详细介绍
二、开发环境的准备
1. Maven的环境任务,请参考第一篇 (
Activiti入门篇—Maven的环境准备)
2. Activiti的Eclipse插件安装
插件更新地址:http://activiti.org/designer/update/
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161003fun9cbz92c9bfepc.png)
安装完成后,可以设计一请假的流程,如下所示:
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161004lrjrw5ror38ffxx3.png)
右键holidayRequest.bpmn
文件,用Xml Editor
打开,可以看到其生成的XML
文件格式:
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161004r63elkz3ee43pkew.png)
3. 安装数据库Mysql(略)
创建一数据库名为activiti,暂时不放表
三、Activiti与Spring的整合
1. 新建Maven项目
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161008vjrtdnweswzt2duu.png)
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161009tc23jxkx2k8xbqqx.png)
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161009g39ig3shihir69i9.png)
2. 加入依赖包
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161010rqbggphfoxggz6ao.png)
依赖的pom.xml
文件如下所示:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.hotent.demo</groupId>
- <artifactId>activiti</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>org.activiti</groupId>
- <artifactId>activiti-engine</artifactId>
- <version>5.15.1</version>
- </dependency>
- <dependency>
- <groupId>org.activiti</groupId>
- <artifactId>activiti-spring</artifactId>
- <version>5.15.1</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.29</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>3.2.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>3.2.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>3.2.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>3.2.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>3.2.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>3.2.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>3.2.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.2.5</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.6</version>
- </dependency>
- <dependency>
- <groupId>org.activiti</groupId>
- <artifactId>activiti-bpmn-model</artifactId>
- <version>5.15.1</version>
- </dependency>
- <dependency>
- <groupId>org.activiti</groupId>
- <artifactId>activiti-bpmn-converter</artifactId>
- <version>5.15.1</version>
- </dependency>
- <dependency>
- <groupId>org.activiti</groupId>
- <artifactId>activiti-process-validation</artifactId>
- <version>5.15.1</version>
- </dependency>
- <dependency>
- <groupId>joda-time</groupId>
- <artifactId>joda-time</artifactId>
- <version>2.1</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- <version>1.7.6</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.6</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.1</version>
- </dependency>
- <dependency>
- <groupId>javax.annotation</groupId>
- <artifactId>javax.annotation-api</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.4</version>
- </dependency>
- </dependencies>
- </project>
【说明】:
在增加Activiti
的包时,可以参考Activiti
本身的依赖包的管理,当执行BaseTestCase
发现缺少包时,可以从该文件找到对应的依赖包及其版本的要求。
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161010suktxa66ht66xhx8.png)
3. 增加Spring中的Activiti配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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">
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/activiti" />
- <property name="username" value="root" />
- <property name="password" value="000" />
- <property name="defaultAutoCommit" value="false" />
- </bean>
- <bean id="processEngineConfiguration"
- class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
- <property name="dataSource" ref="dataSource" />
- <property name="databaseType" value="mysql"/>
- <property name="databaseSchemaUpdate" 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" />
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- </beans>
4. 编写TestCase
其代码如下:
- package com.hotent.test;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import org.springframework.test.context.transaction.TransactionConfiguration;
- import org.springframework.transaction.annotation.Transactional;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration({"classpath:spring-activiti.xml"})
- @TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
- @Transactional
- public class BaseTestCase extends AbstractTransactionalJUnit4SpringContextTests{
- @Test
- public void test(){
- System.out.println ("test");
- }
- }
执行以上TestCase,若控制台输出test则表示已经成功整合了Activiti与Spring,并且可以登录数据库中查看其已经创建好Activiti对应的表,如下图所示:
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161011lshysslawshskssw.png)
5. 发布测试的流程示例
- package com.hotent.test.bpm;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
- import javax.annotation.Resource;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.RuntimeService;
- import org.activiti.engine.TaskService;
- import org.activiti.engine.repository.Deployment;
- import org.activiti.engine.repository.ProcessDefinition;
- import org.activiti.engine.task.Task;
- import org.junit.Assert;
- import org.junit.Test;
- import com.hotent.test.BaseTestCase;
- public class BpmDefinitionTest extends BaseTestCase{
- @Resource
- private RepositoryService repositoryService;
- @Resource
- private RuntimeService runtimeService;
- @Resource
- TaskService taskService;
- @Test
- public void testDeploy() throws IOException{
- InputStream is=readXmlFile();
- Assert.assertNotNull(is);
- //发布流程
- Deployment deployment=repositoryService.createDeployment().addInputStream("bpmn20.xml", is).name("holidayRequest").deploy();
- Assert.assertNotNull(deployment);
- System.out.println("deployId:" + deployment.getId());
- //查询流程定义
- ProcessDefinition processDefinition=repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
- Long businessKey=new Double(1000000*Math.random()).longValue();
- //启动流程
- runtimeService.startProcessInstanceById(processDefinition.getId(),businessKey.toString());
- //查询任务实例
- List<Task> taskList=taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list();
- Assert.assertNotNull(taskList==null);
- Assert.assertTrue(taskList.size()>0);
- for(Task task:taskList){
- System.out.println("task name is " + task.getName() + " ,task key is " + task.getTaskDefinitionKey());
- }
- }
- public InputStream readXmlFile() throws IOException{
- String filePath="holidayRequest.bpmn";
- return Class.class.getClass().getResource("/"+filePath).openStream();
- }
- }
执行结果如下所示:
<ignore_js_op>
![](http://www.activiti.org.cn/data/attachment/forum/201404/28/161011b1004qursqht4h4t.png)