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

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

找完工作该补充的东西

首先: 锻炼身体,包括乒乓球,羽毛球,都必须练习,学习,锻炼身体等是一个很重要的与人交际沟通的方式; 打牌,娱乐:会玩是一个人很重要的交际沟通的法宝; 摄影:这个是一个兴趣爱好,也是提高自己的审美,生活品质,当然也是与人沟通的重要途径; 做饭:这个的话就是对自己,对朋友非常有益的一件事情;

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

2024.6.24 IDEA中文乱码问题(服务器 控制台 TOMcat)实测已解决

1.问题产生原因: 1.文件编码不一致:如果文件的编码方式与IDEA设置的编码方式不一致,就会产生乱码。确保文件和IDEA使用相同的编码,通常是UTF-8。2.IDEA设置问题:检查IDEA的全局编码设置和项目编码设置是否正确。3.终端或控制台编码问题:如果你在终端或控制台看到乱码,可能是终端的编码设置问题。确保终端使用的是支持你的文件的编码方式。 2.解决方案: 1.File -> S

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM