class 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");

        /* Authenticate as kermit to make the process visible in the Explorer UI */
        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);
    }

    public  SpawnProcessForEachMachine(
        ProcessEngine processEngine, String processKey, String type, String resultVariable
    );

}