Activiti7集成SpringBoot2官方实例(四):添加ReST调用列出流程实例
能够列出活动流程实例很有用。并且还能够获得有关流程实例的更多元数据,例如执行流程中的位置。让我们为此创建几个ReST调用,这些调用可以派上用场。
在 org/activiti/training/activiti7apibasicprocessusertaskservicetaskevents/rest
包中添加一个名为 ProcessInstanceController
的新Spring MVC控制器,如下所示:
package org.activiti.training.activiti7apibasicprocessusertaskservicetaskevents.rest;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.ProcessInstanceMeta;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Pageable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* ReST controller to get some info about a process instance
*
*/
@RestController
public class ProcessInstanceController {
private Logger logger = LoggerFactory.getLogger(ProcessInstanceController.class);
@Autowired
private ProcessRuntime processRuntime;
@GetMapping("/process-instances")
public List<ProcessInstance> getProcessInstances() {
List<ProcessInstance> processInstances =
processRuntime.processInstances(Pageable.of(0, 10)).getContent();
return processInstances;
}
@GetMapping("/process-instance-meta")
public ProcessInstanceMeta getProcessInstanceMeta(@RequestParam(value="processInstanceId") String processInstanceId) {
ProcessInstanceMeta processInstanceMeta = processRuntime.processInstanceMeta(processInstanceId);
return processInstanceMeta;
}
}
首先注入 ProcessRuntime Spring bean,以便我们可以使用Process Runtime API,它具有 我们需要的processInstances() 和 processInstanceMeta()方法。 的 /过程实例 URL被用于第一REST调用刚返回前10个活性流程实例。第二个URL /process-instance-meta?processInstanceId = {processInstanceId} 提供有关流程实例的信息,例如它当前正在等待的活动。
现在,按照前面的描述打包并运行应用程序。启动应用程序时,您应该看到指示所有内容都已正确实现的日志:
...
2018-08-30 10:35:46.192 INFO 36251 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/process-instance-meta],methods=[GET]}" onto public org.activiti.runtime.api.model.ProcessInstanceMeta org.activiti.training.activiti7apibasicprocessusertaskservicetaskevents.rest.ProcessInstanceController.getProcessInstanceMeta(java.lang.String)
2018-08-30 10:35:46.193 INFO 36251 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/process-instances],methods=[GET]}" onto public java.util.List<org.activiti.runtime.api.model.ProcessInstance> org.activiti.training.activiti7apibasicprocessusertaskservicetaskevents.rest.ProcessInstanceController.getProcessInstances()
...
当我们运行内存数据库时,我们之前启动的流程实例将在重新启动后消失,创建一个新的 http://localhost:8080/start-process?processDefinitionKey=sampleproc-e9b76ff9-6f70-42c9-8dee-f6116c533a6d
。
现在,要列出流程实例,请访问 http://localhost:8080/process-instances
URL。您应该收到类似这样的回复:
[
{
id: "b0a28a43-fa2b-11e8-9c34-acde48001122",
name: "Sample Process: Fri Dec 07 14:23:38 GMT 2018",
processDefinitionId: "c68315b2-fa2a-11e8-9c34-acde48001122",
processDefinitionKey: "sampleproc-e9b76ff9-6f70-42c9-8dee-f6116c533a6d",
startDate: "2018-12-07T14:23:38.878+0000",
status: "RUNNING"
}
]
然后,我们可以使用其他ReST调用查询此流程实例以获取更多信息。键入以下URL(使用 上面调用中返回的processInstanceId ):http://localhost:8080/process-instance-meta?processInstanceId=b0a28a43-fa2b-11e8-9c34-acde48001122
。然后你应该得到一个像这样的响应:
{
processInstanceId: "b0a28a43-fa2b-11e8-9c34-acde48001122",
activeActivitiesIds:
[
"UserTask_0b6cp1l"
]
}
我们的流程定义中的第一个活动是用户任务1,因此在流程定义中执行的位置。进程引擎正在等待以下用户任务完成:
<bpmn2:userTask id="UserTask_0b6cp1l" name="User Task 1" activiti:assignee="testuser">
<bpmn2:incoming>SequenceFlow_0qdq7ff</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_1sc9dgy</bpmn2:outgoing>
</bpmn2:userTask>
当您遇到问题并且不确切知道流程实例在流程定义中等待的位置时,能够列出和检查流程实例非常有用。