Flowable整合Spring
1. 引入jar包依赖
pom.xml中引入flowable和spring的相关依赖(也添加了mysql数据库连接驱动和druid连接池),如下:
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version> 5.0.9.RELEASE </version>
<scope>provided</scope>
</dependency>
2. flowable配置
在src/main/resources下创建flowable-context.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="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8&serverTimezone=GMT"></property>
<property name="username" value="root"></property>
<property name="password" value="123456" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
</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>
3. Demo开发
新建测试类:FlowableTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:flowable-context.xml")
public class FlowableTest {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private HistoryService historyService;
@Autowired
private ManagementService managementService;
/***
* 部署流程
*/
@Test
public void testDeploy() {
Deployment deployment = repositoryService.createDeployment()
.name("testDeploy")
.addClasspathResource("com/flowable/hello/leave.bpmn").deploy();
System.out.println("Deploy successfullly, deployId:"
+ deployment.getId() + "; deployName:" + deployment.getName());
}
/**
* 查询流程定义
*/
@Test
public void queryProcessDefinitionTest() {
ProcessDefinition processDefinition = repositoryService
.createProcessDefinitionQuery().deploymentId("35001") // 用上一步中的结果
.singleResult();
System.out.println("Found process definition : "
+ processDefinition.getName() + "; key:"
+ processDefinition.getKey() + ";id:"
+ processDefinition.getId());
}
/**
* 启动流程
*/
@Test
public void startProcessInstanceTest() {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("employee", "jack");
variables.put("nrOfHolidays", 3);
variables.put("description", "回家看看");
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey("leave", variables);
}
/**
* 查询并完成任务
*/
@Test
public void queryAndCompleteTask() {
// 查询
List<Task> tasks = taskService.createTaskQuery().taskAssignee("c")
.list();
System.out.println("You have " + tasks.size() + " tasks:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ") " + tasks.get(i).getName());
}
// 选择
Task task = tasks.get(0);
Map<String, Object> processVariables = taskService.getVariables(task
.getId());
System.out.println(processVariables.get("employee") + " wants "
+ processVariables.get("nrOfHolidays")
+ " of holidays. Do you approve this?");
// 完成
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("approved", true);
taskService.complete(task.getId(), variables);
}
/**
* 查询历史数据
*/
@Test
public void queryHistoryData() {
List<HistoricTaskInstance> list = historyService
.createHistoricTaskInstanceQuery().finished().taskAssignee("a")
.list();
for (HistoricTaskInstance task : list) {
System.out.println(task.getId() + " took "
+ task.getDurationInMillis() + " milliseconds");
System.out.println("======================================");
}
}
}
4. 源码下载
源码下载地址:https://download.csdn.net/download/huangjun0210/10834459