1. Overview
1.概述
In our previous Activiti with Java intro article, we saw the importance of the ProcessEngine and created one via the default static API provided by the framework.
在我们之前的Activiti with Java介绍文章中,我们看到了ProcessEngine的重要性,并通过框架提供的默认静态API创建了一个。
Beyond the default, there are other ways of creating a ProcessEngine – which we’ll explore here.
除了默认情况,还有其他创建ProcessEngine的方法–我们将在这里探讨。
2. Obtaining a ProcessEngine Instance
2.获得一个ProcessEngineInstance
There are two ways of getting an instance of ProcessEngine :
有两种方法可以获得ProcessEngine的实例。
- using the ProcessEngines class
- programmatically, via the ProcessEngineConfiguration
Let’s take a closer look at examples for both of these approaches.
让我们仔细看一下这两种方法的例子。
3. Get ProcessEngine Using ProcessEngines Class
3.使用ProcessEngine类获取ProcessEngines
Typically, the ProcessEngine is configured using an XML file called activiti.cfg.xml, with is what the default creation process will use as well.
通常,ProcessEngine是使用一个名为activiti.cfg.xml的XML文件来配置的,默认的创建过程也将使用该文件。
Here’s a quick example of what this configuration can look like:
下面是一个快速的例子,说明这种配置可以是什么样子。
<beans xmlns="...">
<bean id="processEngineConfiguration" class=
"org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl"
vasentence you have mentioned and also changed thelue="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="jdbcDriver" value="org.h2.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>
Notice how the persistence aspect of the engine is configured here.
注意这里是如何配置引擎的持久性方面的。
And now, we can obtain the ProcessEngine:
而现在,我们可以获得ProcessEngine。
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
4. Get ProcessEngine Using ProcessEngineConfiguration
4.使用ProcessEngineConfiguration获取ProcessEngine
Moving past the default route of obtaining the engine – there are two ways of creating the ProcessEngineConfiguration:
越过获取引擎的默认路线–有两种方法来创建ProcessEngineConfiguration。
- Using XML Config
- Using Java Config
Let’s start with XML configuration.
让我们从XML配置开始。
As mentioned in section 2.1. – we can define the ProcessEngineConfiguration programmatically, and build the ProcessEngine using that instance:
正如第2.1节所述。- 我们可以通过编程定义ProcessEngineConfiguration,并使用该实例构建ProcessEngine。
@Test
public void givenXMLConfig_whenCreateDefaultConfiguration_thenGotProcessEngine() {
ProcessEngineConfiguration processEngineConfiguration
= ProcessEngineConfiguration
.createProcessEngineConfigurationFromResourceDefault();
ProcessEngine processEngine
= processEngineConfiguration.buildProcessEngine();
assertNotNull(processEngine);
assertEquals("root", processEngine.getProcessEngineConfiguration()
.getJdbcUsername());
}
The method createProcessEngineConfigurationFromResourceDefault() will also look for the activiti.cfg.xml file, and now we only need to call the buildProcessEngine() API.
方法createProcessEngineConfigurationFromResourceDefault()也将寻找activiti.cfg.xml文件,现在我们只需要调用 buildProcessEngine() API。
In this case, the default bean name it looks for is processEngineConfiguration. If we want to change the config file name or the bean name, we can use other available methods for creating the ProcessEngineConfiguration.
在这种情况下,它寻找的默认bean名称是processEngineConfiguration。如果我们想改变配置文件名称或bean名称,我们可以使用其他可用的方法来创建ProcessEngineConfiguration。
Let’s have a look at a few examples.
让我们看一下几个例子。
First, we’ll change the configuration file name and ask the API to use our custom file:
首先,我们要改变配置文件的名称,要求API使用我们的自定义文件。
@Test
public void givenDifferentNameXMLConfig_whenGetProcessEngineConfig_thenGotResult() {
ProcessEngineConfiguration processEngineConfiguration
= ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource(
"my.activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfiguration
.buildProcessEngine();
assertNotNull(processEngine);
assertEquals("baeldung", processEngine.getProcessEngineConfiguration()
.getJdbcUsername());
}
Now, let’s also change the bean name:
现在,让我们也改变一下Bean的名字。
@Test
public void givenDifferentBeanNameInXMLConfig_whenGetProcessEngineConfig_thenGotResult() {
ProcessEngineConfiguration processEngineConfiguration
= ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource(
"my.activiti.cfg.xml",
"myProcessEngineConfiguration");
ProcessEngine processEngine = processEngineConfiguration
.buildProcessEngine();
assertNotNull(processEngine);
assertEquals("baeldung", processEngine.getProcessEngineConfiguration()
.getJdbcUsername());
}
Of course, now that the configuration is expecting different names, we need to change the filename (and the bean name) to match – before running the test.
当然,现在配置期待不同的名字,我们需要改变文件名(和Bean的名字)来匹配 – 在运行测试之前。
Other available options to create the engine are createProcessEngineConfigurationFromInputStream(InputStream inputStream),
createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName).
创建引擎的其他可用选项有:createProcessEngineConfigurationFromInputStream(InputStream inputStream),
createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName)/em>。
If we don’t want to use XML config, we can also set things up using Java config only.
如果我们不想使用XML配置,我们也可以只使用Java配置进行设置。
We’re going to work with four different classes; each of these represents a different environment:
我们将与四个不同的班级合作;每个班级都代表一个不同的环境。
- org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration – the ProcessEngine is used in a standalone way, backed by the DB
- org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration – by default, an H2 in-memory database is used. The DB is created and dropped when the engine starts and shuts down – hence, this configuration style can be used for testing
- org.activiti.spring.SpringProcessEngineConfiguration – to be used in Spring environment
- org.activiti.engine.impl.cfg.JtaProcessEngineConfiguration – the engine runs in standalone mode, with JTA transactions
Let’s take a look at a few examples.
让我们来看看几个例子。
Here is a JUnit test for creating a standalone process engine configuration:
这里有一个JUnit测试,用于创建一个独立的流程引擎配置。
@Test
public void givenNoXMLConfig_whenCreateProcessEngineConfig_thenCreated() {
ProcessEngineConfiguration processEngineConfiguration
= ProcessEngineConfiguration
.createStandaloneProcessEngineConfiguration();
ProcessEngine processEngine = processEngineConfiguration
.setDatabaseSchemaUpdate(ProcessEngineConfiguration
.DB_SCHEMA_UPDATE_TRUE)
.setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
.buildProcessEngine();
assertNotNull(processEngine);
assertEquals("sa", processEngine.getProcessEngineConfiguration()
.getJdbcUsername());
}
Similarly, we’ll write a JUnit test case for creating the standalone process engine configuration using the in-memory database:
同样地,我们将编写一个JUnit测试用例,用于使用内存数据库创建独立的流程引擎配置。
@Test
public void givenNoXMLConfig_whenCreateInMemProcessEngineConfig_thenCreated() {
ProcessEngineConfiguration processEngineConfiguration
= ProcessEngineConfiguration
.createStandaloneInMemProcessEngineConfiguration();
ProcessEngine processEngine = processEngineConfiguration
.buildProcessEngine();
assertNotNull(processEngine);
assertEquals("sa", processEngine.getProcessEngineConfiguration()
.getJdbcUsername());
}
5. Database Setup
5.数据库设置
By default, Activiti API will use the H2 in-memory database, with database name “activiti” and username “sa”.
默认情况下,Activiti API将使用H2内存数据库,数据库名称为 “activiti”,用户名为 “sa”。
If we need to use any other database, we’ll have to set things up explicitly – using two main properties.
如果我们需要使用任何其他的数据库,我们就必须明确地进行设置–使用两个主要属性。
databaseType – valid values are h2, mysql, oracle, postgres, mssql, db2. This can also be figured out from the DB configuration but will be useful if automatic detection fails.
databaseType – 有效值为h2, mysql, oracle, postgres, mssql, db2。这也可以从数据库配置中计算出来,但如果自动检测失败,这将是非常有用的。
databaseSchemaUpdate – this property allows us to define what happens to the database when the engine boots up or shuts down. It can have these three values:
databaseSchemaUpdate –这个属性允许我们定义当引擎启动或关闭时数据库会发生什么。它可以有这三个值。
- false (default) – this option validates the version of database schema against the library. The engine will throw an exception if they do not match
- true – when the process engine configuration is built, a check is performed on the database. The database will be created/updated create-drop accordingly
- “–” – this will create the DB schema when the process engine is created and will drop it when the process engine shuts down.
We can define the DB configuration as JDBC properties:
我们可以将DB配置定义为JDBC属性。
<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="" />
<property name="databaseType" value="mysql" />
Alternatively, if we are using DataSource:
另外,如果我们使用DataSource。
<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" />
<property name="databaseType" value="mysql" />
</bean>
6. Conclusion
6.结论
In this quick tutorial, we focused on several different ways of creating ProcessEngine in Activiti.
在这个快速教程中,我们重点介绍了在Activiti中创建ProcessEngine的几种不同方法。
We also saw different properties and approaches to handle database configuration.
我们还看到了处理数据库配置的不同属性和方法。
As always, the code for examples we saw can be found over on GitHub.
像往常一样,我们看到的例子的代码可以在GitHub上找到超过。