<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.8version>
<relativePath/>
parent>
<groupId>com.hxgroupId>
<artifactId>Flowable_SpringBoot2artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>Flowable_SpringBoot2name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.mysqlgroupId>
<artifactId>mysql-connector-jartifactId>
<version>8.0.32version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>1.5.9.RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>org.flowablegroupId>
<artifactId>flowable-spring-boot-starterartifactId>
<version>6.6.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.24version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
dependencies>
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/flowable2?serverTimeZone=UTC&nullCatalogMeansCurrent=true
username: root
password: admin
hikari:
minimum-idle: 5
idle-timeout: 600000
maximum-pool-size: 10
auto-commit: true
pool-name: MyHikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: SELECT 1
flowable:
# 关闭定时任务JOB
async-executor-activate: false
# 将databaseSchemaUpdate设置为true。当flowable发现数据库不一致的时候,会自动将数据库表结构更新到最新版本。
database-schema-update: true
server:
port: 8888
processes目录下的任何BPMN 2.0流程定义都会被自动部署。创建processes目录,并在其中创建示例流程定义(命名为one-task-process.bpmn20.xml)。
cases目录下的任何CMMN 1.1事例都会被自动部署。
forms目录下的任何Form定义都会被自动部署。
在SpringBoot项目中会自动部署存储在resource\processes目录下的流程定义。(但是只有在第一次启动的时候才会自动部署,而后续新增的部署文件不会部署。)
但是在我的实际测试过程中,发现在调用部署方法的时候,会重新部署processes目录下的流程定义。我们可以手动来条用deploy方法即可。
@Test
void test_deploy(){
Deployment deploy = repositoryService.createDeployment()
.name("ManualDeployment").deploy();
System.out.println(deploy.getId());
}
如果存在不希望被调用的流程定义,可以不放到processes目录下。
@Autowired
private RepositoryService repositoryService;
@Test
void deploy(){
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("processes/出差申请流程.bpmn20.xml")
.name("出差申请流程2023").deploy();
System.out.println("deployment.getId() = " + deployment.getId());
System.out.println("deployment.getName() = " + deployment.getName());
}
@Autowired
private RuntimeService runtimeService;
@Test
void startFlow() {
Map<String, Object> params = new HashMap<>();
params.put("assignee0", "whx");
params.put("assignee1", "huathy");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qjlc23", params);
}
@Autowired
private TaskService taskService;
@Test
void completeTask() {
Task task = taskService.createTaskQuery()
.processDefinitionKey("qjlc23")
.taskCandidateOrAssigned("whx")
.singleResult();
if (task != null) {
taskService.complete(task.getId());
System.out.println(task.getAssignee()+"-完成任务-"+task.getId());
}
}
多人会签是指一个任务需要多个人来审批。多人或签是指的=一个任务需要多个人中的一个审批通过即可。
package com.example.flowdemo.listener;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* @author Huathy
* @date 2023-01-27 08:47
* @description
*/
@Component("mulitiInstCompleteTask")
public class MultiInstCompleteTask implements Serializable {
/**
* 完成任务时需要触发的方法
* @param execution
* @return true/false 表示会签任务是否结束
*/
public boolean completeTask(DelegateExecution execution){
System.out.println("总会签数量:"+execution.getVariable("nrOfInstances"));
System.out.println("当前完成会签数量:"+execution.getVariable("nrOfActiveInstances"));
System.out.println("当前完成会签任务数量:"+execution.getVariable("nrOfCompletedInstances"));
// 上面的这些nr*变量是由flowable提供的。而下面的flag是我们自定义的。
Boolean flag = (Boolean) execution.getVariable("flag");
System.out.println("当前意见:"+flag);
return flag;
}
}
package com.example.flowdemo.listener;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* @author Huathy
* @date 2023-01-27 08:57
* @description
*/
@Component("mulitiInstTaskListener")
public class MultiInstTaskListener implements Serializable {
public void completeListener(DelegateExecution execution){
System.out.println("任务ID execution.getId() = " + execution.getId());
System.out.println("execution.getVariable(\"persons\") = " + execution.getVariable("persons"));
System.out.println("execution.getVariable(\"person\") = " + execution.getVariable("person"));
}
}
这里我们配置的是串行执行的,所以第一次启动流程只会创建一个任务。如果配置的是并行执行的,那么会在启动任务的时候创建所有的任务。