/** * Varargs constructor * @param name name of this service instance * @param children children */ public WorkflowCompositeService(String name, Service... children) { this(name); for (Service child : children) { addService(child); } }
/** * When this service is started, any service stopping with a failure * exception is converted immediately into a failure of this service, * storing the failure and stopping ourselves. * @param child the service that has changed. */ @Override public void stateChanged(Service child) { //if that child stopped while we are running: if (isInState(STATE.STARTED) && child.isInState(STATE.STOPPED)) { // a child service has stopped //did the child fail? if so: propagate Throwable failureCause = child.getFailureCause(); if (failureCause != null) { LOG.info("Child service " + child + " failed", failureCause); //failure. Convert to an exception Exception e = (failureCause instanceof Exception) ? (Exception) failureCause : new Exception(failureCause); //flip ourselves into the failed state noteFailure(e); stop(); } else { LOG.info("Child service completed {}", child); if (areAllChildrenStopped()) { LOG.info("All children are halted: stopping"); stop(); } } } }
@Override public ServiceParent buildService(Service... services) { ServiceParent parent = new WorkflowCompositeService("test", services); parent.init(new Configuration()); return parent; }
/** * Probe to query if all children are stopped -simply * by taking a snapshot of the child service list and enumerating * their state. * The state of the children may change during this operation -that will * not get picked up. * @return true if all the children are stopped. */ private boolean areAllChildrenStopped() { List<Service> children = getServices(); boolean stopped = true; for (Service child : children) { if (!child.isInState(STATE.STOPPED)) { stopped = false; break; } } return stopped; } }
/** * When this service is started, any service stopping with a failure * exception is converted immediately into a failure of this service, * storing the failure and stopping ourselves. * @param child the service that has changed. */ @Override public void stateChanged(Service child) { //if that child stopped while we are running: if (isInState(STATE.STARTED) && child.isInState(STATE.STOPPED)) { // a child service has stopped //did the child fail? if so: propagate Throwable failureCause = child.getFailureCause(); if (failureCause != null) { LOG.info("Child service " + child + " failed", failureCause); //failure. Convert to an exception Exception e = (failureCause instanceof Exception) ? (Exception) failureCause : new Exception(failureCause); //flip ourselves into the failed state noteFailure(e); stop(); } else { LOG.info("Child service completed {}", child); if (areAllChildrenStopped()) { LOG.info("All children are halted: stopping"); stop(); } } } }
@Override public ServiceParent buildService(Service... services) { ServiceParent parent = new WorkflowCompositeService("test", services); parent.init(new Configuration()); return parent; }
/** * Probe to query if all children are stopped -simply * by taking a snapshot of the child service list and enumerating * their state. * The state of the children may change during this operation -that will * not get picked up. * @return true if all the children are stopped. */ private boolean areAllChildrenStopped() { List<Service> children = getServices(); boolean stopped = true; for (Service child : children) { if (!child.isInState(STATE.STOPPED)) { stopped = false; break; } } return stopped; } }
@Test public void testBasicRun() throws Throwable { WorkflowCompositeService svc = run(new WorkflowCompositeService()); ServiceTerminatingRunnable runnable = new ServiceTerminatingRunnable(svc, new SimpleRunnable()); // synchronous in-thread execution runnable.run(); assertStopped(svc); }
/** * Construct with a list of children * @param name name of this service instance * @param children children to add */ public WorkflowCompositeService(String name, List<Service> children) { this(name); for (Service child : children) { addService(child); } }
@Test public void testBasicRun() throws Throwable { WorkflowCompositeService svc = run(new WorkflowCompositeService()); ServiceTerminatingRunnable runnable = new ServiceTerminatingRunnable(svc, new SimpleRunnable()); // synchronous in-thread execution runnable.run(); assertStopped(svc); }
/** * Varargs constructor * @param name name of this service instance * @param children children */ public WorkflowCompositeService(String name, Service... children) { this(name); for (Service child : children) { addService(child); } }
@Test public void testFailureRun() throws Throwable { WorkflowCompositeService svc = run(new WorkflowCompositeService()); ServiceTerminatingRunnable runnable = new ServiceTerminatingRunnable(svc, new SimpleRunnable(true)); // synchronous in-thread execution runnable.run(); assertStopped(svc); assertNotNull(runnable.getException()); }
/** * Construct with a list of children * @param name name of this service instance * @param children children to add */ public WorkflowCompositeService(String name, List<Service> children) { this(name); for (Service child : children) { addService(child); } }
@Test public void testFailureRun() throws Throwable { WorkflowCompositeService svc = run(new WorkflowCompositeService()); ServiceTerminatingRunnable runnable = new ServiceTerminatingRunnable(svc, new SimpleRunnable(true)); // synchronous in-thread execution runnable.run(); assertStopped(svc); assertNotNull(runnable.getException()); }
@Override public synchronized void addService(Service service) { Preconditions.checkArgument(service != null, "null service argument"); super.addService(service); }
@Override public synchronized void addService(Service service) { Preconditions.checkArgument(service != null, "null service argument"); super.addService(service); }