一.Flowable简介
Flowable是BPMN的一个基于java的软件实现,不过Flowable不仅仅包括BPMN,还有DMN决策表和CMMN Case管理引擎,并且有自己的用户管理、微服务API等一系列功能,是一个服务平台。
二.Springboot整合Flowable
接下来我们介绍Spring Boot集成flowable
1.引入依赖:
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.5.0</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
2.添加配置:
server:
port: 8080
spring:
application:
name: flowable
# 数据源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://IP:port/flowable?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username:
password:
flowable:
#关闭定时任务JOB
async-executor-activate: false
logging:
level:
# com.business.workflow.wrapper.mapper: debug #自己项目的mapper
org.flowable.engine.impl.persistence.entity.*: debug
org.flowable.task.service.impl.persistence.entity.*: debug
3.启动成功后,可以看到数据库创建了很多的表
4.springboot与flowable整合完成后,就可以进行工作流开发
flowable采用业界标准BPMN2.0的XML来描述需要定义的工作流,我这边是基于ldea的插件进行流程图的绘制,需要放在resources/processes下 ,框架启动时会默认加载该目录下配置加载到数据库进行持久化
5.基于用户增加流程
/**
* 创建流程
*
* @return
*/
@GetMapping("add")
public String addExpense(String userId) {
// 设置流程启动用户
identityService.setAuthenticatedUserId(userId);
HashMap<String, Object> map = new HashMap<>();
map.put("taskUser", userId);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("liuchengtu", map);
String startUserId = processInstance.getStartUserId();
return "提交成功,流程ID为:" + processInstance.getId() + ", 用户ID为:" + startUserId;
}
6.根据用户ID查询发起的流程实例列表
/**
* 我发起的流程实例列表
*
* @param userId
* @return 流程实例列表
*/
@GetMapping("myTasks")
public List<HistoricProcessInstance> getMyStartProcint(String userId) {
List<HistoricProcessInstance> list = historyService
.createHistoricProcessInstanceQuery()
.startedBy(userId)
.orderByProcessInstanceStartTime()
.asc()
.list();
return list;
}
7.基于taskID查询流程图
/**
* 查询流程图
*
* @param httpServletResponse
* @param processId
* @throws Exception
*/
@RequestMapping(value = "processDiagram")
public void genProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws Exception {
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
//流程走完的不显示图
if (pi == null) {
return;
}
Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
//使用流程实例ID,查询正在执行的执行对象表,返回流程实例对象
String InstanceId = task.getProcessInstanceId();
List<Execution> executions = runtimeService
.createExecutionQuery()
.processInstanceId(InstanceId)
.list();
//得到正在执行的Activity的Id
List<String> activityIds = new ArrayList<>();
List<String> flows = new ArrayList<>();
for (Execution exe : executions) {
List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
activityIds.addAll(ids);
}
//获取流程图
BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
System.out.println("流程图序列化以后结果 " + JSON.toJSONString(bpmnModel));
ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows, engconf.getActivityFontName(), engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0, true);
OutputStream out = null;
byte[] buf = new byte[1024];
int legth = 0;
try {
out = httpServletResponse.getOutputStream();
while ((legth = in.read(buf)) != -1) {
out.write(buf, 0, legth);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
8.同意任务
/**
* 通过/拒绝任务
*
* @param taskId
* @return
*/
@GetMapping("apply")
public String apply(String taskId) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task == null) {
return "流程不存在";
}
taskService.complete(taskId);
return "审批通过:";
}
9.再次查看流程图
10.审核查看自己的审核任务
/**
* 获取指定用户组流程任务列表
*
* @param group
* @return
*/
@GetMapping("listGroup")
public Object listGroup(String group) {
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(group).list();
// 获取当前任务,怎么嵌套任务角色之类的
// List<Task> tasks = taskService.createTaskQuery().taskCandidateUser().list();
// List<Task> tasks = taskService.createTaskQuery().list();
return tasks.toString();
}