REST API 启动流程实例正在等待流程实例完成

REST API to start a process instance is holding request until process instance is complete

我正在使用 Activiti 版本 6。

我从 activiti-app 创建了一个 BPMN 流程。
然后我想使用 API.

activiti-rest.war 开始这个过程
http://localhost:8080/activiti-rest/service/runtime/process-instances

request body :

 {

   "processDefinitionKey":"cep_dispatch_process",
   "businessKey":"myBusinessKey",
     "returnVariables": false
}

header :
Content-Type:application/json

正如我在 tomcat 个线程中启动的 LOG 进程中看到的那样。 参考最新 GitHub 代码:

Activiti-activiti-6.0.0\modules\activiti-rest\src\main\java\org\activiti\rest\service\api\runtime\process\ProcessInstanceCollectionResource.java

当我看到方法时,

@RequestMapping(value = "/runtime/process-instances", method = RequestMethod.POST, produces = "application/json")
  public ProcessInstanceResponse createProcessInstance(@RequestBody ProcessInstanceCreateRequest request, HttpServletRequest httpRequest, HttpServletResponse response) {

我可以看到进程正在启动而不是等待进程完成,HTTP 响应是 201。我可以理解请求没有被保留以等待进程实例完成。

instance = processInstanceBuilder.start();

response.setStatus(HttpStatus.CREATED.value());

请参考下面截取的日志,我可以看到进程正在服务器线程中执行,请求正在等待进程完成。

276-DEBUG 17-01-2019 14:12:07,177- (http-nio-8080-exec-3) ExecutionEntityManagerImpl: Child execution Execution[ id '130023' ] - parent '130021' created with parent 130021
241-DEBUG 17-01-2019 14:12:07,178- (http-nio-8080-exec-3) ContinueProcessOperation: Executing boundary event activityBehavior class org.activiti.engine.impl.bpmn.behavior.BoundaryTimerEventActivityBehavior with execution 130023
171-DEBUG 17-01-2019 14:12:07,202- (http-nio-8080-exec-3) ContinueProcessOperation: Executing activityBehavior class org.activiti.engine.impl.bpmn.behavior.SubProcessActivityBehavior on activity 'sid-1A2A8DF5-764A-4960-8E5D-F347DC10207C' with execution 130021
276-DEBUG 17-01-2019 14:12:07,203- (http-nio-8080-exec-3) ExecutionEntityManagerImpl: Child execution Execution[ id '130025' ] - parent '130021' created with parent 130021
63-DEBUG 17-01-2019 14:12:07,203- (http-nio-8080-exec-3) DefaultActivitiEngineAgenda: Operation class org.activiti.engine.impl.agenda.ContinueProcessOperation added to agenda
70-DEBUG 17-01-2019 14:12:07,203- (http-nio-8080-exec-3) CommandInvoker: Executing operation class org.activiti.engine.impl.agenda.ContinueProcessOperation 

请求不能等待进程完成。

如何解决这个问题,启动进程的请求不能等待进程实例完成。

如您在以下回复中所见:

{"id":"130028",
"url":"http://localhost:8080/activiti-rest/service/runtime/process-instances/130028",
"businessKey":"myBusinessKey",
"suspended":false,
"ended":true,
"processDefinitionId":"cep_dispatch_process:13:125033",
"processDefinitionUrl":"http://localhost:8080/activiti-rest/service/repository/process-definitions/cep_dispatch_process:13:125033"
,"processDefinitionKey":"cep_dispatch_process",
"activityId":null,
"variables":[],
"tenantId":"",
"name":null,
"completed":true
}

API 仅在进程完成后返回,我在服务任务中添加了 2 分钟的延迟,我可以看到请求将等待。

我不是 Activiti 的专家,但作为最简单的解决方案,我可以向 activate Async executor and use Asynchronous Continuations 推荐您的服务任务。这可以解决您的问题。 Activiti 的行为是预料之中的,因为在它把状态持久化到数据库之前,它不能确定进程已经创建(因为事务可能因为数据库错误而被回滚)