1、启动springboot,resource/processes/下的bpmn文件自动载入,数据库表变动如下:
此时,流程实例尚未启动,所以_RU_相关表都为空。
2、调用方法:
package com.saicfinance.itsmflowable.controller;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/bg")
public class BgController {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private RepositoryService repositoryService;
@Qualifier("processEngine") //样例中没有
@Autowired
private ProcessEngine processEngine;
@GetMapping(value = "/add")
public String addExpense() {
//启动流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("bg"); //样例的流程key
return "提交成功.流程Id为:" + processInstance.getId();
}
}
此时就会正常启动流程实例,并返回流程ID:bad8ad4d-abaf-11ea-a6cf-d8f2ca5b1ea5。查看一下RU数据库表:
ACT_RU_ACTINST、ACT_RU_EXECUTION、ACT_RU_IDENTITYLINK、ACT_RU_TASK出现记录(表数据内容太多,略)。
3、查看当前的流程图,就可看到流程实例已经创建完成:
@GetMapping(value = "/processDiagram")
public void genProcessDiagram(HttpServletResponse httpServletResponse, @RequestParam(value="processId") String processId) throws Exception {
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
//流程走完的不显示图
if (pi == null) {
System.out.println("pi == null");
return;
}
Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
//使用流程实例ID,查询正在执行的执行对象表,返回流程实例对象
String InstanceId = task.getProcessInstanceId();
List<Execution> executions = runtimeService
.createExecutionQuery()
.processInstanceId(InstanceId)
.list();
//得到正在执行的Activity的Id
List<String> activityIds = new ArrayList<>();
List<String> flows = new ArrayList<>();
for (Execution exe : executions) {
List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
activityIds.addAll(ids);
}
//获取流程图
BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows,
engconf.getActivityFontName(), engconf.getLabelFontName(), engconf.getAnnotationFontName(),
engconf.getClassLoader(), 1.0, false); //这里比例子上,多了最后一个true参数
OutputStream out = null;
byte[] buf = new byte[1024];
int legth = 0;
try {
out = httpServletResponse.getOutputStream();
while ((legth = in.read(buf)) != -1) {
out.write(buf, 0, legth);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}