一、流程引擎
ProcessEnigne引擎实例可以获取到的Service
ProcessEnigneConfiguration【流程引擎配置】
- 查找并解析xml配置文件activiti.cfg.xml
- 提供多个静态方法创建配置对象
- 实现几个基于不同场景的子类,配置方式非常灵活
ProcessEngineConfiguration 对象使用 Spring 框架的 DefaultListableBeanFactory 作为 BeanFactory
activiti.cfg.xml配置文件
xml<?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="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration"> <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000;MVCC=TRUE" /> <property name="jdbcDriver" value="org.h2.Driver" /> <property name="jdbcUsername" value="sa" /> <property name="jdbcPassword" value="" /> <property name="databaseSchemaUpdate" value="create-up"/> </bean> </beans>
提供多个静态方法创建引擎对象
java// 根据加载默认Resource目录下的资源文件activiti.cfg.xml,创建流程引擎 createProcessEngineConfigurationFromResourceDefault() // 根据指定路径下的资源文件,创建流程引擎 createProcessEngineConfigurationFromResource(String) // 根据指定路径下的资源文件和注入的配置类名称,创建流程引擎 createProcessEngineConfigurationFromResource(String, String) // 根据文件流读取资源文件,创建流程引擎 createProcessEngineConfigurationFromInputStream(InputStream) // 根据文件流读取资源文件和注入的配置类名称,创建流程引擎 createProcessEngineConfigurationFromInputStream(InputStream, String) // 创建基于内存数据库的流程引擎【h2内存数据库的配置需要手动设置】 createStandaloneProcessEngineConfiguration() // 创建基于内存数据库的流程引擎【h2内存数据库的配置已经默认】 createStandaloneInMemProcessEngineConfiguration()
同时也可以通过ProcessEngines这个抽象类构建引擎实例
多个子类适配不同场景
依赖
xml<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>6.0.0</version> </dependency>
比较重要的相关类
- ProcessEngineConfigurationImpl
- StandaloneProcessEngineConfiguration
- SpringProcessEngineConfiguration【在activiti-engine是没有该类的,只有activiti-spring才会有该类】
二、具体测试代码
java
package com.laogoubi.config;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
/**
* @ClassName ConfigTest
* @Description 创建流程引擎
* @Author eastern
* @Date 2020/6/27 下午9:56
* @Version 1.0
**/
public class ConfigTest {
private static final Logger logger = LoggerFactory.getLogger(ConfigTest.class);
/**
* 读取默认的配置文件
*/
@Test
public void testConfig4FromResourceDefault(){
// 读取src/main/resources/activiti.cfg.xml
ProcessEngineConfiguration processEngineConfiguration =
ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
logger.info("processEngineConfiguration = {}", processEngineConfiguration);
// 获取流程引擎对象
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
logger.info("processEngine = {}", ToStringBuilder.reflectionToString(processEngine, ToStringStyle.JSON_STYLE));
}
/**
* 读取指定的配置文件
*/
@Test
public void testConfig4FromResource(){
// 这边的activiti.cfg.xml可以是自定义文件路径
ProcessEngineConfiguration processEngineConfiguration =
ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
logger.info("processEngineConfiguration = {}", processEngineConfiguration);
// 获取流程引擎对象
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
logger.info("processEngine = {}", ToStringBuilder.reflectionToString(processEngine, ToStringStyle.JSON_STYLE));
}
/**
* 该方法需要提供两个参数来创建 ProcessEngineConfiguration:
* 第一个参数为 Activiti 配置文件的位置;
* 第二个参数为创建 bean 的名称
* 如果找不到名称为“test”的bean ,则抛出 NoSuchBeanDefinitionException
*/
@Test
public void testConfig4FromResourceAbeanName(){
// 这边的activiti.cfg.xml可以是自定义文件路径
ProcessEngineConfiguration processEngineConfiguration =
ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml", "test");
logger.info("processEngineConfiguration = {}", processEngineConfiguration);
// 获取流程引擎对象
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
logger.info("processEngine = {}", ToStringBuilder.reflectionToString(processEngine, ToStringStyle.JSON_STYLE));
}
/**
* 读取输入流配置
*/
@Test
public void testConfig4FrominputStream() throws FileNotFoundException {
// 读取文件
File file = new File("activiti.cfg.xml");
// 得到文件输入流
InputStream inputStream = new FileInputStream(file);
// 通过文件流创建
ProcessEngineConfiguration processEngineConfiguration =
ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream);
logger.info("processEngineConfiguration = {}", processEngineConfiguration);
// 获取流程引擎对象
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
logger.info("processEngine = {}", ToStringBuilder.reflectionToString(processEngine, ToStringStyle.JSON_STYLE));
}
/**
* 使用该方法创建 ProcessEngineConfiguration,
* 并不会读取任何 Activiti 配置文件,
* 这意味着流程引擎配置的全部属性都会使用默认值。
* 与其他子类不一样的是,创建的 StandalonelnMemProcessEngineConfiguration 实例,
* 只特别指定了 databaseSchemaUpdate 属性 和 jdbcUrl 属性,分别为create-drop 和 jdbc:h2:mem:activiti
* StandalonelnMemProcessEngineConfiguration 类是 StandaloneProcessEngineConfiguration 类的子类
*/
@Test
public void testConfig4eStandaloneInMem(){
ProcessEngineConfiguration processEngineConfiguration =
ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
logger.info("processEngineConfiguration = {}", processEngineConfiguration);
// 获取流程引擎对象
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
logger.info("processEngine = {}", processEngine);
}
/**
* 直接返回StandaloneProcessEngineConfiguration 实例
* new StandaloneProcessEngineConfiguration();
* databaseSchemaUpdate 属性 和 jdbcUrl 属性,分别为false 和 jdbc:h2:tcp://localhost/~/activiti
*/
@Test
public void testConfig4Standalone(){
ProcessEngineConfiguration processEngineConfiguration =
ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
logger.info("processEngineConfiguration = {}", ToStringBuilder.reflectionToString(processEngineConfiguration, ToStringStyle.JSON_STYLE));
// 获取流程引擎对象
ProcessEngine processEngine = processEngineConfiguration
.setDatabaseSchemaUpdate(ProcessEngineConfiguration
.DB_SCHEMA_UPDATE_TRUE)
.setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
.buildProcessEngine();
logger.info("processEngine = {}", processEngine);
}
/**
* 通过ProcessEngines获取实例
*/
@Test
public void testConfig4ProcessEngines(){
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
logger.info("processEngine = {}", processEngine);
}
}