论坛首页 Java企业应用论坛

Activiti入门篇--Spring 与Activiti的入门整合

浏览 29202 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-04-28  

一、概述

Activiti相对Jbpm来说,与Spring整合更加完美,具体可见本文的详细介绍

二、开发环境的准备

1.     Maven的环境任务,请参考第一篇 (Activiti入门篇—Maven的环境准备)
2.     Activiti的Eclipse插件安装
              插件更新地址:http://activiti.org/designer/update/
  安装Activiti设计插件
安装完成后,可以设计一请假的流程,如下所示:
        设计Activiti的请假流程
右键holidayRequest.bpmn文件,用Xml Editor打开,可以看到其生成的XML文件格式:
请假流程设计的XML文件
3.     安装数据库Mysql(略)
创建一数据库名为activiti,暂时不放表


三、ActivitiSpring的整合
1.  新建Maven项目

新建Maven项目

2.  加入依赖包

              
依赖的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发现缺少包时,可以从该文件找到对应的依赖包及其版本的要求。

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对应的表,如下图所示:

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();
	}
}

 

执行结果如下所示:
 

四、示例配置项目具体示例文件如:

 

 项目示例代码

  • 大小: 29.6 KB
  • 大小: 49.1 KB
  • 大小: 57.6 KB
  • 大小: 27.2 KB
  • 大小: 21 KB
  • 大小: 22.8 KB
  • 大小: 49.6 KB
  • 大小: 62 KB
  • 大小: 14.9 KB
  • 大小: 17.6 KB
   发表时间:2014-05-01  
呵呵,activiti名字有点绕,但是确实不错。
0 请登录后投票
   发表时间:2014-05-05  
头像有点吓人
1 请登录后投票
   发表时间:2014-10-14  
很不错的入门教程,就是有些细节没有解释清楚,还是感谢楼主了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics