素材巴巴 > 程序开发 >

Flowable深入浅出-10 Flowable-BPMN操作流程部署、启动

程序开发 2023-09-08 07:26:08

10 Flowable-BPMN操作流程部署启动

背景

本文开始将陆续开始Flowable的API编程操作,慢慢掌握Flowable的常见API操作,参考博客https://blog.csdn.net/houyj1986/article/details/85494233完成开发环境的搭建,下面我们来完成流程的部署和启动。

方案设计

  1. Flowable的流程部署通过仓库服务来完成部署,仓库服务的接口为RepositoryService。该接口通过创建DeploymentBuilder来完成部署
  2. 部署前我们编辑了一个普通的流程文件,需要加载下改文件来防止文件有错误,把文件转换为BPMNModel来校验,核心接口为BpmnXMLConverter,通过API接口convertToBpmnModel来实现xml到模型的转换
  3. 加载完成流程后,我们将流程启动,Flowable的启动接口为runtimeService,运行时服务调用startProcessInstanceByKey启动一个流程,并且返回流程对象ProcessInstance。该对象包含ID,后续我们将经常用到该ID。

BPMN业务流程文件

还是使用简单流程来完成业务流程的学习,目前流程只包含一个开始节点/用户任务节点/结束节点。给用户任务节点配置一个分配人为admin.如下图所示:
在这里插入图片描述
BPMNxml的内容如下:


 测试BPMN模型
 
 

服务接口设计

有了xml之后,我们设计web的服务层接口如下:


 /*** 流程服务类*/
 public interface IFlowService {/*** 部署工作流*/Map createFlow(String filePath);/*** 启动工作流*/ProcessInstance strartFlow(String processKey, Map paras);
 }

部署服务实现

部署的代码实现如下,代码中我们加入了流程的校验:

@Overridepublic  Map  createFlow(String filePath) {Map res = new HashMap<>();//解析BPMN模型看是否成功XMLStreamReader reader = null;InputStream inputStream = null;try {BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();XMLInputFactory factory = XMLInputFactory.newInstance();inputStream=new FileInputStream(new File(filePath));reader = factory.createXMLStreamReader(inputStream);BpmnModel model = bpmnXMLConverter.convertToBpmnModel(reader);List processes = model.getProcesses();Process curProcess = null;if (CollectionUtils.isEmpty(processes)) {log.error("BPMN模型没有配置流程");return null;}res.put("processes",processes);curProcess = processes.get(0);inputStream=new FileInputStream(new File(filePath));DeploymentBuilder deploymentBuilder = repositoryService.createDeployment().name("TEST_FLOW").addInputStream(filePath,inputStream);Deployment deployment= deploymentBuilder.deploy();res.put("deployment",deployment);log.warn("部署流程 name:"+curProcess.getName()+" key "+deployment.getKey() + " deploy "+deployment);return res;}catch (Exception e){log.error("BPMN模型创建流程异常",e);return null;}finally {try {reader.close();} catch (XMLStreamException e) {log.error("关闭异常",e);}}}
 

启动服务实现

启动服务实现如下,为了防止没有部署就去启动,加入了流程是否存在的检查:

@Override
 public ProcessInstance strartFlow(String processKey, Map 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);}
 

部署控制器

web入口设计简单,主要启动服务,并且返回服务的部署信息,具体实现如下:

 @RequestMapping("/create")@ResponseBodypublic Map createFlow(){Map res =new HashMap<>();Map data = new HashMap<>();String flowPath ="E:\flowablestudy\flowablech5\src\main\resources\flows\测试BPMN模型2.bpmn20.xml";Map createRes = flowService.createFlow(flowPath);if (null == createRes){res.put("msg","创建流程失败");res.put("res","0");res.put("data",data);return res;}List processes =(List)createRes.get("processes");ArrayList 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;}
 

启动控制器

启动控制器实现如下:

@RequestMapping("/start")@ResponseBodypublic Map startFlow(@RequestBody @RequestParam(required = false) Map paras){Map res =new HashMap<>();Map 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 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;}
 

部署创建流程验证

启动服务后,在浏览器中输入http://localhost:8989/flow/create验证如下:
在这里插入图片描述

运行流程验证

输入http://localhost:8989/flow/start?processKey=test_bpmnhttp://localhost:8989/flow/start?processKey=test_bpmn验证如下:
在这里插入图片描述

Flowable部署启动流程完整代码下载

本文的完整代码已经请到我的下载中下载。地址:https://download.csdn.net/download/houyj1986/10909860

版权

版权所有,侵权必究,代码与文章,使用、copy、转载请联系作者。由书山登峰人创作。


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。