学习资料:《Activiti实战》
第七章 Activiti与容器集成
本章讲解activiti-spring可以做的事情,如何与现有系统集成,包含bean的注入、统一事务管理等。
7.1 流程引擎工厂
7.1.1 ProcessEngine
创建processEngine的方法有三种:
1 通过配置文件 2 测试中通过ActivitiRule 3 通过ProcessEngines类
7.1.2 ProcessEngineFactory
与spring集成的目的有两个:
1 通过spring统一管理ProcessEngine和七大Service的获取 2 业务与流程引擎的统一事务管理
(1)pom.xml
将activiti与spring集成,先要在pom.xml中增加依赖activiti-spring:(此处忽略了spring本身的依赖,请自行配置)
1 <!-- activit begin --> 2 <dependency> 3 <groupId>org.activiti</groupId> 4 <artifactId>activiti-engine</artifactId> 5 <version>5.16.3</version> 6 </dependency> 7 <dependency> 8 <groupId>org.activiti</groupId> 9 <artifactId>activiti-spring</artifactId> 10 <version>5.16.3</version> 11 </dependency> 12 <!-- activit end -->
(2)配置文件applicationContext-test.xml
applicationContext.xml分为几个部分:数据源、事务管理器、引擎配置、引擎工厂、获取service。(同样,spring自身的一些配置此处略过。)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 5 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 6 7 <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> 8 <property name="driverClass" value="org.h2.Driver" /> 9 <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" /> 10 <property name="username" value="sa" /> 11 <property name="password" value="" /> 12 </bean> 13 14 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 15 <property name="dataSource" ref="dataSource" /> 16 </bean> 17 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> 18 19 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> 20 <property name="dataSource" ref="dataSource" /> 21 <property name="transactionManager" ref="transactionManager" /> 22 <property name="databaseSchemaUpdate" value="true" /> 23 <property name="jobExecutorActivate" value="false" /> 24 </bean> 25 26 <bean id="processEngineFactory" class="org.activiti.spring.ProcessEngineFactoryBean"> 27 <property name="processEngineConfiguration" ref="processEngineConfiguration" /> 28 </bean> 29 30 <bean id="repositoryService" factory-bean="processEngineFactory" factory-method="getRepositoryService" /> 31 <bean id="runtimeService" factory-bean="processEngineFactory" factory-method="getRuntimeService" /> 32 <bean id="formService" factory-bean="processEngineFactory" factory-method="getFormService" /> 33 <bean id="identityService" factory-bean="processEngineFactory" factory-method="getIdentityService" /> 34 <bean id="taskService" factory-bean="processEngineFactory" factory-method="getTaskService" /> 35 <bean id="historyService" factory-bean="processEngineFactory" factory-method="getHistoryService" /> 36 <bean id="managementService" factory-bean="processEngineFactory" factory-method="getManagementService" /> 37 </beans>
(3)ProcessEngine和xxxService使用示例
在配置文件applicationContext-test.xml文件中加上一段:
1 <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 --> 2 <context:component-scan base-package="me.kafeitu.activiti"> 3 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 4 </context:component-scan>
然后在代码中使用:
1 @Autowired 2 RuntimeService runtimeService;
7.2 自动部署流程定义
(1)场景
概念:通过Spring创建 ProcessEngine 时一个独有的功能。可以在初始化 ProcessEngine 时自动将定义的资源部署到 ProcessEngine 中。
问题:每次启动都会加载spring配置文件,并且随之初始化ProcessEngine,那么是不是每次都会部署一遍呢?那么一旦重启部署版本就变成新的了?(activiti中有部署版本的参数)Activiti处理整个的办法是,只有流程数据库中没有和自动部署的流程定义相同的记录才会部署,也就是没有新的流程定义就不会再部署一遍。
优点:对新系统的上线或者开发过程中加入新的流程非常有用,可以自动部署引擎数据库中不存在的,或者修改过的流程定义。
(2)配置
在引擎工程的属性里加上一个部署资源的属性。
1 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> 2 <property name="dataSource" ref="dataSource" /> 3 <property name="transactionManager" ref="transactionManager" /> 4 <property name="databaseSchemaUpdate" value="true" /> 5 <property name="jobExecutorActivate" value="false" /> 6 <property name="deploymentResource" value="classpath*:/chapter6/**/*.bpmn"/> 7 </bean>
(3)代码测试
1 long count = repositoryService.createProcessDefinitionQuery().count(); 2 assertEquals(1,count);