springboot特殊问题处理2——springboot集成flowable实现工作流程的完整教程(一)

本文主要是介绍springboot特殊问题处理2——springboot集成flowable实现工作流程的完整教程(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在实际项目开发过程中,流程相关的业务实现采用工作流会异常清晰明了,但是Activity学习成本和开发难度对追求效率的开发工作者来说异常繁琐,但是作为Activity的亲儿子之一的flowable,其轻量化的使用和对应的api会让开发者感受简单,学习成本很低,值得推荐。

本文案基于springboot2.3.12为例讲解,jdk版本要求至少1.8+,mysql为8.0以上。

一.flowable相关官方网址

官方网站(英文):https://www.flowable.com/

第三方中文用户手册(V6.3.0):https://tkjohn.github.io/flowable-userguide/

二.如何集成springboot

1.引入官方jar或者对应springboot的starter
<dependency><groupId>org.flowable</groupId><artifactId>flowable-spring-boot-starter</artifactId><version>${flowable.version}</version>
</dependency>

我这边根据项目需要只引入相关的flowable-engine

        <dependency><groupId>org.flowable</groupId><artifactId>flowable-engine</artifactId><version>6.3.0</version><exclusions><exclusion><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></exclusion><exclusion><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></exclusion></exclusions></dependency>
2. 配置项目需要的数据
  • flowable.properties
flowable.url=jdbc:mysql://10.1.0.223:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
flowable.username=root
flowable.password=123456
flowable.driverClassName=com.mysql.cj.jdbc.Driver
###生成数据表
flowable.initialize=true
flowable.name=flowable
###动态生成流程执行图(定义中文字体为宋体,防止生成的图片资源存在乱码)
flowable.activityFontName=\u5B8B\u4F53
flowable.labelFontName=\u5B8B\u4F53
flowable.annotationFontName=\u5B8B\u4F53
flowable.xml.encoding=UTF-8
  • 项目结构如下 

  • 测试需要的流程图 

 

三.flowable项目正确开发使用流程

1.首先正确配置flowable.properties该文件,默认在启动项目时会生成34张工作流数据表(均已ACT_开头)

2.利用tomcat启动flowable-admin.war,然后用flowable-ui创建对应的bpm文件(或者其他的bpm工具)

3.调用/deployment这个接口,部署已经写好的流程实例,参数参照后台方法说明传递即可

4.分别查看act_re_deployment,act_re_procdef和act_ge_bytearray数据表,如果生成了相关数据即代表部署成功

5.最后就可以在相关模块创建任务开始动态执行流程

四.flowable流程业务实现以及部分关键代码展示

以下关键代码,需要特意说明的是:

  • ProcessEngine是flowable提供对公开BPM和工作流操作的所有服务的访问关键对象。
  • FlowProcessDiagramGenerator是flowable生成流程实例图片的关键,继承自
    org.flowable.image.impl.DefaultProcessDiagramGenerator类
1.流程部署
    /*** 1.部署流程** @return*/@GetMapping("/deployment")public String deploymentFlowable() {RepositoryService repositoryService = processEngine.getRepositoryService();Deployment deployment = repositoryService.createDeployment().addClasspathResource("flowable_xml/test_flowable.bpmn20.xml")//类别.category("审批类").name("领导审批").deploy();return ResponseResult.ok(deployment);}
2. 查询流程定义
    /*** 2.查询流程定义** @return*/@GetMapping("/queryDeployment")public String queryFlowableDeploy() {RepositoryService repositoryService = processEngine.getRepositoryService();//查询所有定义的流程List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();//查询单个定义的流程/*ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId("5").singleResult();*/
//        System.out.println("Found process definition : " + processDefinition.getName());return ResponseResult.ok(list);}
3.启动流程实例
    /*** 3.启动流程实例** @return*/@RequestMapping("/start/instance")public String startProcessInstance() {RuntimeService runtimeService = processEngine.getRuntimeService();//要启动流程实例,需要提供一些初始化流程变量,自定义Map<String, Object> variables = new HashMap<String, Object>(0);variables.put("employee", "工作组");variables.put("nrOfHolidays", 8);variables.put("description", "请假");ProcessInstance processInstance =runtimeService.startProcessInstanceByKey("leader_approval_key", variables);return ResponseResult.ok(processInstance.getName());}
4.通过流程执行人员查询任务和流程变量
    /*** 通过流程人员定义查询任务和流程变量** @return*/@RequestMapping("/query/task")public String queryProcessInstance() {TaskService taskService = processEngine.getTaskService();//通过组查询任务表
//        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();//通过人查询单个任务Task task = taskService.createTaskQuery().taskAssignee("小王").singleResult();//通过任务id查询流程变量Map<String, Object> processVariables = taskService.getVariables(task.getId());return ResponseResult.ok(processVariables);}
5.通过任务id完成任务
    /*** 通过任务id完成任务** @return*/@RequestMapping("/complete/task")public String completeTask(String taskId) {TaskService taskService = processEngine.getTaskService();//领导审批提交的表达信息Map<String, Object> variables = new HashMap<String, Object>(0);taskService.complete(taskId, variables);return ResponseResult.ok();}
 6.通过流程执行人或者审批人查询审批历史记录
    /*** 通过审批人获取历史任务数据** @param name* @return*/@RequestMapping("/history/task")public String getHistoryTask(@RequestParam("name") String name) {HistoryService historyService = processEngine.getHistoryService();//历史任务流程——流程idList<HistoricActivityInstance> activities =historyService.createHistoricActivityInstanceQuery().processInstanceId("2501").finished().orderByHistoricActivityInstanceEndTime().asc().list();//历史任务List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskAssignee(name).list();return ResponseResult.ok(list.toString());}
 7.通过流程id查询流程执行图(多种获取方式)
/*** 通过流程id获取流程资源** @return*/@RequestMapping("/process/resource")public void getProcessResource(HttpServletResponse response) throws IOException {RepositoryService repositoryService = processEngine.getRepositoryService();/*ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId("5085").processDefinitionId("defect_report_flowable:1:5088")
//                .processDefinitionKey("leader_approval_key")
//                .deploymentId("5").singleResult();*/BpmnModel bpmnModel = repositoryService.getBpmnModel("defect_report_flowable:1:4");InputStream imageStream = processDiagramGenerator.generateDiagram(bpmnModel);/*String diagramResourceName = processDefinition.getDiagramResourceName();InputStream imageStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);*/FileOutputStream fos = new FileOutputStream("D:\\data\\22222.png");byte[] b = new byte[1024];int leng = -1;while ((leng = imageStream.read(b)) != -1) {fos.write(b, 0, leng);}fos.flush();imageStream.close();fos.close();/*//文件流直接写出ByteArrayOutputStream baos = new ByteArrayOutputStream();OutputStream os = response.getOutputStream();int ch = 0;while (-1 != (ch = imageStream.read())) {baos.write(ch);}os.write(baos.toByteArray());imageStream.close();baos.close();os.close();*/}

五.其他相关的功能和问题持续更新,有问题私信 

1.生成流程实例图片的关键代码
@Service
public class FlowProcessDiagramGenerator extends DefaultProcessDiagramGenerator {private static final String IMAGE_TYPE = "png";@Value("${flowable.activityFontName}")private String activityFontName;@Value("${flowable.labelFontName}")private String labelFontName;@Value("${flowable.annotationFontName}")private String annotationFontName;@Value("${flowable.xml.encoding}")private String encoding;@Autowiredprivate ProcessEngine processEngine;/*** 生成执行动态图片流** @param processDefinitionId 流程定义的id——xml文件规固定的key* @param businessKey* @return*/public InputStream generateActiveDiagram(String processDefinitionId, String businessKey) {RuntimeService runtimeService = processEngine.getRuntimeService();HistoryService historyService = processEngine.getHistoryService();RepositoryService repositoryService = processEngine.getRepositoryService();//1.获取当前的流程定义ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionId(processDefinitionId)
//                .processInstanceId(processInstanceId).processInstanceBusinessKey(businessKey).singleResult();//流程实例执行的实例idString processId = null;List<String> activeActivityIds = new ArrayList<>();List<String> highLightedFlows = new ArrayList<>();//3. 获取流程定义id和高亮的节点idif (processInstance != null) {//3.1. 正在运行的流程实例processId = processInstance.getProcessInstanceId();//2.获取所有的历史轨迹线对象List<HistoricActivityInstance> historicSquenceFlows = historyService.createHistoricActivityInstanceQuery()
//                .processDefinitionId(processInstanceId).processInstanceId(processId).activityType(BpmnXMLConstants.ELEMENT_SEQUENCE_FLOW).list();historicSquenceFlows.forEach(historicActivityInstance -> highLightedFlows.add(historicActivityInstance.getActivityId()));activeActivityIds = runtimeService.getActiveActivityIds(processId);} else {//3.2. 已经结束的流程实例HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processDefinitionId)
//                    .processInstanceId(processId).processInstanceBusinessKey(businessKey).singleResult();if(historicProcessInstance == null){throw new MessageCodeException(MessageCode.FLOWABLE_PROCESS_IS_RELEASE_SUCCESS);}processId = historicProcessInstance.getId();//3.3. 获取结束节点列表List<HistoricActivityInstance> historicEnds = historyService.createHistoricActivityInstanceQuery().processInstanceId(processId).activityType(BpmnXMLConstants.ELEMENT_EVENT_END).list();List<String> finalActiveActivityIds = activeActivityIds;historicEnds.forEach(historicActivityInstance -> finalActiveActivityIds.add(historicActivityInstance.getActivityId()));}//4. 获取bpmnModel对象BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);//模型 活动节点 高亮线return generateDiagram(bpmnModel, IMAGE_TYPE, activeActivityIds,highLightedFlows, activityFontName, labelFontName, annotationFontName,null, 1.0);}/*** 生成工作流程图** @param bpmnModel 模型* @return*/public InputStream generateDiagram(BpmnModel bpmnModel) {return generateDiagram(bpmnModel, IMAGE_TYPE, activityFontName,labelFontName, annotationFontName,null, 1.0);}

这篇关于springboot特殊问题处理2——springboot集成flowable实现工作流程的完整教程(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1085272

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法