接上一篇:
第9篇:Flowable-Modeler集成以及集成代码下载
https://blog.csdn.net/weixin_40816738/article/details/102901208
文章目录
- 一、背景
- 二、方案设计
- 2.1. 流程部署
- 2.2. 模型的转换
- 2.3. 启动流程
- 三、BPMN业务流程文件
- 3.1. 启动flowable-idm
- 3.2. 启动flowable-modeler
- 3.3. BPMNxml的内容
- 四、服务
- 4.1. 服务接口设计
- 4.2. 部署服务实现
- 4.3. 启动服务实现
- 4.4. 部署控制器
- 4.5. 启动控制器
- 4.6. 部署创建流程验证
- 4.7. 运行流程验证
一、背景
本文开始将陆续开始Flowable的API编程操作,慢慢掌握Flowable的常见API操作,参考博客
https://blog.csdn.net/weixin_40816738/article/details/102887854
二、方案设计
2.1. 流程部署
- Flowable的流程部署通过仓库服务来完成部署,仓库服务的接口为RepositoryService。该接口通过创建DeploymentBuilder来完成部署
2.2. 模型的转换
- 部署前我们编辑了一个普通的流程文件,需要加载下改文件来防止文件有错误,把文件转换为BPMNModel来校验,核心接口为BpmnXMLConverter,通过API接口convertToBpmnModel来实现xml到模型的转换
2.3. 启动流程
- 加载完成流程后,我们将流程启动,Flowable的启动接口为runtimeService,运行时服务调用startProcessInstanceByKey启动一个流程,并且返回流程对象ProcessInstance。该对象包含ID,后续我们将经常用到该ID。
三、BPMN业务流程文件
还是使用简单流程来完成业务流程的学习,目前流程只包含一个开始节点/用户任务节点/结束节点。给用户任务节点配置一个分配人为admin.如下图所示:
3.1. 启动flowable-idm
java -jar .\flowable-idm.war
启动如下图所示,默认端口8080:
http://localhost:8080/flowable-idm/
3.2. 启动flowable-modeler
http://localhost:8080/flowable-modeler/
注:删除权限验证证后,直接访问http://localhost:8080/flowable-modeler/
即可
3.3. BPMNxml的内容
- 如下:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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"><process id="test_bpmn" name="测试BPMN模型" isExecutable="true"><documentation>测试BPMN模型</documentation><startEvent id="start" name="开始"></startEvent><endEvent id="end" name="结束"></endEvent><userTask id="testUser" name="用户任务测试" flowable:assignee="admin"><extensionElements><modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete></extensionElements></userTask><sequenceFlow id="sid-8D834F3C-45A8-4C88-9AD1-1AC426CC9002" sourceRef="start" targetRef="testUser"></sequenceFlow><sequenceFlow id="sid-AB59612A-1B33-4FB8-8758-5D773EDF9C44" sourceRef="testUser" targetRef="end"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_test_bpmn"><bpmndi:BPMNPlane bpmnElement="test_bpmn" id="BPMNPlane_test_bpmn"><bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start"><omgdc:Bounds height="30.0" width="30.0" x="210.0" y="60.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end"><omgdc:Bounds height="28.0" width="28.0" x="525.0" y="61.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="testUser" id="BPMNShape_testUser"><omgdc:Bounds height="80.0" width="100.0" x="315.0" y="35.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="sid-8D834F3C-45A8-4C88-9AD1-1AC426CC9002" id="BPMNEdge_sid-8D834F3C-45A8-4C88-9AD1-1AC426CC9002"><omgdi:waypoint x="239.94999779398907" y="75.0"></omgdi:waypoint><omgdi:waypoint x="315.0" y="75.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="sid-AB59612A-1B33-4FB8-8758-5D773EDF9C44" id="BPMNEdge_sid-AB59612A-1B33-4FB8-8758-5D773EDF9C44"><omgdi:waypoint x="414.9499999999903" y="75.0"></omgdi:waypoint><omgdi:waypoint x="525.0" y="75.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>
四、服务
4.1. 服务接口设计
有了xml之后,我们设计web的服务层接口如下
package com.gblfy.service;import org.flowable.engine.runtime.ProcessInstance;import java.util.Map;/*** 流程服务类*/
public interface IFlowService {/*** 部署工作流*/Map<String,Object> createFlow(String filePath);/*** 启动工作流*/ProcessInstance strartFlow(String processKey, Map<String,Object> paras);
}
4.2. 部署服务实现
部署的代码实现如下,代码中我们加入了流程的校验:
@Override
public ProcessInstance strartFlow(String processKey, Map<String, Object> paras) {if (StringUtils.isEmpty(processKey)){return null;}if (null == paras){paras = new HashMap<>();}Deployment deployment = repositoryService.createDeploymentQuery().processDefinitionKey(processKey).singleResult();if (deployment == null){log.error("没有该流程");return null;}return runtimeService.startProcessInstanceByKey(processKey,paras);}
4.3. 启动服务实现
启动服务实现如下,为了防止没有部署就去启动,加入了流程是否存在的检查:
@Override
public ProcessInstance strartFlow(String processKey, Map<String, Object> paras) {if (StringUtils.isEmpty(processKey)){return null;}if (null == paras){paras = new HashMap<>();}Deployment deployment = repositoryService.createDeploymentQuery().processDefinitionKey(processKey).singleResult();if (deployment == null){log.error("没有该流程");return null;}return runtimeService.startProcessInstanceByKey(processKey,paras);}
4.4. 部署控制器
web入口设计简单,主要启动服务,并且返回服务的部署信息,具体实现如下:
@RequestMapping("/create")@ResponseBodypublic Map<String, Object> createFlow() {Map<String, Object> res = new HashMap<>();Map<String, Object> data = new HashMap<>();String flowPath ="C:\\Users\\gblfy\\Desktop\\Workflow\\flowablestudy\\flowablelech10\\src\\main\\resources\\processes\\测试BPMN模型2.bpmn20.xml";Map<String, Object> createRes = flowService.createFlow(flowPath);if (null == createRes) {res.put("msg", "创建流程失败");res.put("res", "0");res.put("data", data);return res;}List<Process> processes = (List<Process>) createRes.get("processes");ArrayList<String> ids = new ArrayList<>();for (Process process : processes) {ids.add(process.getId());}data.put("processKeys", ids);data.put("deployId", ((Deployment) createRes.get("deployment")).getId());res.put("data", data);res.put("msg", "创建流程成功");res.put("res", "1");return res;}
4.5. 启动控制器
启动控制器实现如下:
@RequestMapping("/start")@ResponseBodypublic Map<String, Object> startFlow(@RequestBody @RequestParam(required = false) Map<String, String> paras) {Map<String, Object> res = new HashMap<>();Map<String, String> data = new HashMap<>();if (MapUtils.isEmpty(paras)) {res.put("msg", "启动流程失败");res.put("res", "0");res.put("data", data);return res;}String processKey = paras.get("processKey");if (StringUtils.isEmpty(processKey)) {res.put("msg", "启动流程失败");res.put("res", "0");res.put("data", data);return res;}Map<String, Object> flowParas = new HashMap<>();flowParas.putAll(paras);ProcessInstance processInstance = flowService.strartFlow(processKey, flowParas);if (null == processInstance) {res.put("msg", "启动流程失败");res.put("res", "0");res.put("data", data);return res;}data.put("processId", processInstance.getId());res.put("msg", "启动流程成功");res.put("res", "1");res.put("data", data);return res;}
4.6. 部署创建流程验证
启动服务后,在浏览器中输入http://localhost:8989/flow/create
验证如下:
4.7. 运行流程验证
输入http://localhost:8989/flow/start?processKey=test_bpmn
验证如下:
Flowable部署启动流程完整代码下载
Gitlab链接:https://gitlab.com/gb-heima/flowablestudy/tree/master/flowablelech10
码云地址:https://gitee.com/gb_90/flowable-study/tree/master/flowablelech10
下一篇:
第11篇:Flowable-BPMN部署常见问题没有对ACT_RE_PROCDEF表进行插入操作
https://blog.csdn.net/weixin_40816738/article/details/102902524