AnsweredAssumed Answered

multiInstance won't run task in parallel.

Question asked by clstokes on Oct 10, 2013
Latest reply on Oct 13, 2014 by oliver35
I'm trying to execute a task on a collection in parallel, however no combination of settings seems to change the behavior from running sequentially.

Here's my test case in Groovy:
<blockcode>
package tests

import org.activiti.engine.ProcessEngine
import org.activiti.engine.ProcessEngineConfiguration
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test

class ParallelActivitiTests {

  @Test
  void test( ) {
    _logger.debug( "Starting…" )
    processEngine.getRuntimeService()
      .startProcessInstanceByKey( "ParallelServiceTest", [ values: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ] )
    _logger.debug( "Completed." )
    Thread.sleep( 30000l )
  }

  @BeforeClass
  void init( ) {
    ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
    configuration.setClassLoader( Thread.currentThread().getContextClassLoader() )

    processEngine = configuration
      .setJobExecutorActivate( true ) // allows asynchronous activities
      .buildProcessEngine()

    processEngine.getRepositoryService().createDeployment()
      .addClasspathResource( "activiti-processes/ParallelServiceTest.bpmn20.xml" )
      .deploy()
  }

  private ProcessEngine processEngine
  private static final Logger _logger = LoggerFactory.getLogger( ParallelActivitiTests.class )

}
</blockcode>

And ParallelServiceTest.bpmn20.xml:
<blockcode>
<?xml version="1.0" encoding="UTF-8" ?>
<definitions id="definitions"
             targetNamespace="http://activiti.org/bpmn20"
             xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:activiti="http://activiti.org/bpmn">

  <process id="ParallelServiceTest">

    <startEvent id="start"/>
    <sequenceFlow id="_1" sourceRef="start" targetRef="logValue"/>

<!–
    <serviceTask id="logValue"
                 activiti:class="tasks.LogValueServiceTask"
                 activiti:exclusive="false">
      <multiInstanceLoopCharacteristics
          isSequential="false"
          activiti:collection="${values}"
          activiti:elementVariable="value">
      </multiInstanceLoopCharacteristics>
    </serviceTask>
–>

    <scriptTask id="logValue"
                name="Execute script"
                scriptFormat="groovy"
        >
      <multiInstanceLoopCharacteristics
          isSequential="false"
          activiti:collection="${values}"
          activiti:elementVariable="value">
      </multiInstanceLoopCharacteristics>
      <script>
        System.out.println(execution.getVariable("value"))
      </script>
    </scriptTask>

    <sequenceFlow id="_end" sourceRef="logValue" targetRef="end"/>
    <endEvent id="end"/>

  </process>

</definitions>
</blockcode>

Here's my test case output:
<blockcode>
Gradle test > com.weather.grid.services.platform.environment.ParallelActivitiTests.test STANDARD_OUT
    23:01:49.718 [Test worker] DEBUG c.w.g.s.p.e.ParallelActivitiTests - Starting…
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    23:01:50.391 [Test worker] DEBUG c.w.g.s.p.e.ParallelActivitiTests - Completed.
</blockcode>

I would expect values 0 to 9 to be possibly interleaved, not completely sequential. In my actual code, it's clear that my service task is called sequentially.

Note, neither the serviceTask or scriptTask work and marking both with
activiti:async="true" activiti:exclusive="false"
doesn't change their behavior.

What am I doing wrong?

Outcomes