Flowable高级篇 - Flowable的API

Flowable工作流引擎通过其API提供与流程交互的主要方式,包括RepositoryService(资源管理)、RuntimeService(流程运行管理)、TaskService(任务管理)等。RepositoryService用于部署和管理流程定义,RuntimeService启动流程实例并处理流程变量,TaskService则关注任务的分配和完成。此外,还有IdentityService管理用户和组,HistoryService记录历史数据,FormService处理表单,ManagementService提供引擎管理和作业操作,而DynamicBpmnService允许动态修改流程定义。

flowable的中文手册

描述

引擎API是与Flowable交互的最常用手段。总入口点是ProcessEngine。像配置章节中介绍的一样,ProcessEngine可以使用多种方式创建。使用ProcessEngine,可以获得各种提供工作流/BPM方法的服务。ProcessEngine与服务对象都是线程安全的,因此可以在服务器中保存并共用同一个引用。

示例代码: 

ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();
ManagementService managementService = processEngine.getManagementService();
IdentitySer
<think> 我们正在讨论Spring Boot 3.4.4集成Flowable后根据流程生成流程图的方法。 关键点:使用Flowable提供的API生成流程图的图片。 步骤: 1. 确保已正确集成Flowable Spring Boot Starter。 2. 获取流程定义。 3. 使用Flowable的DiagramGenerator生成流程图。 具体实现: - 注入RepositoryService来获取流程定义。 - 使用ProcessDiagramGenerator生成流程图,并输出为图片流(如PNG)。 注意:Flowable 6.x版本中,ProcessDiagramGenerator是生成流程图的关键接口。 示例代码: 我们可以创建一个REST控制器,提供一个端点,传入流程定义ID,返回流程图图片。 依赖:确保pom.xml中包含flowable-spring-boot-starter。 实现步骤: (1) 获取流程定义对象。 (2) 获取流程定义的BPMN XML资源,通过解析它来获取图形信息。 (3) 生成流程图并写入响应输出流。 具体代码示例: </think> ### Spring Boot 3.4.4 集成 Flowable 生成流程图的方法 在 Spring Boot 3.4.4 中集成 `flowable-spring-boot-starter` 后,可通过以下步骤生成流程图: #### 1. **核心依赖配置** 确保 `pom.xml` 包含 Flowable 依赖: ```xml <dependency> <groupId>org.flowable</groupId> <artifactId>flowable-spring-boot-starter</artifactId> <version>6.8.0</version> <!-- 兼容Spring Boot 3.4.4的最新版本 --> </dependency> ``` #### 2. **流程图生成服务类** 创建服务类使用 Flowable 的 `ProcessDiagramGenerator`: ```java import org.flowable.bpmn.model.BpmnModel; import org.flowable.engine.RepositoryService; import org.flowable.image.ProcessDiagramGenerator; import org.springframework.stereotype.Service; import java.io.InputStream; @Service public class ProcessDiagramService { private final RepositoryService repositoryService; private final ProcessDiagramGenerator diagramGenerator; public ProcessDiagramService(RepositoryService repositoryService, ProcessDiagramGenerator diagramGenerator) { this.repositoryService = repositoryService; this.diagramGenerator = diagramGenerator; } /** * 生成流程图PNG图片流 * @param processDefinitionId 流程定义ID */ public InputStream generateDiagram(String processDefinitionId) { // 获取BPMN模型 BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId); // 生成流程图(默认无高亮) return diagramGenerator.generatePngDiagram(bpmnModel); } } ``` #### 3. **REST 接口实现** 创建控制器提供流程图访问端点: ```java import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; @RestController public class DiagramController { private final ProcessDiagramService diagramService; public DiagramController(ProcessDiagramService diagramService) { this.diagramService = diagramService; } @GetMapping(value = "/process-diagram/{processDefinitionId}", produces = MediaType.IMAGE_PNG_VALUE) public void getProcessDiagram(@PathVariable String processDefinitionId, HttpServletResponse response) { try (InputStream is = diagramService.generateDiagram(processDefinitionId)) { // 将图片流写入HTTP响应 response.setContentType("image/png"); IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (Exception e) { throw new RuntimeException("流程图生成失败: " + e.getMessage()); } } } ``` #### 4. **流程图高级配置** ##### (1) 高亮当前流程节点 ```java // 在ProcessDiagramService中添加 public InputStream generateHighlightedDiagram(String processInstanceId) { HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult(); BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId()); // 获取当前活动节点 List<String> activeNodes = runtimeService.getActiveActivityIds(processInstanceId); // 生成带高亮的流程图 return diagramGenerator.generateDiagram( bpmnModel, activeNodes, // 高亮节点 Collections.emptyList(), "宋体", "宋体", "宋体" ); } ``` ##### (2) 自定义生成器配置 在 `application.yml` 中配置字体(避免中文乱码): ```yaml flowable: process-diagram: activity-font-name: 宋体 label-font-name: 宋体 annotation-font-name: 宋体 ``` #### 5. **访问流程图** 启动应用后访问(示例): ``` http://localhost:8080/process-diagram/myProcess:1:1234 ``` 将显示 `myProcess` 流程定义的流程图 PNG 图片。 #### 注意事项 1. **流程定义获取**:需先部署流程(通过 `src/main/resources/processes` 或 API 部署) 2. **字体问题**:Linux 环境需安装中文字体库 3. **版本兼容性**:Flowable 6.8.0+ 已适配 Spring Boot 3.x 4. **性能优化**:对复杂流程图建议添加缓存机制 > 流程图生成原理:Flowable 通过解析 BPMN 2.0 XML 定义,使用 SVG 转换引擎生成图片[^1]。核心类 `DefaultProcessDiagramGenerator` 实现了坐标计算和图形渲染逻辑[^2]。 --- ### 相关问题 1. 如何在 Flowable 中实现自定义流程节点图标? 2. 如何解决 Flowable 流程图生成时的中文乱码问题? 3. Flowable 与 Camunda 在流程可视化方面的主要区别是什么? 4. 如何为已完成的流程实例生成带执行路径标记的流程图? [^1]: Flowable 官方文档 - BPMN 可视化章节 [^2]: `org.flowable.image.impl.DefaultProcessDiagramGenerator` 源码分析
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值