Activiti基礎教程--01(簡介、代碼生成Activiti的25張表、Activiti配置文件activiti.cfg.xml生成25張表、在Eclipse上安裝Activiti插件)

gihub:https://github.com/changwensir/Activiti/tree/master/ActivitiDemo1html

1、簡介

 

    Activiti項目是一項新的基於Apache許可的開源BPM平臺,從基礎開始構建,旨在提供支持新的BPMN 2.0標準,包括支持對象管理組(OMG),面對新技術的機遇,諸如互操做性和雲架構,提供技術實現。java

    創始人Tom Baeyens是JBoss jBPM的項目架構師,以及另外一位架構師Joram Barrez,一塊兒加入到建立Alfresco這項首次實現Apache開源許可的BPMN 2.0引擎開發中來。mysql

    Activiti是一個獨立運做和經營的開源項目品牌,並將獨立於Alfresco開源ECM系統運行。 Activiti將是一種輕量級,可嵌入的BPM引擎,並且還設計適用於可擴展的雲架構。 Activiti將提供寬鬆的Apache許可2.0,以便這個項目能夠普遍被使用,同時促進Activiti BPM引擎和的BPMN 2.0的匹配,該項目現正由OMG經過標準審定。 加入Alfresco Activiti項目的是VMware的SpringSource分支,Alfresco的計劃把該項目提交給Apache基礎架構,但願吸引更多方面的BPM專家和促進BPM的創新。git

 

Activiti官方主頁:http://www.activiti.org/index.html
Activiti下載:http://www.activiti.org/download.html
用戶指南中文:http://activiti.org/userguide/index.html(咱們經過用戶指南來學習Activiti)
在線API文檔: http://activiti.org/javadocs/index.html(咱們開發的時候,要用到)github

2、代碼Activiti的25張表

    Activiti的運行支持,必需要有Activiti的25張表,主要是在流程運行過程當中,記錄存儲一些參與流程的用戶主體,
組,以及流程定義的存儲,流程執行時候的一些信息,以及流程的歷史信息等(後面咱們會詳細介紹這些表做用)

  咱們先寫一個小實例,來把Activiti的25張表自動生成出來;
  咱們先建一個Maven項目 ActivitiDemo1
pom.xml里加上 Activiti支持,以及mysql驅動包。spring

 

[java] view plain copysql

  1. <dependencies>  
  2.      <dependency>  
  3.         <groupId>org.activiti</groupId>  
  4.         <artifactId>activiti-engine</artifactId>  
  5.         <version>5.19.0.2</version>  
  6.     </dependency>  
  7.        
  8.     <dependency>  
  9.         <groupId>org.activiti</groupId>  
  10.         <artifactId>activiti-spring</artifactId>  
  11.         <version>5.19.0.2</version>  
  12.     </dependency>  
  13.        
  14.     <dependency>  
  15.         <groupId>org.activiti</groupId>  
  16.         <artifactId>activiti-bpmn-model</artifactId>  
  17.         <version>5.19.0.2</version>  
  18.     </dependency>  
  19.        
  20.     <dependency>  
  21.         <groupId>mysql</groupId>  
  22.         <artifactId>mysql-connector-java</artifactId>  
  23.         <version>5.1.38</version>  
  24.     </dependency>  
  25.        
  26. </dependencies>  

 

而後咱們建立一個單元測試類 ActivitiTest01  (注意,有時候用本身的junit jar包,會出現詭異問題,因此咱們推薦用eclipse插件自帶的junit jar包)數據庫

建立後的項目結構以下:安全

 

  而後咱們寫一個測試方法,testCreateTable,而且在建立db_activiti數據庫;
  咱們要先來獲取流程引擎配置,而後來配置流程引擎,要配置驅動包,url,數據庫用戶名,密碼;還要設置schema,這裏的schema要設置成update。這樣能夠自動更新,配置後,咱們經過配置來獲取流程引擎。建立實例的時候咱們就能夠自動生成須要的25張表。架構

