activiti工作流3-引擎配置
1 流程引擎配置
配置测试
测试1:
//1. 方式1配置
@Test
public void test1(){
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
logger.info("{}",configuration);
//org.act[email protected]eb21112
}
测试2:
@Test
public void test2(){
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
logger.info("{}",configuration);
}
//报错:java.io.FileNotFoundException:
//class path resource [activiti.cfg.xml] cannot be opened because it does not exist
需要默认的activiti.cfg.xml
文件。
该方法的源码为:
public static ProcessEngineConfiguration createProcessEngineConfigurationFromResourceDefault() {
return createProcessEngineConfigurationFromResource("activiti.cfg.xml", "processEngineConfiguration");
}
需要放置resource下才行,并且配置中的bean
为"processEngineConfiguration"
。activiti.cfg.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" />
<property name="jdbcDriver" value="org.h2.Driver" />
<property name="jdbcUsername" value="sa"></property>
<property name="jdbcPassword" value=""></property>
<!-- databaseSchemaUpdate 配置流程引擎启动/关闭时对数据库Schema的策略.取值有:
false:默认子,检查DBSchema版本与Activiti开发包是否一致
true:更新数据库中标信息,如果不存在则创建,推荐使用
create-drop 启动时创建表 关闭时删除表-->
<property name="databaseSchemaUpdate" value="true"></property>
</bean>
</beans>
添加后启动ok。
数据库配置
- activiti默认使用H2内存数据库
- 配置jdbc属性,使用mybatis连接池
- 配置dataSource属性,可选第三方实现
- 配置jdbc使用mybatis连接池
- druid数据源连接池
- activiti支持的数据库类型
- 配置databaseSchemaUpdate
databaseSchemaUpdate 配置流程引擎启动/关闭时对数据库Schema的策略.取值有:
- false:默认,检查DBSchema版本与Activiti开发包是否一致。不一致抛异常
- true:更新数据库中标信息,如果不存在则创建,推荐使用
- create-drop 启动时创建表 关闭时删除表
使用mysql数据库
说明:jdbc或druid 都有自己的属性名。
1. 使用jdbc连接mysql
添加mysql依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
activiti.cfg.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:mysql://localhost:3306/activiti" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root"></property>
<property name="jdbcPassword" value="whb"></property>
<property name="databaseSchemaUpdate" value="true"></property>
</bean>
</beans>
测试代码:
@Test
public void test2(){
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
logger.info("{}",configuration); //ok
ProcessEngine processEngine = configuration.buildProcessEngine();
logger.info("获取流程引擎 {}",processEngine.getName());
processEngine.close();
}
2. 使用druid连接mysql
添加依赖:
<!--mysql依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!--mysql依赖-->
activiti_druid_cfg.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">
<!--流程引擎配置-->
<!-- databaseSchemaUpdate 配置流程引擎启动/关闭时对数据库Schema的策略.取值有:
false:默认,检查DBSchema版本与Activiti开发包是否一致。不一致抛异常
true:更新数据库中标信息,如果不存在则创建,推荐使用
create-drop 启动时创建表 关闭时删除表-->
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="databaseSchemaUpdate" value="true"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/activiti?characterEncoding=UTF-8&allowMultiQueries=true" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="whb" />
<property name="initialSize" value="1"></property>
<property name="maxActive" value="10" />
<property name="filters" value="stat,slf4j" />
</bean>
</beans>
从activiti_druid_cfg.xml
中读取数据库配置
//3. 方式3配置(druid)
@Test
public void test3(){
ProcessEngineConfiguration configuration = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("activiti_druid_cfg.xml");
logger.info("{}",configuration); //ok
ProcessEngine processEngine = configuration.buildProcessEngine();
logger.info("获取流程引擎 {}",processEngine.getName());
processEngine.close();
}
其他的一些配置
历史数据、身份校验等配置。
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="databaseSchemaUpdate" value="true"></property>
<property name="dataSource" ref="dataSource" />
<!--其他配置-->
<property name="dbHistoryUsed" value="true" /> <!--是否使用历史数据(流程结束后)-->
<property name="dbIdentityUsed" value="false" /> <!--是否使用自带的身份验证-->
</bean>
数据库连接源码
源码阅读:buildProcessEngine()
方法中会一系列初始化:
public ProcessEngine buildProcessEngine() {
init();
return new ProcessEngineImpl(this);
}
// init /////////////////////////////////////////////////////////////////////
protected void init() {
initConfigurators();
configuratorsBeforeInit();
initProcessDiagramGenerator();
initHistoryLevel();
initExpressionManager();
initDataSource();
initVariableTypes();
initBeans();
initFormEngines();
initFormTypes();
initScriptingEngines();
initClock();
initBusinessCalendarManager();
initCommandContextFactory();
initTransactionContextFactory();
initCommandExecutors();
initServices();
initIdGenerator();
initDeployers();
initJobHandlers();
initJobExecutor();
initAsyncExecutor();
initTransactionFactory();
initSqlSessionFactory();
initSessionFactories();
initJpa();
initDelegateInterceptor();
initEventHandlers();
initFailedJobCommandFactory();
initEventDispatcher();
initProcessValidator();
initDatabaseEventLogging();
configuratorsAfterInit();
}
数据源初始化如下:
protected DataSource dataSource;
protected void initDataSource() {
if (dataSource==null) {
if (dataSourceJndiName!=null) {
try {
dataSource = (DataSource) new InitialContext().lookup(dataSourceJndiName);
} catch (Exception e) {
throw new ActivitiException("couldn't lookup datasource from "+dataSourceJndiName+": "+e.getMessage(), e);
}
} else if (jdbcUrl!=null) {
if ( (jdbcDriver==null) || (jdbcUrl==null) || (jdbcUsername==null) ) {
throw new ActivitiException("DataSource or JDBC properties have to be specified in a process engine configuration");
}
log.debug("initializing datasource to db: {}", jdbcUrl);
PooledDataSource pooledDataSource =
new PooledDataSource(ReflectUtil.getClassLoader(), jdbcDriver, jdbcUrl, jdbcUsername, jdbcPassword );
if (jdbcMaxActiveConnections > 0) {
pooledDataSource.setPoolMaximumActiveConnections(jdbcMaxActiveConnections);
}
if (jdbcMaxIdleConnections > 0) {
pooledDataSource.setPoolMaximumIdleConnections(jdbcMaxIdleConnections);
}
if (jdbcMaxCheckoutTime > 0) {
pooledDataSource.setPoolMaximumCheckoutTime(jdbcMaxCheckoutTime);
}
if (jdbcMaxWaitTime > 0) {
pooledDataSource.setPoolTimeToWait(jdbcMaxWaitTime);
}
if (jdbcPingEnabled == true) {
pooledDataSource.setPoolPingEnabled(true);
if (jdbcPingQuery != null) {
pooledDataSource.setPoolPingQuery(jdbcPingQuery);
}
pooledDataSource.setPoolPingConnectionsNotUsedFor(jdbcPingConnectionNotUsedFor);
}
if (jdbcDefaultTransactionIsolationLevel > 0) {
pooledDataSource.setDefaultTransactionIsolationLevel(jdbcDefaultTransactionIsolationLevel);
}
dataSource = pooledDataSource;
}
if (dataSource instanceof PooledDataSource) {
// ACT-233: connection pool of Ibatis is not properely initialized if this is not called!
((PooledDataSource)dataSource).forceCloseAll();
}
}
if (databaseType == null) {
initDatabaseType();
}
}