1. 程式人生 > >俯瞰工作流Activiti-引擎配置

俯瞰工作流Activiti-引擎配置

在開源工作流流程引擎Activiti的過程中,有很多引數是可配置的,例如資料庫配置、事務配置、流程引擎內建服務配置等,Activiti通過流程引擎配置物件封裝這些配置。本文對Activiti流程引擎的配置進行比較詳細的介紹,讓讀者對各個配置項有深入的理解。 在這裡插入圖片描述

1.流程引擎配置物件

流程引擎配置物件ProcessEngineConfiguration裡面有Activiti流程引擎裡面的全部配置,為這些可配置的引擎屬性提供對是對應的setter合getter方法。該類還提供了很多建立流程引擎配置物件ProcessEngineConfiguration例項的靜態方法,這些方法會讀取解析配置屬性,然後返回物件的例項。該類是一個抽象類,實現類的類圖結構如下圖。在這裡插入圖片描述

ProcessEngineConfiguration代表流程引擎的一個配置例項,由於ProcessEngineConfiguration是一個抽象類,我們就要用它的子類去建立bean。官方文件給出了每個實現類的作用。

The activiti.cfg.xml must contain a bean that has the id processEngineConfiguration.

<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"
>

This bean is then used to construct the ProcessEngine. There are multiple classes available that can be used to define the processEngineConfiguration. These classes represent different environments, and set defaults ccordingly. It’s a best practice to select the class the matches (the most) your environment, to minimalise the number of properties needed to configure the engine. The following classes are currently available (more will follow in future releases):

  • org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration: the process engine is used in a standalone way. Activiti will take care of the transactions. By default, the database will only be checked when the engine boots (and an exception is thrown if there is no Activiti schema or the schema version is incorrect).
  • org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration: this is a convenience class for unit testing purposes. Activiti will take care of the transactions. An H2 in-memory database is used by default. The database will be created and dropped when the engine boots and shuts down. When using this, probably no additional configuration is needed (except when using for example the job executor or mail capabilities).
  • org.activiti.spring.SpringProcessEngineConfiguration: To be used when the process engine is used in a Spring environment.
  • org.activiti.engine.impl.cfg.JtaProcessEngineConfiguration: To be used when the engine runs in standalone mode, with JTA transactions.

簡單翻譯如下: 配置檔案activiti.cfg.xml必須有一個id為processEngineConfiguration的例項.這個例項是用來建立流程引擎例項ProcessEngine的。有多種類可以用來定義processEngineConfiguration,不同的類適合不同的場景,並設定相應的預設值。選擇合適的類匹配我們的場景,可以讓引擎配置的屬性少而準確。下面是提供的可用的類(以後的版本中將會有更多的選擇):

  • org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration: 應用在獨立的環境中。 Activiti將會管理事務。預設情況下,只有在引擎啟動時才會檢查資料庫(沒有找到資料庫或者錯誤的版本都會丟擲異常)。
  • org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration: 這個類的目的是為了更方便單元測試。Activiti也會管理事務。預設就使用記憶體資料庫H2。在流程引擎啟動或者結束的時候會對應建立和刪除資料庫。不需要進行額外的配置。(除非使用工作執行器和或者郵件功能)。
  • org.activiti.spring.SpringProcessEngineConfiguration: 用於與spring整合。
  • org.activiti.engine.impl.cfg.JtaProcessEngineConfiguration: 用於獨立環境中,使用JTA事務。

2.資料庫配置

Activiti使用兩種資料庫配置方式,第一種是直接用JDBC日的屬性進行配置。

  • jdbcUrl: JDBC URL。
  • jdbcDriver:不同資料庫型別的驅動。
  • jdbcUsername: 使用者名稱。
  • jdbcPassword: 密碼。

第二種是用Mybatis的連線池配置,下面這些屬性引用的是mybaits的文件。

  • jdbcMaxActiveConnections: 最大活躍連線數,預設10。
  • jdbcMaxIdleConnections: 最大空閒連線數。
  • jdbcMaxCheckoutTime: 心跳檢查,連線被取出使用的最長時間,超過時間會被強制回收。預設20000(20s)。
  • jdbcMaxWaitTime: 最大等待時間。預設20000(20s)。官方解釋如下:

This is a low level setting that gives the pool a chance to print a log status and re-attempt the acquisition of a connection in the case that it is taking unusually long (to avoid failing silently forever if the pool is misconfigured) Default is 20000 (20 seconds).

下面是一個數據庫配置舉例:

<property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="jdbcDriver" value="org.h2.Driver" />
<property name="jdbcUsername" value="sa" />
<property name="jdbcPassword" value="" />

