activiti整合spring

@author YHC

 首先描述一下版本:

activiti:Activiti5.10

mysql:5.5(Activiti有规定最低版本是5.1.11)

mysql驱动:mysql-connector-java-5.1.17-bin.jar

spring:3.1.2(也就是Activiti自带的例子中的jar包)


该示例主要是从Activiti官网的10分钟入门中提取的.

流程图:

activiti整合spring_第1张图片

对应的XML文件:(FinancialReportProcess.bpmn20.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<definitions id="definitions"
	targetNamespace="http://activiti.org/bpmn20" 
  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" 
	xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
	<!-- 流程信息 begin -->
	<process id="financialReport" name="Monthly financial report process">
	   
		<startEvent id="theStart" />
		
		<sequenceFlow id="flow1" sourceRef="theStart" targetRef="writeReportTask" />
		
		<userTask id="writeReportTask" name="Write monthly financial report" >
		  <documentation>
        Write monthly financial report for publication to shareholders.
      </documentation>
      <potentialOwner>
        <resourceAssignmentExpression>
          <formalExpression>accountancy</formalExpression>
        </resourceAssignmentExpression>
      </potentialOwner>
		</userTask>
		
		<sequenceFlow id="flow2" sourceRef="writeReportTask" targetRef="verifyReportTask" />
			
		<userTask id="verifyReportTask" name="Verify monthly financial report" >
		  <documentation>
        Verify monthly financial report composed by the accountancy department.
        This financial report is going to be sent to all the company shareholders.  
      </documentation>
      <potentialOwner>
        <resourceAssignmentExpression>
          <formalExpression>management</formalExpression>
        </resourceAssignmentExpression>
      </potentialOwner>
		</userTask>
		
		<sequenceFlow id="flow3" sourceRef="verifyReportTask" targetRef="theEnd" />
			
		<endEvent id="theEnd" />
			
	</process>
<!-- 流程信息 end -->

<!-- 下面的代码是Activiti绘制可视化流程图使用的 -->
	<bpmndi:BPMNDiagram>
		<bpmndi:BPMNPlane bpmnElement="financialReport">
			<bpmndi:BPMNShape bpmnElement="theStart">
				<omgdc:Bounds height="30.0" width="30.0" x="75.0" y="225.0" />
			</bpmndi:BPMNShape>
			<bpmndi:BPMNShape bpmnElement="writeReportTask">
				<omgdc:Bounds height="80.0" width="100.0" x="165.0" y="200.0" />
			</bpmndi:BPMNShape>
			<bpmndi:BPMNShape bpmnElement="verifyReportTask">
				<omgdc:Bounds height="80.0" width="100.0" x="330.0" y="200.0" />
			</bpmndi:BPMNShape>
			<bpmndi:BPMNShape bpmnElement="theEnd">
				<omgdc:Bounds height="28.0" width="28.0" x="480.0" y="226.0" />
			</bpmndi:BPMNShape>
			<bpmndi:BPMNEdge bpmnElement="flow1">
        <omgdi:waypoint x="105.0" y="240.0" />
        <omgdi:waypoint x="165.0" y="240.0" />
      </bpmndi:BPMNEdge>
			<bpmndi:BPMNEdge bpmnElement="flow2">
				<omgdi:waypoint x="265.0" y="240.0" />
				<omgdi:waypoint x="330.0" y="240.0" />
			</bpmndi:BPMNEdge>
			<bpmndi:BPMNEdge bpmnElement="flow3">
				<omgdi:waypoint x="430.0" y="240.0" />
				<omgdi:waypoint x="480.0" y="240.0" />
			</bpmndi:BPMNEdge>
		</bpmndi:BPMNPlane>
	</bpmndi:BPMNDiagram>
	
</definitions>


接下来看以下对应spring和数据库的配置文件:(activiti.cfg.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">

     <!-- mysql 数据源配置-->
	<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="root" />
		<property name="defaultAutoCommit" value="true" />
	</bean>
	
	
	<!-- sqlserver 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
		<property name="url" value="jdbc:jtds:sqlserver://192.168.10.104:1433/activiti" />
		<property name="username" value="sa" />
		<property name="password" value="xxxxx" />
		<property name="defaultAutoCommit" value="true" />
	</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="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" /> 
		<property name="jdbcDriver" value="com.mysql.jdbc.Driver" /> 
		<property name="jdbcUsername" value="root" />
		 <property name="jdbcPassword" value="root" /> 
		<property name="databaseType" value="mysql">
		</property> <property name="databaseSchemaUpdate" value="true" /> 
		-->
		<!--  sqlserver 对应数据源配置
		<property name="databaseType" value="mssql"></property>
		<property name="dataSource" ref="dataSource" />
		<property name="transactionManager" ref="transactionManager" />
		<property name="databaseSchemaUpdate" value="true" />
		<property name="jobExecutorActivate" value="false" />
		-->
		<!--mysql 对应数据源配置--> 
		<!-- databaseType 不写默认是h2,如果数据库不是h2那么这项是必须的 -->
		<property name="databaseType" value="mysql"></property>
		<property name="dataSource" ref="dataSource" />
		<property name="transactionManager" ref="transactionManager" />
		<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" />
		
	<!-- 使用注解添加事物 -->	
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	 <!-- 
	 <bean id="printer" class="org.activiti.test.Printer" />
	  -->
</beans>
所需jar包均是从Activiti提供的Demo中的activiti-spring-examples项目libs-runtime下获取;

activiti整合spring_第2张图片

在测试的时候没有写junit单元测试,而是直接写的main方法.

package org.activiti.test.financial;

import java.util.List;

import org.activiti.engine.HistoryService;
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.support.ClassPathXmlApplicationContext;

public class FinancialTest {
	public static void main(String[] args) {
		
		//在测试的时候没有使用junit
		 ClassPathXmlApplicationContext applicationContext = 
			    new ClassPathXmlApplicationContext("activiti.cfg.xml");
		 
		 FinancialTest t=new FinancialTest();
		 //step 1  部署
		 //t.deploymentInstance(applicationContext);
		 //step 2  启动流程实例
		 //t.startInstance(applicationContext);
         //step 3 查询用户任务 
		  //t.queryUserTask(applicationContext);
		 //step 4   分配任何给user 
		// t.claimTask(applicationContext);
		 //step 5    查询个人任务列表
		 // t.queryPersonalTaskList(applicationContext);
		 //step 6     完成任务
		// t.completePersonalTask(applicationContext);
		 //step 7   查询历史流程信息
		 //t.queryHistoryProcessInstance(applicationContext);
		 
	}
	
	
	//部署流程实例
	public void deploymentInstance(ClassPathXmlApplicationContext applicationContext){
		//获得repositoryService
		RepositoryService repositoryService = (RepositoryService) applicationContext.getBean("repositoryService");
		//从文件部署流程
		Deployment deployment = repositoryService.createDeployment()
		  .addClasspathResource("org/activiti/test/financial/FinancialReportProcess.bpmn20.xml")
		  .deploy();
		
	}
	
	//启动流程
	public void startInstance(ClassPathXmlApplicationContext applicationContext){
		//获得 runtimeservice对象
		RuntimeService runtimeService = (RuntimeService) applicationContext.getBean("runtimeService");
		//启动流程实例 ,注意这里的key是我们流程文件中的<process id="financialReport" ,id属性,在Activiti术语叫key
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("financialReport");
		
	}
	
	//查询组任务
	public void queryUserTask(ClassPathXmlApplicationContext applicationContext){
		//获得 TaskService 对象
		TaskService taskService = (TaskService) applicationContext.getBean("taskService");
	    
		//查询任务列表使用组名称
		List<Task> tasks02 = taskService.createTaskQuery().taskCandidateGroup("accountancy").list();
		for (Task task : tasks02) {
			System.out.println(task.getName());
		}
	}
	
	//分配任务给用户
	public void claimTask(ClassPathXmlApplicationContext applicationContext){
		//获得TaskService对象
		TaskService taskService = (TaskService) applicationContext.getBean("taskService");
		//得到组任务,也可以是 management组
		List<Task> tasks02 = taskService.createTaskQuery().taskCandidateGroup("accountancy").list();
		//任务分配之后这个任务就会从accountancy用户组的任务列表中消失
		for (Task task2 : tasks02) {
			taskService.claim(task2.getId(), "fozzie");
			System.out.println("任务名称:"+task2.getName());
		}
		
		//此时查询fozzie人的个人任务列表
		List<Task> tasks = taskService.createTaskQuery().taskAssignee("fozzie").list();
		for (Task t : tasks) {
			System.out.println("已受理任务名称:"+t.getName());
		}
		
	}
	
	//查询个人的任务列表
	public void queryPersonalTaskList(ClassPathXmlApplicationContext applicationContext){
		//获得TaskService对象
		TaskService taskService = (TaskService) applicationContext.getBean("taskService");
		//查询fozzie用户的个人任务列表
		List<Task> tasks = taskService.createTaskQuery().taskAssignee("fozzie").list();
		//输出受理任务信息
		for (Task t : tasks) {
			System.out.println("已受理任务名称:"+t.getName());
		}
	}
	
	//完成任务
	public void completePersonalTask(ClassPathXmlApplicationContext applicationContext){
		//获得TaskService对象
		TaskService taskService = (TaskService) applicationContext.getBean("taskService");
		
		//查询 fozzie用户个人任务列表并完成其任务
		List<Task> tasks = taskService.createTaskQuery().taskAssignee("fozzie").list();
		
		//完成用户任务
		for (Task task : tasks) {
			System.out.println("完成任务名称:"+task.getName());
			//通过任务id完成任务
			taskService.complete(task.getId());	
		}
		
	}
	
	//查询历史流程信息,也许你在查询的时候这个任务没有结束
	//那么请你将management组的任务claimTask分配给用户然后completePersonalTask完成任务
	//这个流程实例就算完成了,你在这里也才会查询出来,否则流程实例没有到达
	public void queryHistoryProcessInstance(ClassPathXmlApplicationContext applicationContext){
		//获取historyService
		HistoryService historyService = (HistoryService) applicationContext.getBean("historyService");
		//在这里需要注意的是,你的financialReport流程如果启动多个,singleResult将会出错
		//由于这里是测试我很清除这个实例只启动了一个,所以使用singleResult方法,如果你在测试时候需要注意
		HistoricProcessInstance historicProcessInstance = 
		historyService.createHistoricProcessInstanceQuery().processDefinitionKey("financialReport").singleResult();//processInstanceId("financialReport").singleResult();
		
		System.out.println("流程结束时间: " + historicProcessInstance.getEndTime());
	}
	
}


下面介绍一下Activiti提供的可视化测试的环境:

点击我下载[已配置Activiti]

点击到页面上之后下载这一项:


1.解压文件

2.启动h2数据库,如下图:

activiti整合spring_第3张图片

3.启动tomcat,如下图

activiti整合spring_第4张图片

4.打开浏览器输入:http://localhost:8080/activiti-explorer

5.使用下面给出账户登录,如下图

activiti整合spring_第5张图片

6.部署一个新的流程,如下图操作:(请按照A,B顺序操作)

activiti整合spring_第6张图片

7.选择文件,如下图:

activiti整合spring_第7张图片

7.选择以下文件上传,如下图:(在7.1处描述文件位置)


7.1文件位置,如下图:

activiti整合spring_第8张图片

8.退出切换用户fozzie/fozzie登录

activiti整合spring_第9张图片


9.打开流程,选择流程,

activiti整合spring_第10张图片

10.启动此流程,注意:是紧接着上一步的操作:

activiti整合spring_第11张图片

11.查看任务,如下图

activiti整合spring_第12张图片

12.接受任务,如下图:

activiti整合spring_第13张图片


13,查看并完成任务,如下图

activiti整合spring_第14张图片

14.切换用户,kermit/kermit登陆.

15.查看任务.,如下图:

activiti整合spring_第15张图片

16.接受任务.如下图:

activiti整合spring_第16张图片

17.完成任务:如下图

activiti整合spring_第17张图片

18.确认是否有当前执行流程.来确认流程是否执行完毕.

activiti整合spring_第18张图片


点击我下载示例代码









你可能感兴趣的:(spring,mysql,jdbc,report,sqlserver,documentation)