[java] view plain copy

  1. package com.java1234.activiti.test;  
  2.    
  3. import org.activiti.engine.ProcessEngine;  
  4. import org.activiti.engine.ProcessEngineConfiguration;  
  5. import org.junit.Test;  
  6.    
  7. public class ActivitiTest01 {  
  8.    
  9.     /** 
  10.      * 生成25張Activiti表 
  11.      */  
  12.     @Test  
  13.     public void testCreateTable() {  
  14.         // 引擎配置  
  15.         ProcessEngineConfiguration pec=ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();  
  16.         pec.setJdbcDriver("com.mysql.jdbc.Driver");  
  17.         pec.setJdbcUrl("jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&characterEncoding=utf8");  
  18.         pec.setJdbcUsername("root");  
  19.         pec.setJdbcPassword("123456");  
  20.            
  21.         /** 
  22.          * DB_SCHEMA_UPDATE_FALSE 不能自動建立表,須要表存在 
  23.          * create-drop 先刪除表再建立表 
  24.          * DB_SCHEMA_UPDATE_TRUE 如何表不存在,自動建立和更新表   
  25.          */  
  26.         pec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);  
  27.            
  28.         // 獲取流程引擎對象  
  29.         ProcessEngine processEngine=pec.buildProcessEngine();  
  30.     }  
  31.    
  32. }  

咱們運行測試類,運行完咱們刷新數據庫,便可看到25張表:

    ACT_RE_*: 'RE'表示repository。 這個前綴的表包含了流程定義和流程靜態資源 (圖片,規則,等等)。
    ACT_RU_*: 'RU'表示runtime。 這些運行時的表,包含流程實例,任務,變量,異步任務,等運行中的數據。 Activiti只在流程實例執行過程當中保存這些數據, 在流程結束時就會刪除這些記錄。 這樣運行時表能夠一直很小速度很快。
    ACT_ID_*: 'ID'表示identity。 這些表包含身份信息,好比用戶,組等等。
    ACT_HI_*: 'HI'表示history。 這些表包含歷史數據,好比歷史流程實例, 變量,任務等等。
    ACT_GE_*: 'GE'表示general。通用數據, 用於不一樣場景下,如存放資源文件。
這些表結構,還有一些輔助表。咱們後續會詳細解釋,這裏你們先大致瞭解便可;

表結構操做:
一、資源庫流程規則表
  1).act_re_deployment     部署信息表
  2).act_re_model      流程設計模型部署表
  3).act_re_procdef      流程定義數據表
二、運行時數據庫表
  1).act_ru_execution            運行時流程執行實例表
  2).act_ru_identitylink    運行時流程人員表,主要存儲任務節點與參與者的相關信息
  3).act_ru_task        運行時任務節點表
  4).act_ru_variable        運行時流程變量數據表
三、歷史數據庫表
  1).act_hi_actinst         歷史節點表
  2).act_hi_attachment        歷史附件表
  3).act_hi_comment        歷史意見表
  4).act_hi_identitylink    歷史流程人員表
  5).act_hi_detail        歷史詳情表,提供歷史變量的查詢
  6).act_hi_procinst        歷史流程實例表
  7).act_hi_taskinst        歷史任務實例表
  8).act_hi_varinst        歷史變量表
四、組織機構表
  1).act_id_group        用戶組信息表
  2).act_id_info        用戶擴展信息表
  3).act_id_membership            用戶與用戶組對應信息表
  4).act_id_user        用戶信息表
這四張表很常見,基本的組織機構管理,關於用戶認證方面建議仍是本身開發一套,組件自帶的功能太簡單,使用中有不少需求難以知足 
五、通用數據表
  1).act_ge_bytearray        二進制數據表
  2).act_ge_property        屬性數據表存儲整個流程引擎級別的數據,初始化表結構時,會默認插入三條記錄,

