Activiti入门第一章-构建流程引擎
2018-12-12 本文已影响2人
Blue_hr
下列示例将演示如何使用配置文件以及Spring来帮助我们获取Activiti的流程引擎ProcessEngine对象
首先构建一个Maven项目引入Spring的相关依赖,以及Activiti依赖(5.22和6.0在这儿没有区别)
pom.xml如下
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.8.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring依赖start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring依赖end -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!--activiti依赖start-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.22.0</version>
</dependency>
<!--activiti依赖end-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
项目环境搭建完成后可以新建Activiti的配置文件了 ,Activiti的核心功能7大Service接口全依赖于ProcessEngine获取,而ProcessEngine需要通过processEngineConfiguration的buildProcessEngine()方法获得,所以我们唯一需要关心的就是processEngineConfiguration这个Bean的相关配置
<?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.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/activiti5demo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--流程配置器-->
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!--流程引擎名称-->
<property name="processEngineName" value="MyProcessEngine"/>
<property name="dataSource" ref="dataSource"/>
<!--数据库表策略-->
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
- 使用Spring容器实例化流程引擎
@Test
public void testEngine() {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "activiti.cfg.xml" });
ProcessEngineConfiguration processEngineConfiguration = (ProcessEngineConfiguration) context.getBean("processEngineConfiguration");
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
Assert.assertEquals("MyProcessEngine", processEngine.getName());
RuntimeService runtimeService = processEngine.getRuntimeService();
Assert.assertNotNull(runtimeService);
TaskService taskService = processEngine.getTaskService();
Assert.assertNotNull(taskService);
}
- 使用Activiti配置类读取配置文件初始化流程引擎
@Test
public void testDefaltEngine(){
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
Assert.assertEquals("MyProcessEngine", processEngine.getName());
RuntimeService runtimeService = processEngine.getRuntimeService();
Assert.assertNotNull(runtimeService);
TaskService taskService = processEngine.getTaskService();
Assert.assertNotNull(taskService);
}
- activiti 最简单的方式获取ProcessEngine 是调用ProcessEngines.getDefaultProcessEngine()方法,它会自动去ClassPath查找activiti.cfg.xml文件,并按照配置文件创建流程引擎(如果你是按照我的配置来的,这里会报错)
@Test
public void getDefaultProcessEngine(){
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
Assert.assertNotNull(engine);
}
这里有个很有意思的坑,我测试了几遍都是获取不到ProcessEngine,同时教大家一个小技巧,因为我们配置的数据库策略databaseSchemaUpdate为 true 所以在引擎启动的时候一定会创建数据库表,所以在刚才的单元测试中我根据数据库判断流程引擎一定是启动的,点进源码可以看到getDefaultProcessEngine()是会将类路径下的所有配置文件都创建一次流程引擎,最后将这些引擎存入Map中Key为引擎名称,value为引擎对象。而getDefaultProcessEngine()只会从这个map中查找名为default的对象
public static final String NAME_DEFAULT = "default";
protected static Map<String, ProcessEngine> processEngines = new HashMap<String, ProcessEngine>();
/** Initializes all process engines that can be found on the classpath for
* resources <code>activiti.cfg.xml</code> (plain Activiti style configuration)
* and for resources <code>activiti-context.xml</code> (Spring style configuration). */
public synchronized static void init() {
if (!isInitialized()) {
if(processEngines == null) {
// Create new map to store process-engines if current map is null
processEngines = new HashMap<String, ProcessEngine>();
}
ClassLoader classLoader = ReflectUtil.getClassLoader();
Enumeration<URL> resources = null;
try {
resources = classLoader.getResources("activiti.cfg.xml");
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("problem retrieving activiti.cfg.xml resources on the classpath: "+System.getProperty("java.class.path"), e);
}
// Remove duplicated configuration URL's using set. Some classloaders may return identical URL's twice, causing duplicate startups
Set<URL> configUrls = new HashSet<URL>();
while (resources.hasMoreElements()) {
configUrls.add( resources.nextElement() );
}
for (Iterator<URL> iterator = configUrls.iterator(); iterator.hasNext();) {
URL resource = iterator.next();
log.info("Initializing process engine using configuration '{}'", resource.toString());
initProcessEnginFromResource(resource);
} 略...
所以这里我们只需要将配置文件中的流程名称配置删除就能正常获取流程引擎了
使用Activiti配置类读取配置文件方式还有以下几种,不过原理都是差不多的,了解即可
//使用IO流读取配置
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("activiti.cfg.xml");
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(stream);
//默认配置
configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
//指定文件名
configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");