简体   繁体   English

activiti 中的异步任务未执行

[英]Async task in activiti not executed

I have a problem with a simple workflow implemented with activiti engine and Java I'm not able to execute a task asynchronously.我对使用 activiti 引擎和 Java 实现的简单工作流程有疑问,我无法异步执行任务。 The workflow is very simple工作流程非常简单在此处输入图像描述

The upper part of the workflow start a process execute the "Start Service Task" emit a signal and execute the "Loop service Task" and repeat 10 times.工作流的上半部分启动一个流程执行“启动服务任务”发出信号并执行“循环服务任务”并重复10次。 The bottom part is triggered by the signal emited in the upper part and must be execute asynchronously respect the loop part but in reality it block the "Loop service Task".底部由上部发出的信号触发,必须异步执行循环部分,但实际上它阻塞了“循环服务任务”。

I try with set the async attribute in the bottom flow but in this case the bottom is not executed.我尝试在底部流程中设置 async 属性,但在这种情况下,底部不会执行。 Follow the link to github project.按照链接到 github 项目。 https://github.com/giane88/testActiviti https://github.com/giane88/testActiviti

package configuration;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.asyncexecutor.AsyncExecutor;
import org.activiti.engine.impl.asyncexecutor.ManagedAsyncJobExecutor;
import org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;

public class WorkflowConfiguration {
  final ProcessEngine processEngine;

  public WorkflowConfiguration(final String workFlowName) {
    processEngine = setUpProcessEngine(workFlowName);
  }

  public ProcessEngine getProcessEngine() {
    return processEngine;
  }

  private ProcessEngine setUpProcessEngine(String workFlowName) {
    ProcessEngineConfiguration cfg = null;
    cfg = new StandaloneProcessEngineConfiguration()
        .setJdbcUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000")
        .setJdbcUsername("sa")
        .setJdbcPassword("")
        .setJdbcDriver("org.h2.Driver")
     .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
    final ProcessEngine processEngine = cfg.buildProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    repositoryService.createDeployment().addClasspathResource("activiti/" + workFlowName)
        .deploy();
    return processEngine;
  }

}
package configuration;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.HashMap;
import java.util.Map;

public class WorkflowManipulator {
  private final Map<String, Object> nextDelegateVariables ;
  private final String wfName;
  private final ProcessEngine engine;

  public WorkflowManipulator(String wfName, ProcessEngine engine) {
    this.nextDelegateVariables = new HashMap<>();
    this.wfName = wfName;
    this.engine = engine;
  }


  public ProcessInstance startProcess() {
    if (nextDelegateVariables.size() > 0) {
      return engine.getRuntimeService().startProcessInstanceByKey(wfName, nextDelegateVariables);
    } else {
      return engine.getRuntimeService().startProcessInstanceByKey(wfName);
    }
  }
}
@Log4j2
public class TestWorkFlowMain {

  public static void main(String[] args) throws IOException {

    WorkflowConfiguration workflowConfiguration = new WorkflowConfiguration("test.bpmn");
    WorkflowManipulator workflowManipulator = new WorkflowManipulator("testProcess", workflowConfiguration.getProcessEngine());
    ProcessInstance processInstance = workflowManipulator.startProcess();
  }
}
package delegates;

import lombok.extern.log4j.Log4j2;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

@Log4j2
public class AsyncServiceTask implements JavaDelegate {
  @Override
  public void execute(DelegateExecution execution) throws Exception {
    log.info("Sleeping for 3 second");
    Thread.sleep(3000);
    log.warn("AsyncCompleted");
  }
}

As usualy i found the solution some minutes after post the question.像往常一样,我在发布问题几分钟后找到了解决方案。 The problems was in the process engine configuration you need to set up an asyncExecutor and enable and active it like in the example below.问题出在流程引擎配置中,您需要设置 asyncExecutor 并启用和激活它,如下例所示。

    ProcessEngineConfiguration cfg;
    AsyncExecutor asyncExecutor = new ManagedAsyncJobExecutor();
    cfg = new StandaloneInMemProcessEngineConfiguration()
        .setAsyncExecutor(asyncExecutor)
        .setAsyncExecutorEnabled(true)
        .setAsyncExecutorActivate(true);
    final ProcessEngine processEngine = cfg.buildProcessEngine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM