配置
创建 Event Registry 引擎
Flowable Event Registry 引擎的结构与 Flowable 流程引擎非常相似。因此,部分文档与流程引擎对应部分非常相似。
Flowable Event Registry 引擎通过一个名为 flowable.eventregistry.cfg.xml 的 XML 文件进行配置。注意,如果你使用以 Spring 方式构建 Event Registry 引擎,则不适用此方式。
获取 EventRegistryEngine 最简单的方式是使用 org.flowable.eventregistry.engine.EventRegistryEngines 类:
EventRegistryEngine eventRegistryEngine = EventRegistryEngines.getDefaultEventRegistryEngine()
这会在 classpath 上查找 flowable.eventregistry.cfg.xml 文件,并基于该文件中的配置构建一个引擎。下面的代码片段展示了一个示例配置。后续各节将详细介绍配置属性。
<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="eventRegistryEngineConfiguration" class="org.flowable.eventregistry.impl.cfg.StandaloneEventRegistryEngineConfiguration">
<property name="jdbcUrl" value="jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000" />
<property name="jdbcDriver" value="org.h2.Driver" />
<property name="jdbcUsername" value="sa" />
<property name="jdbcPassword" value="" />
<property name="databaseSchemaUpdate" value="true" />
<property name="strictMode" value="false"/>
</bean>
</beans>
注意,配置 XML 实际上是一个 Spring 配置。这并不意味着 Flowable Event Registry 只能在 Spring 环境中使用! 我们只是在内部利用 Spring 的解析和依赖注入能力来构建引擎。
EventRegistryEngineConfiguration 对象也可以使用配置文件以编程方式创建。也可以使用不同的 bean ID(例如,见第 3 行)。
EventRegistryEngineConfiguration.
createEventRegistryEngineConfigurationFromResourceDefault();
createEventRegistryEngineConfigurationFromResource(String resource);
createEventRegistryEngineConfigurationFromResource(String resource, String beanName);
createEventRegistryEngineConfigurationFromInputStream(InputStream inputStream);
createEventRegistryEngineConfigurationFromInputStream(InputStream inputStream, String beanName);
也可以不使用配置文件,而是基于默认值创建配置(更多信息参见支持的不同类)。
EventRegistryEngineConfiguration.createStandaloneEventRegistryEngineConfiguration();
EventRegistryEngineConfiguration.createStandaloneInMemEventRegistryEngineConfiguration();
所有这些 EventRegistryEngineConfiguration.createXXX() 方法都返回一个 EventRegistryEngineConfiguration,如有需要可进一步调整。调用 buildEventRegistryEngine() 操作后,即创建一个 EventRegistryEngine:
EventRegistryEngine eventRegistryEngine = EventRegistryEngineConfiguration.createStandaloneInMemEventRegistryEngineConfiguration()
.setDatabaseSchemaUpdate(EventRegistryEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
.setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
.buildEventRegistryEngine();
EventRegistryEngineConfiguration bean
flowable.eventregistry.cfg.xml 必须包含一个 ID 为 'eventRegistryEngineConfiguration' 的 bean。
<bean id="eventRegistryEngineConfiguration" class="org.flowable.eventregistry.impl.cfg.StandaloneEventRegistryEngineConfiguration">
然后使用该 bean 来构造 EventRegistryEngine。有多个类可用于定义 eventRegistryEngineConfiguration。这些类代表不同的环境,并相应地设置默认值。最佳实践是选择最接近你环境的类,以最小化配置引擎所需的属性数量。目前可用的类如下:
- org.flowable.eventregistry.impl.cfg.StandaloneEventRegistryEngineConfiguration:以独立方式使用 event registry 引擎。Flowable 将负责事务。默认情况下,仅在引擎启动时检查数据库(如果没有 Flowable Event Registry schema 或 schema 版本不正确,则抛出异常)。
- org.flowable.eventregistry.impl.cfg.StandaloneInMemEventRegistryEngineConfiguration:这是一个用于单元测试的便捷类。Flowable Event Registry 将负责事务。默认使用 H2 内存数据库。引擎启动和关闭时会创建和删除数据库。使用此类时,可能无需额外配置。
- org.flowable.eventregistry.spring.SpringEventRegistryEngineConfiguration:在 Spring 环境中使用 Event Registry 引擎时使用。更多信息参见Spring 集成章节。
插入 Process 引擎
启动流程引擎时会自动引导 Event Registry。因此,在已使用流程引擎的情况下,启动 event registry 无需额外配置。流程引擎使用默认的 EventRegistryEngineConfigurator 来启动 event registry。可以通过在流程引擎配置中设置 eventRegistryConfigurator 来覆盖此默认 configurator。
也可以在流程引擎配置中使用 disableEventRegistry 属性禁用 event registry。
数据库配置
Flowable Event Registry 引擎将使用的数据库有两种配置方式。第一种是定义数据库的 JDBC 属性:
- jdbcUrl:数据库的 JDBC URL。
- jdbcDriver:特定数据库类型的驱动实现。
- jdbcUsername:连接数据库的用户名。
- jdbcPassword:连接数据库的密码。
基于提供的 JDBC 属性构造的数据源将使用默认的 MyBatis 连接池设置。可以选择设置以下属性来调整该连接池(取自 MyBatis 文档):
- jdbcMaxActiveConnections:连接池在任何时候可包含的最大活动连接数。默认 10。
- jdbcMaxIdleConnections:连接池在任何时候可包含的最大空闲连接数。
- jdbcMaxCheckoutTime:连接从连接池被“检出”后、在被强制归还之前可占用的时间(毫秒)。默认 20000(20 秒)。
- jdbcMaxWaitTime:这是一个底层设置,当获取连接耗时异常过长时(为避免连接池配置错误而永远静默失败),给连接池一个打印日志状态并重试获取连接的机会。默认 20000(20 秒)。
数据库配置示例:
<property name="jdbcUrl" value="jdbc:h2:mem:flowable_event;DB_CLOSE_DELAY=1000" />
<property name="jdbcDriver" value="org.h2.Driver" />
<property name="jdbcUsername" value="sa" />
<property name="jdbcPassword" value="" />
我们的基准测试表明,在处理大量并发请求时,MyBatis 连接池并不是最高效或最有弹性的。因此,建议使用一个 javax.sql.DataSource 实现(例如 HikariCP、Tomcat JDBC Connection Pool 等)并将其注入 event registry 引擎配置:
<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/flowable_event" />
<property name="username" value="flowable" />
<property name="password" value="flowable" />
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="eventRegistryEngineConfiguration" class="org.flowable.eventregistry.impl.cfg.StandaloneEventRegistryEngineConfiguration">
<property name="dataSource" ref="dataSource" />
...
注意,Flowable Event Registry 并未附带允许你定义此类数据源的库。因此你必须确保这些库在你的 classpath 上。
无论你 使用 JDBC 方式还是数据源方式,都可以设置以下属性:
-
databaseType:通常无需指定此属性,因为它会从数据库连接元数据自动检测。仅在自动检测失败时才应指定。可能值:{h2, mysql, oracle, postgres, mssql, db2}。此设置将决定使用哪些建表/删表脚本和查询。支持哪些类型的概览见“支持的数据库”章节。
-
databaseSchemaUpdate:允许你设置在流程引擎启动和关闭时处理数据库 schema 的策略。
-
false(默认):创建 event registry 引擎时对照库检查数据库 schema 的版本,如果版本不匹配则抛出异常。
-
true:构建 event registry 引擎时执行检查,如有必要则更新 schema。如果 schema 不存在,则创建它。
-
create-drop:创建 event registry 引擎时创建 schema,关闭 event registry 引擎时删除 schema。
-
JNDI 数据源配置
默认情况下,Flowable Event Registry 的数据库配置包含在每个 web 应用 WEB-INF/classes 下的 db.properties 文件中。这并不总是理想的,因为它要求用户要么修改 Flowable 源码中的 db.properties 并重新编译 WAR 文件,要么在每次部署时解压 WAR 并修改 db.properties。
通过使用 JNDI(Java Naming and Directory Interface)获取数据库连接,连接完全由 Servlet 容器管理,配置可以在 war 部署之外管理。这也比 db.properties 文件提供的方式对连接参数有更多控制。
配置
JNDI 数据源的配置会因你使用的 servlet 容器应用而异。下面的说明适用于 Tomcat,但对于其他容器应用,请参考你所用容器应用的文档。
如果使用 Tomcat,JNDI 资源在 $CATALINA_BASE/conf/[enginename]/[hostname]/[warname].xml 中配置(对于 Flowable UI,通常是 $CATALINA_BASE/conf/Catalina/localhost/flowable-app.xml)。首次部署应用时,默认上下文会从 Flowable WAR 文件复制过来,所以如果它已存在,你需要替换它。例如,要更改 JNDI 资源使应用连接到 MySQL 而非 H2,将文件改为如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/flowable-app">
<Resource auth="Container"
name="jdbc/flowableDB"
type="javax.sql.DataSource"
description="JDBC DataSource"
url="jdbc:mysql://localhost:3306/flowable"
driverClassName="com.mysql.jdbc.Driver"
username="sa"
password=""
defaultAutoCommit="false"
initialSize="5"
maxWait="5000"
maxActive="120"
maxIdle="5"/>
</Context>
JNDI 属性
要配置 JNDI 数据源,在 Flowable UI 的属性文件中使用以下属性:
- spring.datasource.jndi-name=:数据源的 JNDI 名称。
- datasource.jndi.resourceRef:设置是否在 J2EE 容器中进行查找,换句话说,如果 JNDI 名称尚未包含前缀 "java:comp/env/",是否需要添加该前缀。默认 "true"。
自定义属性
也可以在 flowable.eventregistry.cfg.xml 中使用系统属性,格式为 $\{propertyName:defaultValue}。
<property name="jdbcUrl" value="${jdbc.url:jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000}" />
<property name="jdbcDriver" value="${jdbc.driver:org.h2.Driver}" />
<property name="jdbcUsername" value="${jdbc.username:sa}" />
<property name="jdbcPassword" value="${jdbc.password:}" />
使用此配置时,如果属性 jdbc.url 可用,它将用于 EventRegistryEngineConfiguration 的 jdbcUrl。否则将使用第一个 : 之后的值。
也可以通过使用 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 类型的 bean 来定义从系统中读取属性的位置。
带自定义属性位置的配置示例
<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 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/opt/conf/flowable.properties" />
</bean>
<bean id="eventRegistryEngineConfiguration" class="org.flowable.eventregistry.impl.cfg.StandaloneEventRegistryEngineConfiguration">
<property name="jdbcUrl" value="${jdbc.url:jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000}" />
<property name="jdbcDriver" value="${jdbc.driver:org.h2.Driver}" />
<property name="jdbcUsername" value="${jdbc.username:sa}" />
<property name="jdbcPassword" value="${jdbc.password:}" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>
使用此配置时,属性将首先在 /opt/conf/flowable.properties 文件中查找。
支持的数据库
下面列出 Flowable 用 于指代数据库的类型(区分大小写!)。
| Flowable Event Registry 数据库类型 | JDBC URL 示例 | 备注 |
|---|---|---|
| h2 | jdbc:h2:tcp://localhost/flowable_event | 默认配置的数据库 |
| mysql | jdbc:mysql://localhost:3306/flowable_event?autoReconnect=true | 使用 mysql-connector-java 数据库驱动测试 |
| oracle | jdbc:oracle:thin:@localhost:1521:xe | |
| postgres | jdbc:postgresql://localhost:5432/flowable_event | |
| db2 | jdbc:db2://localhost:50000/flowable_event | |
| mssql | jdbc:sqlserver://localhost:1433;databaseName=flowable_event (jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver) 或 jdbc:jtds:sqlserver://localhost:1433/flowable_event (jdbc.driver=net.sourceforge.jtds.jdbc.Driver) | 使用 Microsoft JDBC Driver 4.0(sqljdbc4.jar)和 JTDS Driver 测试 |
创建数据库表
Flowable Event Registry 使用 Liquibase 来跟踪、管理和应用数据库 schema 变更。
为你的数据库创建数据库表最简单的方式是:
- 将 flowable-event-registry JAR 添加到你的 classpath
- 添加合适的数据库驱动
- 将一个 Flowable 配置文件(flowable.eventregistry.cfg.xml)添加到 你的 classpath,指向你的数据库(参见数据库配置章节)
- 执行 DbSchemaCreate 类的 main 方法
数据库表名说明
Flowable Event Registry 的数据库名都以 FLW_EVENT_ 或 FLW_CHANNEL_ 开头。
- FLW_EVENT_DATABASECHANGELOG:此表由 Liquibase 用于跟踪已运行的 changeset。
- FLW_EVENT_DATABASECHANGELOGLOCK:此表由 Liquibase 用于确保同一时间只有一个 Liquibase 实例在运行。
- FLW_EVENT_DEFINITION:此表包含已部署事件定义的元数据。
- FLW_CHANNEL_DEFINITION:此表包含已部署通道定义的元数据。
- FLW_EVENT_DEPLOYMENT:此表包含部署元数据。
- FLW_EVENT_RESOURCE:此表包含 Event Registry 定义资源和元数据。
数据库升级
运行升级之前,请确保备份你的数据库(使用你的数据库备份功能)。
默认情况下,每次创建 event registry 引擎时都会执行版本检查。这通常在你的应用或 Flowable webapps 启动时发生一次。如果 Flowable 库发现库版本与 Flowable 数据库表版本之间存在差异,则抛出异常。
要升级,首先要在你的 flowable.dmn.cfg.xml 配置文件中加入以下配置属性:
<beans >
<bean id="eventRegistryEngineConfiguration" class="org.flowable.eventregistry.impl.cfg.StandaloneEventRegistryEngineConfiguration">
<!-- ... -->
<property name="databaseSchemaUpdate" value="true" />
<!-- ... -->
</bean>
</beans>
此外,在 classpath 中为你的数据库包含合适的数据库驱动。 升级你的应用中的 Flowable Event Registry 库。或者启动新版本的 Flowable Event Registry,并将其指向包含旧版本的数据库。当 databaseSchemaUpdate 设为 true 时,Flowable Event Registry 在首次发现库与数据库 schema 不同步时,会自动将数据库 schema 升级到较新版本。
部署缓存配置
所有事件和通道定义在解析后都会被缓存,以避免每次需要此类定义时都访问数据库,因为这些定义不会改变。默认情况下,此缓存没有限制。要限制事件定义缓存,添加以下属性
<property name="eventDefinitionCacheLimit" value="10" />
设置此属性将用一个具有所提供硬限制的 LRU 缓存替换默认的 hashmap 缓存。当然,此属性的最佳值取决于存储的事件定义总量以及运行时实际使用的事件定义数量。
你也可以注入自己的缓存实现。它必须是一个实现了 org.flowable.common.engine.impl.persistence.deploy.DeploymentCache 接口的 bean:
<property name="eventDefinitionCache">
<bean class="org.flowable.MyCache" />
</property>
通道 定义缓存不能配置限制,因为所有活动通道定义都需要在缓存中可用。
日志
所有日志(flowable、spring、mybatis 等)都通过 SLF4J 路由,并允许选择你喜欢的日志实现。
默认情况下,flowable-engine 依赖中不包含 SFL4J 绑定 JAR;要使用你喜欢的日志框架,应在你的项目中添加它。 如果未添加实现 JAR,SLF4J 将使用一个 NOP 日志器,除了发出“将不记录任何日志”的警告外,什么都不记录。有关这些绑定的更多信息,参见 http://www.slf4j.org/codes.html#StaticLoggerBinder。
例如,使用 Maven 时添加如下依赖(这里使用 log4j),注意你仍需添加版本:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
</dependency>
flowable-rest webapp 配置为使用 Log4j 绑定。运行所有 flowable-* 模块的测试时也使用 Log4j。
在 classpath 中使用带 commons-logging 的容器时的重要提示: 为了将 spring 日志通过 SLF4J 路由,使用了一个桥接器(参见 http://www.slf4j.org/legacy.html#jclOverSLF4J)。如果你的容器提供了 commons-logging 实现,请按照此页面上的说明操作:http://www.slf4j.org/codes.html#release 以确保稳定性。
使用 Maven 时的示例(版本省略):
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>