本文开始将陆续开始Flowable的API编程操作,慢慢掌握Flowable的常见API操作,参考博客https://blog.csdn.net/houyj1986/article/details/85494233完成开发环境的搭建,下面我们来完成流程的部署和启动。
RepositoryService
。该接口通过创建DeploymentBuilder
来完成部署BpmnXMLConverter
,通过API接口convertToBpmnModel
来实现xml到模型的转换runtimeService
,运行时服务调用startProcessInstanceByKey
启动一个流程,并且返回流程对象ProcessInstance
。该对象包含ID,后续我们将经常用到该ID。还是使用简单流程来完成业务流程的学习,目前流程只包含一个开始节点/用户任务节点/结束节点。给用户任务节点配置一个分配人为admin.如下图所示:
BPMNxml的内容如下:
<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">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>
有了xml之后,我们设计web的服务层接口如下:
/**
* 流程服务类
*/
public interface IFlowService {
/**
* 部署工作流
*/
Map<String,Object> createFlow(String filePath);
/**
* 启动工作流
*/
ProcessInstance strartFlow(String processKey, Map<String,Object> paras);
}
部署的代码实现如下,代码中我们加入了流程的校验:
@Override
public Map<String,Object> createFlow(String filePath) {
Map<String,Object> 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<Process> 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<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);
}
web入口设计简单,主要启动服务,并且返回服务的部署信息,具体实现如下:
@RequestMapping("/create")
@ResponseBody
public Map<String,Object> createFlow(){
Map<String,Object> res =new HashMap<>();
Map<String,Object> data = new HashMap<>();
String flowPath ="E:\\flowablestudy\\flowablech5\\src\\main\\resources\\flows\\测试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;
}
启动控制器实现如下:
@RequestMapping("/start")
@ResponseBody
public 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;
}
启动服务后,在浏览器中输入http://localhost:8989/flow/create验证如下:
输入http://localhost:8989/flow/start?processKey=test_bpmnhttp://localhost:8989/flow/start?processKey=test_bpmn验证如下:
本文的完整代码已经请到我的下载中下载。地址:https://download.csdn.net/download/houyj1986/10909860
如果您觉得该文章对您有帮助,欢迎打赏作者,谢谢。
版权所有,侵权必究,代码与文章,使用、copy、转载请联系作者。由书山登峰人创作。