这篇文章将为大家详细讲解有关Activiti Spring集成的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
Activiti版本:5.17.0
Eclipse版本:Kepler Service Release 2
JDK版本:1.7.0_51
Mysql版本:5.5.33
在Eclipse中集成了 Activiti Eclipse Designer 插件后(见《Eclipse 集成Activiti Eclipse Designer 插件》),
File->New->Other->Activiti->Activiti Project
Project Name为activiti-demo
创建项目后,默认是这样的
已经是maven结构,但是还需要将项目转换成maven项目
在项目上右键->Configure->Conver to Maven Project,见下图
转换完成后,编辑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>
目录结构如下
至此,准备工作到此结束
在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>
各个Activiti的主要bean的作用如下:
processEngineConfiguration Activiti流程引擎
processEngine Activiti的主接口
repositoryService Activiti的资源操作接口
runtimeService 运行时操作接口(大多操作流程实例)
taskService 任务操作接口
identityService 身份操作接口(用户和用户组)
historyService 历史查询接口
formService 表单数据绑定接口
managementService 管理接口
在src/main/resources/diagrams/下创建一个工作流,命名为DemoProcess.bpmn,如下图,利用Activiti插件新建流程图
此时进入Activiti Diagram Editor,在空白处点一下,下边属性栏中设置id和name,id就是这个流程的key,后边启动流程的时候使用到。
在图中单机FirstTask,设置其ID为firstTask,同样方法设置SecondTask的ID为secondTask
设置完毕后,此工作流以xml方式打开是这样的
<?xml version="1.0">
至此,流程设计完毕
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集成的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。