Activiti系列02:获取ProcessEngine的三种方

2019-12-04  本文已影响0人  我问你瓜保熟吗

Activiti系列01:与SpringBoot2.2.2结合

Activiti系列02:获取ProcessEngine的三种方式

一、使用H2数据库

ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
//  .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
//  .setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
//  .setAsyncExecutorActivate(false)
  .buildProcessEngine();

二、通过ProcessEngineConfiguration获取Mysql数据库

package cn.lonecloud.mavenActivi;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test;
/**
 * 通过使用ProcessEngineConfiguration获取
 * @Title: ConfigByClass.java
 * @Package cn.lonecloud.mavenActivi
 * @Description: 
 * @author lonecloud
 * @date 2016年8月22日 下午10:50:33
 */
public class ConfigByClass {
    @Test
    public void config() {
        //获取config对象
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
                .createStandaloneProcessEngineConfiguration();
        //Jdbc设置
        String jdbcDriver = "com.mysql.jdbc.Driver";
        processEngineConfiguration.setJdbcDriver(jdbcDriver);
        String jdbcUrl = "jdbc:mysql://localhost:3306/mavenActiviti?useUnicode=true&characterEncoding=utf-8";
        processEngineConfiguration.setJdbcUrl(jdbcUrl);
        String jdbcUsername = "root";
        processEngineConfiguration.setJdbcUsername(jdbcUsername);
        String jdbcPassword = "123456";
        processEngineConfiguration.setJdbcPassword(jdbcPassword);
        /**
         * Checks the version of the DB schema against the library when the
         * process engine is being created and throws an exception if the
         * versions don't match.
         */
//        public static final String DB_SCHEMA_UPDATE_FALSE = "false";//不自动创建新表

        /**
         * Creates the schema when the process engine is being created and drops
         * the schema when the process engine is being closed.
         */
//        public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//每次运行创建新表

        /**
         * Upon building of the process engine, a check is performed and an
         * update of the schema is performed if it is necessary.
         */
//        public static final String DB_SCHEMA_UPDATE_TRUE = "true";设置自动对表结构进行改进和升级
        //设置是否自动更新
        processEngineConfiguration
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        //获取引擎对象
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        processEngine.close();
    }
}

三、通过ProcessEngineConfiguration载入xml文件

package cn.lonecloud.mavenActivi;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.junit.Test;
/**
 * 通过通过配置文件进行初始化获取引擎对
 * @Title: ConfigByConfig.java
 * @Package cn.lonecloud.mavenActivi
 * @Description: 
 * @author lonecloud
 * @date 2016年8月22日 下午10:40:35
 */
public class ConfigByConfig {
    /**
     * 通过配置文件进行初始化获取引擎对象
     * @Description:
     */
    @Test
    public void configByConf(){
        //载入资源
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //创建引擎
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        processEngine.getRepositoryService();
    }
 }
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!--数据库连接信息 -->
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <property name="jdbcUsername" value="root"></property>
        <property name="jdbcPassword" value="123456"></property>
        <!-- 是否生成表结构 -->
        <property name="databaseSchemaUpdate" value="true"></property>
    </bean>
    
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <!-- 注入自动建表设置 -->
        <property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
    </bean>
</beans>

四、通过默认载入activiti.cfg.xml进行获取

// 通过默认载入activiti.cfg.xml进行获取,这里的xml文件名必须设置为activiti.cfg.xml
// 推荐使用

    @Test
    public void configByDefault(){
        //通过获取载入默认获取
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.close();
    }

参考:https://www.cnblogs.com/lonecloud/p/5797532.html

上一篇下一篇

猜你喜欢

热点阅读