SpawnProcessForEachMachine implements JavaDelegate { @Override public void execute(DelegateExecution execution) throws Exception { final Pool pool = (Pool) execution.getVariable(CoreProcessVariables.POOL); checkNotNull(pool, "Expecting to find a pool description as process variable"); @SuppressWarnings("unchecked") List<Machine> machines = (List<Machine>) execution.getVariable(CoreProcessVariables.MACHINES); checkNotNull(machines, "Expecting to find the list of machines as process variable"); final String poolBusinessKey = String.class.cast(execution.getVariable(CoreProcessVariables.POOL_BUSINESS_KEY)); checkNotNull(poolBusinessKey, "No way to link sub-processes to master process, poolBusinessKey is null"); processEngine.getIdentityService().setAuthenticatedUserId(CoreConstants.ACTIVITI_EXPLORER_DEFAULT_USER); List<String> processIds = Lists.newArrayList(); for (Machine machine : machines) { final String perMachineProcessBusinessKey = String.format("%s-%s-%s", execution.getProcessBusinessKey(), type, machine.getExternalId()); ProcessInstance perMachineProcess = processEngine.getRuntimeService().startProcessInstanceByKey( processKey, perMachineProcessBusinessKey, ImmutableMap.<String, Object>of(CoreProcessVariables.POOL, pool, CoreProcessVariables.POOL_BUSINESS_KEY, poolBusinessKey, MACHINE, machine)); LOG.info("Started background '" + type + "' process {} ({}) for machine {}", new Object[]{perMachineProcessBusinessKey, perMachineProcess.getId(), machine.getExternalId()}); processIds.add(perMachineProcess.getId()); } LOG.info("Saving process IDs {} as {}", processIds, resultVariable); execution.setVariable(resultVariable, processIds); } SpawnProcessForEachMachine(
        ProcessEngine processEngine, String processKey, String type, String resultVariable
    ); @Override void execute(DelegateExecution execution);  }
@Test public void testSpawnSampleProcessForLocalhost() throws Exception { DelegateExecution execution = mock(DelegateExecution.class); when(execution.getVariable(eq(CoreProcessVariables.POOL))).thenReturn(mock(Pool.class)); when(execution.getVariable(eq(CoreProcessVariables.POOL_BUSINESS_KEY))).thenReturn(BUSINESS_KEY); List<Machine> machines = Lists.newArrayList( Machine.builder().localhost().createMachine(), Machine.builder().localhost().externalId("local-2").createMachine() ); when(execution.getVariable(eq(CoreProcessVariables.MACHINES))).thenReturn(machines); ProcessVariablesCollector collector = new ProcessVariablesCollector(); collector.install(execution); ProcessEngine processEngine = new StandaloneInMemProcessEngineConfiguration() .setJobExecutorActivate(true).buildProcessEngine(); processEngine.getRepositoryService().createDeployment() .addClasspathResource("diagrams/empty.bpmn20.xml").deploy(); try { JavaDelegate delegate = new SpawnProcessForEachMachine(processEngine, EMPTY_PROCESS_KEY, "test", RESULT); delegate.execute(execution); @SuppressWarnings("unchecked") List<String> processInstanceIds = (List<String>) collector.getVariable(RESULT); assertThat(processInstanceIds).hasSize(2); } finally { processEngine.close(); } }