Activiti獲取ProcessEngine的三種方法

lonecloud發表於2016-08-22

1.通過ProcessEngineConfiguration獲取

 1 package cn.lonecloud.mavenActivi;
 2 
 3 import org.activiti.engine.ProcessEngine;
 4 import org.activiti.engine.ProcessEngineConfiguration;
 5 import org.junit.Test;
 6 /**
 7  * 通過使用ProcessEngineConfiguration獲取
 8  * @Title: ConfigByClass.java
 9  * @Package cn.lonecloud.mavenActivi
10  * @Description: 
11  * @author lonecloud
12  * @date 2016年8月22日 下午10:50:33
13  */
14 public class ConfigByClass {
15     @Test
16     public void config() {
17         //獲取config物件
18         ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
19                 .createStandaloneProcessEngineConfiguration();
20         //Jdbc設定
21         String jdbcDriver = "com.mysql.jdbc.Driver";
22         processEngineConfiguration.setJdbcDriver(jdbcDriver);
23         String jdbcUrl = "jdbc:mysql://localhost:3306/mavenActiviti?useUnicode=true&characterEncoding=utf-8";
24         processEngineConfiguration.setJdbcUrl(jdbcUrl);
25         String jdbcUsername = "root";
26         processEngineConfiguration.setJdbcUsername(jdbcUsername);
27         String jdbcPassword = "123456";
28         processEngineConfiguration.setJdbcPassword(jdbcPassword);
29         /**
30          * Checks the version of the DB schema against the library when the
31          * process engine is being created and throws an exception if the
32          * versions don't match.
33          */
34 //        public static final String DB_SCHEMA_UPDATE_FALSE = "false";//不自動建立新表
35 
36         /**
37          * Creates the schema when the process engine is being created and drops
38          * the schema when the process engine is being closed.
39          */
40 //        public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//每次執行建立新表
41 
42         /**
43          * Upon building of the process engine, a check is performed and an
44          * update of the schema is performed if it is necessary.
45          */
46 //        public static final String DB_SCHEMA_UPDATE_TRUE = "true";設定自動對錶結構進行改進和升級
47         //設定是否自動更新
48         processEngineConfiguration
49                 .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
50         //獲取引擎物件
51         ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
52         processEngine.close();
53     }
54 }

2.通過ProcessEngineConfiguration載入xml檔案

xml檔案:

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 3     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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
 6 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 7     <!--這裡的類太多別導錯了 -->
 8     <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
 9         <!-- 配置流程引擎配置物件 -->
10         <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
11         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&amp;characterEncoding=utf-8"></property>
12         <property name="jdbcUsername" value="root"></property>
13         <property name="jdbcPassword" value="123456"></property>
14         <!-- 注入資料來源資訊 -->
15         <property name="databaseSchemaUpdate" value="true"></property>
16     </bean>
17     
18     <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
19         <!-- 注入自動建表設定 -->
20         <property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
21     </bean>
22 </beans>

java

 1 package cn.lonecloud.mavenActivi;
 2 
 3 import org.activiti.engine.ProcessEngine;
 4 import org.activiti.engine.ProcessEngineConfiguration;
 5 import org.activiti.engine.ProcessEngines;
 6 import org.junit.Test;
 7 /**
 8  * 通過通過配置檔案進行初始化獲取引擎對
 9  * @Title: ConfigByConfig.java
10  * @Package cn.lonecloud.mavenActivi
11  * @Description: 
12  * @author lonecloud
13  * @date 2016年8月22日 下午10:40:35
14  */
15 public class ConfigByConfig {
16     /**
17      * 通過配置檔案進行初始化獲取引擎物件
18      * @Description:
19      */
20     @Test
21     public void configByConf(){
22         //載入資源
23         ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
24         //建立引擎
25         ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
26         processEngine.getRepositoryService();
27     }
28  }

3.通過預設載入activiti.cfg.xml進行獲取

1      * 通過預設載入activiti.cfg.xml進行獲取
2      * @Description:推薦使用
3      */
4     @Test
5     public void configByDefault(){
6         //通過獲取載入預設獲取
7         ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
8         processEngine.close();
9     }

這裡的xml檔名必須設定為activiti.cfg.xml

 

相關文章