cancel
Showing results for 
Search instead for 
Did you mean: 

5.17 Issue with repeating async service tasks

mishamo
Champ in-the-making
Champ in-the-making
Hi,

I've noticed an issue that has creeped in in 5.17; it presents itself when trying to execute an async service task for a second time during a process. I've created the following test case:


public class AsyncProcessTest {

  @Rule
  public ActivitiRule activitiRule = new ActivitiRule("test.activiti.cfg.xml");

  @Test
  @Deployment(resources = {"AsyncFailProcess.bpmn20.xml"})
  public void retryTakesProcessBackToUserTask() throws Exception {
    ProcessInstance process = activitiRule.getRuntimeService().startProcessInstanceByKey("async_process");
    completeAsyncJobs();

    assertThat(process.getActivityId(), is("usertask1"));

    TaskService taskService = activitiRule.getTaskService();
    String taskId = taskService.createTaskQuery().executionId(process.getProcessInstanceId())
          .singleResult().getId();
    Map<String, Object> variables = new HashMap<>();
    variables.put("action", "retry");
    taskService.complete(taskId, variables);

    completeAsyncJobs();
    assertThat(process.getActivityId(), is("usertask1"));
  }

  private void completeAsyncJobs() {
    //Complete async jobs
    ManagementService managementService = activitiRule.getManagementService();
    List<Job> jobs = managementService.createJobQuery().list();
    for (Job job : jobs) {
      managementService.executeJob(job.getId());
    }
  }

}



<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="async_process" name="Async Process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <serviceTask id="servicetask1" name="Async Service Task" activiti:async="true" activiti:class="com.asyncexample.NothingServiceTask"></serviceTask>
    <userTask id="usertask1" name="User Task"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask1" targetRef="endevent1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action=="complete"}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow4" name="Retry" sourceRef="usertask1" targetRef="servicetask1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${action=="retry"}]]></conditionExpression>
    </sequenceFlow>
  </process>
</definitions>



<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">

    <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
    <property name="jdbcDriver" value="org.h2.Driver" />
    <property name="jdbcUsername" value="sa" />
    <property name="jdbcPassword" value="" />

    <!– I've tried multiple combinations of these… –>

    <!–<property name="jobExecutorActivate" value="false" />–>
    <!–<property name="jobExecutorEnabled" value="false" />–>

    <!–<property name="asyncExecutorEnabled" value="false" />–>
    <!–<property name="asyncExecutorActivate" value="false" />–>

    <property name="databaseSchemaUpdate" value="true" />


  </bean>

</beans>



public class NothingServiceTask implements JavaDelegate {

  @Override
  public void execute(DelegateExecution execution) throws Exception {
    System.out.println("I do nothing");
  }
}


If I take out the activiti:async="true" from the service task or revert to 5.16, this test passes. N.B. This is not just an issue in the tests, this also manifests itself in regular production code. Please advise if the usage of this has changed or whether I should report a bug.
5 REPLIES 5

jbarrez
Star Contributor
Star Contributor
I'm not sure how this could work, even on previous versions!

The ProcessInstance object you get back reflects the state at the point of when the transaction commits. This is the service task, NOT the user task. ie  assertThat(process.getActivityId(), is("usertask1")); should be  assertThat(process.getActivityId(), is("servicetask"));

In the sync case, it is the user task, as the transaction commits when the user task is reached.

mishamo
Champ in-the-making
Champ in-the-making
That makes sense, but I think that was an oversight on my part, rather than the underlying issue. From what I understand, based on your description, if I add a:
<code>
process = activitiRule.getRuntimeService().createProcessInstanceQuery()
          .processInstanceId(process.getProcessInstanceId())
          .singleResult();
</code>
before the last assert, then that should fetch the latest instance of that process (after the first transaction has been committed), which should now have advanced to the user task. Unfortunately, this isn't the case since 5.17.

trademak
Star Contributor
Star Contributor
Which combinations did you use exactly?
This one for example?
<blockcode>
<property name="jobExecutorEnabled" value="false" />
<property name="asyncExecutorEnabled" value="true" />
<property name="asyncExecutorActivate" value="false" />
</blockcode>

If you want to manually complete the jobs this would be the correct configuration. If it's not working for you, please create a unit test showing the issue and create a JIRA issue for it. That makes it easier for us to look into the issue.

Best regards,

mishamo
Champ in-the-making
Champ in-the-making
Hi Tijs,

Yes, I've tried that combination; the line:
<code><property name="jobExecutorEnabled" value="false" \></code>
causes a BeanCreationException to be thrown. The other lines seem to make no difference to whether the unit test succeeds or not.

Anyway, I've created a JIRA issue with attached unit test project: http://jira.codehaus.org/browse/ACT-2183

Thanks.

jbarrez
Star Contributor
Star Contributor
Ok, we will look into it