简体   繁体   中英

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. 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. 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. Follow the link to github project. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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