ProcessEngineFactoryBean
對(duì)于Spring集成,配置和引擎bean將如下所示:
<bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
...
</bean>
<bean id="processEngine" class="org.flowable.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
交易
- 將DataSource傳遞給SpringProcessEngineConfiguration
- Flowable使用org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy內(nèi)部苇倡,它包裝傳遞的DataSource
- 目的:確保從DataSource和Spring事務(wù)中檢索到的SQL連接能夠很好地協(xié)同工作(意味著它不再需要代理的數(shù)據(jù)源自己在Spring的配)
- 確保在TransactionAwareDataSourceProxy自己聲明Spring配置時(shí),不要將它用于已經(jīng)知道Spring事務(wù)的資源(例如损离,DataSourceTransactionManager和JPATransactionManager需要未代理的dataSource)
- Flowable使用org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy內(nèi)部苇倡,它包裝傳遞的DataSource
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="asyncExecutorActivate" value="false" />
</bean>
<bean id="processEngine" class="org.flowable.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
...
<beans>
...
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="userBean" class="org.flowable.spring.test.UserBean">
<property name="runtimeService" ref="runtimeService" />
</bean>
<bean id="printer" class="org.flowable.spring.test.Printer" />
</beans>
- 使用Spring支持的任何方式創(chuàng)建應(yīng)用程序上下文
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"org/flowable/examples/spring/SpringTransactionIntegrationTest-context.xml");
- 使用repositoryService來(lái)部署這樣的進(jìn)程
RepositoryService repositoryService =
(RepositoryService) applicationContext.getBean("repositoryService");
String deploymentId = repositoryService
.createDeployment()
.addClasspathResource("org/flowable/spring/test/hello.bpmn20.xml")
.deploy()
.getId();
表達(dá)式
- 使用ProcessEngineFactoryBean時(shí),BPMN進(jìn)程中的所有表達(dá)式默認(rèn)情況下也會(huì)看到所有Spring bean
- 要完全暴露NO bean撤卢,只需在SpringProcessEngineConfiguration上將空列表作為beans屬性傳遞艳悔。如果未設(shè)置beans屬性悄窃,則上下文中的所有Spring bean都可用讥电。
<bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
...
<property name="beans">
<map>
<entry key="printer" value-ref="printer" />
</map>
</property>
</bean>
<bean id="printer" class="org.flowable.examples.spring.Printer" />
3.在表達(dá)式中使用公開(kāi)的bean:例如,SpringTransactionIntegrationTest hello.bpmn20.xml顯示如何使用UEL方法表達(dá)式調(diào)用Spring bean上的方法
<definitions id="definitions">
<process id="helloProcess">
<startEvent id="start" />
<sequenceFlow id="flow1" sourceRef="start" targetRef="print" />
<serviceTask id="print" flowable:expression="#{printer.printMessage()}" />
<sequenceFlow id="flow2" sourceRef="print" targetRef="end" />
<endEvent id="end" />
</process>
</definitions>
自動(dòng)資源部署
- 在流程引擎配置中轧抗,您可以指定一組資源恩敌。創(chuàng)建流程引擎后,將掃描和部署所有這些資源
- 有過(guò)濾可以防止重復(fù)部署鸦致。僅當(dāng)資源實(shí)際更改時(shí)潮剪,才會(huì)將新部署部署到Flowable DB
<bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
...
<property name="deploymentResources"
value="classpath*:/org/flowable/spring/test/autodeployment/autodeploy.*.bpmn20.xml" />
</bean>
<bean id="processEngine" class="org.flowable.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
- 默認(rèn)情況下涣楷,上面的配置會(huì)將與過(guò)濾器匹配的所有資源分組到Flowable引擎的單個(gè)部署中
- 這些資源中只有一個(gè)流程定義發(fā)生了變化,則整個(gè)部署將被視為新部署
- 自定義部署的確定方式
- 在SpringProcessEngineConfiguration中指定其他屬性deploymentMode(定義從與篩選器匹配的資源集確定部署的方式)
- default:將所有資源分組到單個(gè)部署中抗碰,并對(duì)該部署應(yīng)用重復(fù)篩選
- single-resource:為每個(gè)單獨(dú)的資源創(chuàng)建單獨(dú)的部署狮斗,并對(duì)該部署應(yīng)用重復(fù)過(guò)濾。這是用于單獨(dú)部署每個(gè)流程定義的值弧蝇,如果已更改碳褒,則僅創(chuàng)建新的流程定義版本
- resource-parent-folder:為共享同一父文件夾的資源創(chuàng)建單獨(dú)的部署,并對(duì)該部署應(yīng)用重復(fù)篩選看疗。此值可用于為大多數(shù)資源創(chuàng)建單獨(dú)的部署沙峻,但仍可以通過(guò)將它們放在共享文件夾中來(lái)對(duì)其進(jìn)行分組
<bean id="processEngineConfiguration"
class="org.flowable.spring.SpringProcessEngineConfiguration">
...
<property name="deploymentResources" value="classpath*:/flowable/*.bpmn" />
<property name="deploymentMode" value="single-resource" />
</bean>
- 創(chuàng)建一個(gè)子類SpringProcessEngineConfiguration并覆蓋該getAutoDeploymentStrategy(String deploymentMode)方法
單元測(cè)試
與Spring集成時(shí),可以使用標(biāo)準(zhǔn)的Flowable測(cè)試工具輕松測(cè)試業(yè)務(wù)流程
JUnit 5測(cè)試
@ExtendWith(FlowableSpringExtension.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SpringJunitJupiterTest.TestConfiguration.class)
public class MyBusinessProcessTest {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Test
@Deployment
void simpleProcessTest() {
runtimeService.startProcessInstanceByKey("simpleProcess");
Task task = taskService.createTaskQuery().singleResult();
assertEquals("My Task", task.getName());
taskService.complete(task.getId());
assertEquals(0, runtimeService.createProcessInstanceQuery().count());
}
}