SpringBoot2.0.1与flowable工作流引擎如何整合
这篇文章主要介绍了SpringBoot2.0.1与flowable工作流引擎如何整合,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
1.准备依赖,pom.xml文件如下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-----------------------------------------------------------------------------------------------------------------------
2.准备流程文件,ExpenseProcess.bpmn20.xml文件如下
xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef"> class="com.springboot.demo.handler.ManagerTaskHandler"> class="com.springboot.demo.handler.BossTaskHandler">
-----------------------------------------------------------------------------------------------------------------------
3.准备SpringBoot属性文件,application.properties内容如下
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xh_activiti?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
server.port=8082
server.tomcat.uri-encoding=UTF-8
flowable.async-executor-activate=false
-----------------------------------------------------------------------------------------------------------------------
4.编写流程中用到的处理类
包名:com.springboot.demo.handler
类名:ManagerTaskHandler
类内容如下
package com.springboot.demo.handler;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
/**
* @ClassName ManagerTaskHandler
* @Description TODO
* @Author yunshuodeng
* @Date 2019-05-05 09:53
* @Version 1.0
**/
public class ManagerTaskHandler implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
delegateTask.setAssignee("经理");
}
}
包名:com.springboot.demo.handler
类名:BossTaskHandler
类内容如下
package com.springboot.demo.handler;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
/**
* @ClassName BossTaskHandler
* @Description TODO
* @Author yunshuodeng
* @Date 2019-05-05 09:54
* @Version 1.0
**/
public class BossTaskHandler implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
delegateTask.setAssignee("老板");
}
}
-----------------------------------------------------------------------------------------------------------------------
5.编写控制器
包名:com.springboot.demo.controller
类名:ExpenseController
类内容如下:
package com.springboot.demo.controller;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.*;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @ClassName ExpenseController
* @Description TODO
* @Author yunshuodeng
* @Date 2019-05-05 09:57
* @Version 1.0
**/
@Controller
@RequestMapping(value = "expense")
public class ExpenseController {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private RepositoryService repositoryService;
@Autowired
private ProcessEngine processEngine;
/**
* 生成当前流程图
* @param httpServletResponse
* @param processId
*/
@RequestMapping(value = "processDiagram")
public void genProcessDiagram(HttpServletResponse httpServletResponse,String processId){
try {
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
if (StringUtils.isEmpty(processInstance)){
return;
}
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
String instanceId = task.getProcessInstanceId();
List
List
List
for (Execution execution : executionList){
List
activityIdList.addAll(idList);
}
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
ProcessDiagramGenerator processDiagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
InputStream inputStream = processDiagramGenerator.generateDiagram(bpmnModel,"png",activityIdList,flowList,
processEngineConfiguration.getActivityFontName(),
processEngineConfiguration.getLabelFontName(),
processEngineConfiguration.getAnnotationFontName(),
processEngineConfiguration.getClassLoader(),
1.0);
OutputStream out = null;
byte[] buf = new byte[1024];
int legth = 0;
try {
out = httpServletResponse.getOutputStream();
while ((legth = inputStream.read(buf)) != -1) {
out.write(buf, 0, legth);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (out != null) {
out.close();
}
}
}catch (Exception e){
System.out.println(e.getMessage());
}
}
/**
* 审核-拒绝
* @param taskId
* @return
*/
@RequestMapping(value = "reject")
@ResponseBody
public String reject(String taskId){
HashMap
map.put("outcome","驳回");
taskService.complete(taskId,map);
return "reject";
}
/**
* 审核-通过
* @param taskId
* @return
*/
@RequestMapping(value = "apply")
@ResponseBody
public String apply(String taskId){
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (StringUtils.isEmpty(task)){
throw new RuntimeException("流程不存在");
}
HashMap
map.put("outcome","通过");
taskService.complete(taskId,map);
return "processed ok";
}
/**
* 获取审批列表
* @param userId
* @return
*/
@RequestMapping(value = "list")
@ResponseBody
public Object list(String userId){
String temp = "";
List
for (Task task : taskList){
temp += task.toString();
System.out.println(task.toString());
}
return temp;
}
/**
* 开始报销流程
* @param userId 用户ID
* @param money 报销金额
* @param description 描述
* @return
*/
@RequestMapping(value = "add")
@ResponseBody
public String addExpense(String userId,Integer money,String description){
HashMap
map.put("taskUser",userId);
map.put("money",money);
map.put("description",description);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Expense",map);
return "提交成功,流程ID为"+processInstance.getId();
}
}
-----------------------------------------------------------------------------------------------------------------------
6.启动服务,没有报错即可
7.分别访问如下地址进行流程的执行操作
1)提交报销申请
请求地址:http://localhost:8082/expense/add?userId=4&money=2000
返回结果:提交成功,流程ID为5043
说明:
http://localhost本地服务
8082表示本地服务对外提供访问端口
expense表示控制器名称
add表示控制器中的方法,该方法用于开始报销流程,也就是提交报销申请
userId=4表示用户ID为4
money=2000表示报销金额为2000
2)查看待办列表
请求地址:http://localhost:8082/expense/list?userId=4
返回结果:Task[id=5050, name=出差报销]
说明:
http://localhost本地服务
8082表示本地服务对外提供访问端口
expense表示控制器名称
list表示获取待办列表方法名
userId=4表示用户ID为4,获取用户ID为4的待办事项列表
3)对报销申请进行审核,且审核结果为通过
请求地址:http://localhost:8082/expense/apply?taskId=5050
返回结果:processed ok
说明:
http://localhost本地服务
8082表示本地服务对外提供访问端口
expense表示控制器名称
apply表示审核通过方法
taskId表示任务ID,些任务ID是查看待办列表中的某个任务ID
4)生成当前任务状态图
请求地址:http://localhost:8082/expense/processDiagram?processId=5043
返回结果:在浏览器中一张图片
说明:
http://localhost本地服务
8082表示本地服务对外提供访问端口
expense表示控制器名称
processDiagram表示生成图片方法
processId=5043表示流程ID,该流程ID是第1步提交报销申请时返回的流程ID
感谢你能够认真阅读完这篇文章,希望小编分享的"SpringBoot2.0.1与flowable工作流引擎如何整合"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!