CheckProcessesEnded implements JavaDelegate { @Override public void execute(DelegateExecution execution) throws Exception { @SuppressWarnings("unchecked") List<String> processIds = (List<String>) execution.getVariable(variableWithProcessIds); List<String> ended = Lists.newArrayList(Iterables.filter(processIds, new Predicate<String>() { @Override public boolean apply(String processInstanceId) { ProcessInstance instance = runtimeService.createProcessInstanceQuery() .processInstanceId(processInstanceId).singleResult(); return instance == null || instance.isEnded(); } })); boolean done = (processIds.size() == ended.size()); execution.setVariable(resultVariable, done); if (done) { LOG.info("All background processes ENDED: {}", processIds); } else { if (LOG.isInfoEnabled()) { LOG.info("Still waiting for: {}", Sets.difference(ImmutableSet.copyOf(processIds), ImmutableSet.copyOf(ended))); } } } CheckProcessesEnded(RuntimeService runtimeService, String variableWithProcessIds,
                               String resultVariable); @Override void execute(DelegateExecution execution);  }
@Test public void testWithAListOfEndedProcesses() throws Exception { DelegateExecution execution = mock(DelegateExecution.class); when(execution.getVariable(eq(PROCESS_IDS))).thenReturn(Lists.newArrayList("1", "2")); ProcessVariablesCollector collector = new ProcessVariablesCollector(); collector.install(execution); RuntimeService runtimeService = mockRuntimeService(ImmutableMap.of( "1", mockProcessInstance( true), "2", mockProcessInstance( true) )); JavaDelegate delegate = new CheckProcessesEnded(runtimeService, PROCESS_IDS, RESULT); delegate.execute(execution); assertThat((Boolean) collector.getVariable(RESULT)).isTrue(); }
@Test public void testWithOneEndedAndOneStillRunning() throws Exception { DelegateExecution execution = mock(DelegateExecution.class); when(execution.getVariable(eq(PROCESS_IDS))).thenReturn(Lists.newArrayList("1", "2")); ProcessVariablesCollector collector = new ProcessVariablesCollector(); collector.install(execution); RuntimeService runtimeService = mockRuntimeService(ImmutableMap.of( "1", mockProcessInstance( true), "2", mockProcessInstance( false) )); JavaDelegate delegate = new CheckProcessesEnded(runtimeService, PROCESS_IDS, RESULT); delegate.execute(execution); assertThat((Boolean) collector.getVariable(RESULT)).isFalse(); }
@Test public void testWithOneInvalidProcessId() throws Exception { DelegateExecution execution = mock(DelegateExecution.class); when(execution.getVariable(eq(PROCESS_IDS))).thenReturn(Lists.newArrayList("1", "invalid")); ProcessVariablesCollector collector = new ProcessVariablesCollector(); collector.install(execution); RuntimeService runtimeService = mockRuntimeService(ImmutableMap.of( "1", mockProcessInstance( true)), "invalid"); JavaDelegate delegate = new CheckProcessesEnded(runtimeService, PROCESS_IDS, RESULT); delegate.execute(execution); assertThat((Boolean) collector.getVariable(RESULT)).isTrue(); }