Camunda的使用有两种方法,一种是JAVA API,一种是RESET API
个人理解JAVA API是从上到下通过JAVA类一级一级调用,RESET API则是从下到上通过注解+接口一层一层实现,如果你只是使用Camunda实现一些简单的操作,用JAVA API就能满足。
1.如何部署流程?
@Override
public Deployment createDeployment(String path) throws IOException {
File file = new File(path);
// 文件发布 其中param 是文件的路径信息
Deployment deploy = repositoryService.createDeployment().addInputStream("bpmnProcessId",
new FileInputStream(file)).deploy();
return deploy;
}
流程定义中字段
processDefinitionKey
的值来自于bpmn文件中的id值,
processDefinitionName
来自于name值。
<bpmn2:process id="loanApproval" name="Loan Approval" isExecutable="true">
有时候我们需要在部署直接做一些校验,那么可以利用下面这个静态方法:BpmnModelInstance modelInstance = Bpmn.readModelFromStream(is)
通过BpmnModelInstance我们可以做一些简单或者深入的判断,比如判断流程有没有结束事件:
modelInstance.getModelElementsByType(EndEvent.class).isEmpty()
判断启动任务有没有设置表单
Collection startEvents = modelInstance.getModelElementsByType(StartEvent.class);
for (StartEvent startEvent : startEvents) {
Collection flows = startEvent.getIncoming();
if (flows.isEmpty()) {
if (StringUtils.isEmpty(startEvent.getCamundaFormKey())) {
//提醒用户设置启动表单
}
}
}
2.如何查看流程定义
(1)查看单个流程
@Override
public CommonResult<Map> getProcessDefinitionById(String processDefinitionId){
ProcessDefinition definition = repositoryService.getProcessDefinition(processDefinitionId);
// 构建Map
Map<String, Object> data = new HashMap<String, Object>();
data.put("result", definition);
return CommonResult.success(data);
}
(2)查看流程定义列表
@Override
public CommonResult<Map> getProcesses(int currentPage, int pageSize){
// 查询流程定义
// 遍历查询结果
PageHelper.startPage(currentPage, pageSize);
//下面为链式调用
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
.latestVersion()
.orderByProcessDefinitionVersion()
.asc()
.list();// 返回一个集合列表,封装流程定义
// long count = repositoryService.createProcessDefinitionQuery().latestVersion().count();
// 取分页结果
PageInfo<ProcessDefinition> pageInfo = new PageInfo<>(list);
// 取记录总数
long total= pageInfo.getTotal();
System.out.println(list);
List<ProcessModel> processDefinitionList =new ArrayList<>();
for(ProcessDefinition processDefinition:list){
ProcessModel processDefinitionLike = new ProcessModel();
processDefinitionLike.setDeploymentId(processDefinition.getDeploymentId());
processDefinitionLike.setProcessDefinitionName(processDefinition.getName());
processDefinitionLike.setProcessDefinitionVersion(processDefinition.getVersion());
processDefinitionLike.setProcessDefinitionDescription(processDefinition.getDescription());
processDefinitionLike.setProcessDefinitionKey(processDefinition.getKey());
processDefinitionLike.setProcessDefinitionId(processDefinition.getId());
processDefinitionLike.setProcessDefinitionResourceName(processDefinition.getResourceName());
processDefinitionList.add(processDefinitionLike);
}
// 构建Map
Map<String, Object> data = new HashMap<String, Object>();
data.put("result", processDefinitionList);
data.put("total", total);
data.put("currentPage", currentPage);
return CommonResult.success(data);
}
流程定义key值相同的情况下,版本是从1开始逐次升级的,流程定义的id是【key:版本:生成ID】
流程定义key值可以理解为,一个流程只有1个固定key值,你每修改一次流程定义,key值不变,但流程ID,版本号会改变,流程ID会重新生成,版本号会在之前基础上加1;
.latestVersion()
实际是通过key值对列表进行了过滤,只保留最后一版的流程定义。
(3)获取流程定义的文件资源-XML
@Override
public CommonResult<Map> getProcessModel(String processDefinitionId){
// BpmnModelInstance bpmnModelInstance = repositoryService.getBpmnModelInstance(processDefinitionId);
InputStream processModelIn = null;
try {
processModelIn = repositoryService.getProcessModel(processDefinitionId);
byte[] processModel = IoUtil.readInputStream(processModelIn, "processModelBpmn20Xml");
ProcessDefinitionDiagramDto processDefinitionDiagramDto = ProcessDefinitionDiagramDto
.create(processDefinitionId, new String(processModel, "UTF-8"));
System.out.println(processDefinitionDiagramDto);
Map<String, Object> data = new HashMap<String, Object>();
data.put("bpmn20Xml", processDefinitionDiagramDto);
data.put("id", processDefinitionId);
return CommonResult.success(data);
} catch (AuthorizationException e) {
throw e;
} catch (ProcessEngineException e) {
throw new InvalidRequestException(Response.Status.BAD_REQUEST,
e, "No matching definition with id " + processDefinitionId);
} catch (UnsupportedEncodingException e) {
throw new RestException(Response.Status.INTERNAL_SERVER_ERROR, e);
} finally {
IoUtil.closeSilently(processModelIn);
}
}
(4)获取流程
public void viewPic() throws IOException {
// 将生产的图片放到文件夹下
String deploymentId = "401";// TODO
// 获取图片资源名称
List<String> list = processEngine.getRepositoryService()
.getDeploymentResourceNames(deploymentId);
// 定义图片资源名称
String resourceName = "";
if (list != null && list.size() > 0) {
for (String name : list) {
if (name.indexOf(".png") >= 0) {
resourceName = name;
}
}
}
// 获取图片的输入流
InputStream in = processEngine.getRepositoryService()
.getResourceAsStream(deploymentId, resourceName);
File file = new File("D:/" + resourceName);
// 将输入流的图片写到D盘下
FileUtils.copyInputStreamToFile(in, file);
}
3.获取流程下活动实例的节点
@Override
public CommonResult<Map> getActivityStatistics(String processDefinitionId){
List<ActivityStatistics> activityStatistics = managementService
.createActivityStatisticsQuery(processDefinitionId)
.list();
Map<String, Object> data = new HashMap<String, Object>();
data.put("result", activityStatistics);
return CommonResult.success(data);
}
4.获取流程下活动实例
@Override
public CommonResult<Map> getActiveInstances(String processDefinitionId){
List<ProcessInstance> lists = runtimeService.createProcessInstanceQuery()
.active()
.processDefinitionId(processDefinitionId)
.orderByProcessInstanceId()
.asc()
.list();
Map<String, Object> data = new HashMap<String, Object>();
data.put("result", lists);
return CommonResult.success(data);
}
5.获取流程下失败实例
@Override
public CommonResult<Map> getIncidentByProcessId(String processDefinitionId){
List<Incident> lists = runtimeService.createIncidentQuery()
.processDefinitionId(processDefinitionId)
.orderByIncidentId()
.asc()
.list();
Map<String, Object> data = new HashMap<String, Object>();
data.put("result", lists);
return CommonResult.success(data);
}
6.根据key删除所有版本流程定义
public void deleteProcessDefinitionByKey() {
String processDefinitionKey = "Flow_15kr3p7";
List<ProcessDefinition> list = repositoryService
.createProcessDefinitionQuery()
.processDefinitionKey(processDefinitionKey)
.list();
for (ProcessDefinition processDefinition : list)
{
repositoryService.deleteDeployment(processDefinition.getDeploymentId(), true);
System.out.println("删除" + processDefinition.getDeploymentId());
}
System.out.println("删除成功");
}
网友评论