我們都知道,當我們處理更多的連線請求,Mybatis連線池並不是最有效和恢復比較好的。所以官方建議使用javax.sql.DataSource 的實現類注入到我們的流程引擎中。 (例如 DBCP, C3P0, Hikari, Tomcat Connection Pool等)。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
	<property name="driverClassName" value="com.mysql.jdbc.Driver" />
	<property name="url" value="jdbc:mysql://localhost:3306/activiti" />
	<property name="username" value="activiti" />
	<property name="password" value="activiti" />
    <property name="defaultAutoCommit" value="false" />
</bean>
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
	<property name="dataSource" ref="dataSource" />
	...

注意: Activit並沒有並沒有將這些資料來源整合,所以我們需要確保我的的classpath有這些資料來源的類庫。

不管是使用JDBC還是還是資料來源,下面兩個屬性都可以配置:

  • databaseType: 資料庫型別並不需要特別的去指定,從資料庫連線的元資料可以自動識別資料庫型別,僅僅當自動識別失敗才需要配置該屬性。可選的值有:{h2, mysql, oracle, postgres, mssql, db2}. 這個設定會決定哪種資料庫指令碼將會被執行,支援的資料庫型別如下。

    資料庫型別 JDBC URL 備註
    h2 jdbc:h2:tcp://localhost/activit 預設的資料庫配置,記憶體資料庫,主要用於單元測試
    mysql jdbc:mysql://localhost:3306/activiti?autoReconnect=true 開源,體積小,速度快
    oracle jdbc:oracle:thin:@localhost:1521:xe 效能較高,可靠的資料管理,價格昂貴
    postgres jdbc:postgresql://localhost:5432/activiti
    db2 jdbc:db2://localhost:50000/activiti
    mssql jdbc:sqlserver://localhost:1433;databaseName=activiti OR jdbc:jtds:sqlserver://localhost:1433/activiti Tested using Microsoft JDBC Driver 4.0 (sqljdbc4.jar) and JTDS Driver
  • databaseSchemaUpdate: 該屬性可以設定流程引擎啟動和關閉時資料庫執行的策略:

    • false (預設值): 流程引擎啟動時檢查資料庫表中的的版本,如果沒有表或者版本不匹配,丟擲異常。
    • true: 引擎啟動時,自動更新表,表不存在車自動建立。
    • create-drop: 引擎啟動是建立資料庫,引擎關閉時刪除資料庫。

3.任務執行器配置(Activiti6.0+)

asyncExecutorActivate屬性主要用於配置非同步執行器是否啟動,true則表示Activiti在建立路程引擎時,需要啟動非同步執行器,該屬性的預設值為false。非同步執行器啟動後,會啟動定時器掃描並執行各種工作。

4.郵件服務配置

Activiti提供配置郵件伺服器的能力。Activiti支援在業務流程中傳送電子郵件。要實際傳送電子郵件,需要一個有效的SMTP郵件伺服器配置。下面這些有關配置可以在activiti.cfg.xml裡設定:

  • mailServerHost:郵件伺服器地址。非必填,預設為本機localhost。
  • mailServerPort:埠,預設25.
  • mailServerDefaultFrom:非必填,傳送人的郵箱地址,預設為[email protected]
  • mailServerUsername:郵箱登入名。
  • mailServerPassword:郵箱密碼。
  • mailServerUseSSL:是否使用SSL協議通道,預設false。
  • mailServerUseTLS:是否使用TSL協議通道,預設false。

5.歷史資料配置

在流程執行的過程中,會產生一些流程相關的資料,例如流程例項、流程任務和流程引數等,隨著流程的結束,這些資料會從執行時資料表刪除,並且儲存到歷史資料表。 歷史資料的儲存可以設定不同的級別,Activiti提供了history屬性對儲存歷史資料的級別進行配置。有點類似平時使用的日子級別,history提供了是個選項:

  • none:不儲存任何值,效率高,但是歷史資料一點都不儲存。
  • activiti:高於none級別,儲存流程例項與流程行為,其他資料不儲存。
  • audit:預設級別,出去activiti級別的資料會儲存外,還會儲存全部的流程任務以及其屬性資料。
  • full:最高級別,出去前面級別儲存的,還會把偶才能其他全部流程相關的細節資料,包括一些流程引數等。

本文講述了Activiti流程引擎的一些常用配置,並且分析了這些配置在流程引擎中的一些具體作用,同時也講述了ProcessEngineConfiguration的bean配置。這些配置是流程引擎的基礎,掌握了這些配置知識後,可以建立與具體業務更加貼近的個性化流程引擎。