Activiti Spring集成Demo

1.版本

Activiti版本:5.17.0

Eclipse版本:Kepler Service Release 2

JDK版本:1.7.0_51

Mysql版本:5.5.33

2.创建Activiti工程

在Eclipse中集成了   Activiti Eclipse Designer 插件后(见《Eclipse 集成Activiti Eclipse Designer 插件》),

File->New->Other->Activiti->Activiti Project

Project Name为activiti-demo

创建项目后,默认是这样的

Activiti Spring集成Demo_第1张图片

已经是maven结构,但是还需要将项目转换成maven项目

在项目上右键->Configure->Conver to Maven Project,见下图

Activiti Spring集成Demo_第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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.activiti.examples</groupId>
  <artifactId>activiti-examples</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>BPMN 2.0 with Activiti - Examples</name>
  <properties>
    <activiti-version>5.17.0</activiti-version>
    <spring-version>3.2.4.RELEASE</spring-version>
    <mysql-version>5.1.29</mysql-version>
  </properties>
  <dependencies>
  	<!-- junit -->
  	<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
  
  	<!-- spring -->
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-context</artifactId>
	  <version>${spring-version}</version>
	</dependency>
	
	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-jdbc</artifactId>
	  <version>${spring-version}</version>
	</dependency>
	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>${spring-version}</version>
	</dependency>
	
  	<!-- activiti -->
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-engine</artifactId>
      <version>${activiti-version}</version>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-spring</artifactId>
      <version>${activiti-version}</version>
    </dependency>
    
    <!-- Mysql -->
    <dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>${mysql-version}</version>
	</dependency>
	
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.2</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>1.7.2</version>
    </dependency>
    
  </dependencies>
</project>

目录结构如下

Activiti Spring集成Demo_第3张图片

至此,准备工作到此结束

3.开始第一个Demo

3.1 spring与activiti集成

在src/main/resources下添加Spring的配置文件,spring-activiti.xml如下


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
 
  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
	<property name="dataSource" ref="dataSource" />
	<property name="transactionManager" ref="transactionManager" />
	<!-- 
	databaseSchemaUpdate: 设置流程引擎启动和关闭时如何处理数据库表。
		false(默认):检查数据库表的版本和依赖库的版本, 如果版本不匹配就抛出异常。
		true: 构建流程引擎时,执行检查,如果需要就执行更新。 如果表不存在,就创建。
		create-drop: 构建流程引擎时创建数据库表, 关闭流程引擎时删除这些表。
	-->
   	<property name="databaseSchemaUpdate" value="true" />
   	<property name="jobExecutorActivate" value="false" />
   	<property name="asyncExecutorEnabled" value="true" />
   	<property name="asyncExecutorActivate" value="false" />
   	<property name="mailServerHost" value="mail.my-corp.com" />
   	<property name="mailServerPort" value="5025" />
  </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="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
  
  
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
	  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
	  <property name="url" value="jdbc:mysql://192.168.150.63:3306/activiti?autoReconnect=true" />
	  <property name="username" value="root" />
	  <property name="password" value="root111111" />
	  <!--  
	  <property name="defaultAutoCommit" value="false" />
	  -->
  </bean>
  
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
    
 
</beans>

3.2 Activiti的主要接口

各个Activiti的主要bean的作用如下:

processEngineConfiguration    Activiti流程引擎

processEngine    Activiti的主接口

repositoryService    Activiti的资源操作接口

runtimeService    运行时操作接口(大多操作流程实例)

taskService    任务操作接口

identityService    身份操作接口(用户和用户组)

historyService    历史查询接口

formService    表单数据绑定接口

managementService    管理接口

3.3 设计工作流程DemoProcess

在src/main/resources/diagrams/下创建一个工作流,命名为DemoProcess.bpmn,如下图,利用Activiti插件新建流程图

Activiti Spring集成Demo_第4张图片

此时进入Activiti Diagram Editor,在空白处点一下,下边属性栏中设置idnameid就是这个流程的key,后边启动流程的时候使用到。

Activiti Spring集成Demo_第5张图片

在图中单机FirstTask,设置其ID为firstTask,同样方法设置SecondTask的ID为secondTask 

设置完毕后,此工作流以xml方式打开是这样的


<?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:xsd="http://www.w3.org/2001/XMLSchema" 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="DemoProcess" name="Demo process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="firstTask" name="FirtsTask"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="firstTask"></sequenceFlow>
    <userTask id="secondTask" name="SecondTask"></userTask>
    <sequenceFlow id="flow2" sourceRef="firstTask" targetRef="secondTask"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow3" sourceRef="secondTask" targetRef="endevent1"></sequenceFlow>
  </process>