3、Activiti配置文件activiti.cfg.xml生成25張表

  前面咱們用代碼實現了生成25張activiti表,今天咱們用Activiti提供的activiti.cfg.xml配置文件來簡化實現前面的功能
  官方文檔參考地址:http://activiti.org/userguide/index.html#configuration 

咱們先在src/test/resources下建立一個xml文件 名字是:activiti.cfg.xml,而後咱們從官方文檔貼下參考的xml代碼:

 

[html] view plain copy

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">  
  4.    
  5.   <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">  
  6.    
  7.     <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
  8.     <property name="jdbcDriver" value="org.h2.Driver" />  
  9.     <property name="jdbcUsername" value="sa" />  
  10.     <property name="jdbcPassword" value="" />  
  11.    
  12.     <property name="databaseSchemaUpdate" value="true" />  
  13.    
  14.     <property name="jobExecutorActivate" value="false" />  
  15.     <property name="asyncExecutorEnabled" value="true" />  
  16.     <property name="asyncExecutorActivate" value="false" />  
  17.    
  18.     <property name="mailServerHost" value="mail.my-corp.com" />  
  19.     <property name="mailServerPort" value="5025" />  
  20.   </bean>  
  21.    
  22. </beans>  

  這裏的話,咱們要根據咱們的項目 修改jdbcUrl jdbcDriver jdbcUsername jdbcPassword 固然還有下面的配置咱們能夠去掉一些 後面會降到這些配置的用途;修改完後的xml以下:

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  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.    
  6.   <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">  
  7.    
  8.     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&amp;characterEncoding=utf8" />  
  9.     <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />  
  10.     <property name="jdbcUsername" value="root" />  
  11.     <property name="jdbcPassword" value="123456" />  
  12.  <!-- 對應 DB_SCHEMA_UPDATE_TRUE 如何表不存在,自動建立和更新表 -->  
  13.     <property name="databaseSchemaUpdate" value="true" />  
  14.    
  15.   </bean>  
  16.    
  17. </beans>  

接下來咱們就是要經過代碼來讀取配置文件,而後獲取工做流引擎實例(測試的時候 先把前面db_activiti數據下的表 所有刪除;):

[java] view plain copy

  1. /** 
  2.  * 使用xml配置 簡化 
  3.  */  
  4. @Test  
  5. public void testCreateTableWithXml(){  
  6.     // 引擎配置  
  7.     ProcessEngineConfiguration pec=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");  
  8.     // 獲取流程引擎對象  
  9.     ProcessEngine processEngine=pec.buildProcessEngine();  
  10. }  

而後運行上面的測試類,咱們會發現 表自動生成了:

 

表依然是前面的25張表;咱們會發現,使用xml配置會簡化不少東西。

4、在Eclipse上安裝Activiti插件

 

這裏咱們在本身的eclipse來裝下這個牛逼轟轟的插件:

首先打開Eclipse(J2ee版本的,最好用新的版本,Maven啥的都支持,不要搞個老古董)

而後點擊help -> Install new Software 

而後點擊 Add

 

咱們在Name上 寫上 Activiti BPMN 2.0 designer

Location上寫上 http://activiti.org/designer/update/  (插件更新地址)

而後點擊 OK 按鈕

勾選下,而後next:

繼續next

點下接受,而後 Finish 

中間可能會一些安全提示,點 OK 便可;

 

這裏咱們點Yes,重啓下Eclipse;纔會生效;

爲了驗證咱們是否成功安裝插件,咱們點擊File -> New -> Others

 

咱們若是看到這個,說明就已經安裝成功了。後面咱們會具體講這東西怎麼用;

這裏咱們有個地方要設置下,點下Window - > Preferences

 

這裏我先大致解釋下。後面學習的時候 會建立圖表,以及生成一張圖片。咱們這裏打上勾。

保存圖表的時候,就建立圖片。咱們開發的時候必須這麼配置;(後面你們會明白的,先這樣設置)

 

OK到這裏的話,插件已經安裝和設置完畢了。

相關文章
相關標籤/搜索