</definitions>

至此,流程设计完毕

3.4 创建测试类DemoProcess.java

package com.lei.demo.activiti.spring.test;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoProcess {
	
	private static ApplicationContext context;

	public static void main(String[] args) {
		
		System.out.println( "start" );
		
		context = new ClassPathXmlApplicationContext("spring-activiti.xml");
		
		//创建Activiti流程引擎,Activiti主接口
		ProcessEngine processEngine = (ProcessEngine) context.getBean("processEngine");
		System.out.println("processEngine Name:"+processEngine.getName());
		
		// 获得Activiti的资源操作接口 
        RepositoryService repositoryService = processEngine.getRepositoryService();
        
        //获得运行时操作接口(大多操作流程实例)
        RuntimeService runtimeService = processEngine.getRuntimeService();
        
        //部署流程定义,部署后,activiti会根据数据库配置自动创建表,添加数据
      	Deployment deployment = repositoryService
      							.createDeployment()
      							.addClasspathResource("diagrams/DemoProcess.bpmn")
      							.deploy();
      	System.out.println("deployment ID:"+deployment.getId()+" deployment Name:"+deployment.getName());
      	
      	//启动流程实例 ,startProcessInstanceByKey参数DemoProcess即DemoProcess.bpmn中的ID
        ProcessInstance instance = processEngine  
                 .getRuntimeService().startProcessInstanceByKey("DemoProcess");  
        String procId = instance.getId();  
        System.out.println("procId:"+ procId);
        
        //获得第一个任务 ,taskDefinitionKey参数为流程途中UserTask的ID
        TaskService taskService = processEngine.getTaskService();  
        List<Task> tasks = taskService.createTaskQuery().taskDefinitionKey("firstTask").list();  
        for (Task task : tasks) {  
            System.out.println("Task信息: taskID -" +task.getId()+" taskName -"+ task.getName());  
            // 认领任务  
            taskService.claim(task.getId(), "testUser");
        }
        
        //完成任务前,输出testUser下的任务数量
        System.out.println("完成任务前,testUser认领的task数量: " 
                + taskService.createTaskQuery().taskAssignee("testUser").count());
        //查看testUser 现在是否能够获取到该任务  
        tasks = taskService.createTaskQuery().taskAssignee("testUser").list();
        for (Task task : tasks) {  
            System.out.println("Task for testUser: " + task.getName());  
            // 完成任务  
            taskService.complete(task.getId());  
        }
        //完成任务后,输出testUser下的任务数量
        System.out.println("完成任务后,testUser认领的task数量: " 
                + taskService.createTaskQuery().taskAssignee("testUser").count());  
          
  
        // 获取并认领第二个任务  
        tasks = taskService.createTaskQuery().taskDefinitionKey("secondTask").list();  
        for (Task task : tasks) {  
            System.out.println("Task信息: taskID -" +task.getId()+" taskName -"+ task.getName());  
            taskService.claim(task.getId(), "testUser");  
        }  
          
        //完成第二个任务结束结束流程  
        for (Task task : tasks) {  
            taskService.complete(task.getId());  
        }
        
     // 核实流程是否结束  
        HistoryService historyService = processEngine.getHistoryService();  
        HistoricProcessInstance historicProcessInstance = historyService
        													.createHistoricProcessInstanceQuery()
        													.processInstanceId(procId)
        													.singleResult();
        System.out.println("流程实例结束时间: " + historicProcessInstance.getEndTime());
		
		System.out.println( "end" );

	}

}
运行结果截图

Activiti Spring集成Demo_第6张图片

演示项目已经上传到Git@OSC 点此下载

4 友情赞助

如果您喜欢此文,感觉对您工作有帮助,预期领导会给您涨工资,不妨小额赞助一下,让我有动力继续努力。

别忘了留言哦!留言时说明“oschina博文赞助”并且留下您的大名,您可以在《赞助友人》里查询到

赞助方式1:如果您有支付宝,打开支付宝App,使用“扫一扫”付款,付款码见下图

Activiti Spring集成Demo_第7张图片

赞助方式2: 如果您有微信钱包,请打开微信,使用“扫一扫”付款,付款码见下图

Activiti Spring集成Demo_第8张图片




你可能感兴趣的:(spring,mysql,Activiti)