@Override public final State state() { return delegate.state(); }
@Override public final State state() { return delegate.state(); }
@Override public final State state() { return delegate.state(); }
@Override public final State state() { return delegate.state(); }
@Override public final State state() { return delegate.state(); }
@Override public final State state() { return delegate.state(); }
@Override public synchronized void starting() { assertTrue(stateHistory.isEmpty()); assertNotSame(State.NEW, service.state()); stateHistory.add(State.STARTING); }
/** * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It * is only valid to call this method if all of the services are {@linkplain State#NEW new}. * * @return this * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the * method is called. */ @CanIgnoreReturnValue public ServiceManager startAsync() { for (Service service : services) { State state = service.state(); checkState(state == NEW, "Service %s is %s, cannot start it.", service, state); } for (Service service : services) { try { state.tryStartTiming(service); service.startAsync(); } catch (IllegalStateException e) { // This can happen if the service has already been started or stopped (e.g. by another // service or listener). Our contract says it is safe to call this method if // all services were NEW when it was called, and this has already been verified above, so we // don't propagate the exception. logger.log(Level.WARNING, "Unable to start Service " + service, e); } } return this; }
checkArgument(service.state() == NEW, "Can only manage NEW services, %s", service);
/** * Marks the {@link State} as ready to receive transitions. Returns true if no transitions have * been observed yet. */ void markReady() { monitor.enter(); try { if (!transitioned) { // nothing has transitioned since construction, good. ready = true; } else { // This should be an extremely rare race condition. List<Service> servicesInBadStates = Lists.newArrayList(); for (Service service : servicesByState().values()) { if (service.state() != NEW) { servicesInBadStates.add(service); } } throw new IllegalArgumentException( "Services started transitioning asynchronously before " + "the ServiceManager was constructed: " + servicesInBadStates); } } finally { monitor.leave(); } }
@Override public synchronized void running() { assertEquals(State.STARTING, Iterables.getOnlyElement(stateHistory)); stateHistory.add(State.RUNNING); service.awaitRunning(); assertNotSame(State.STARTING, service.state()); }
/** * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It * is only valid to call this method if all of the services are {@linkplain State#NEW new}. * * @return this * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the * method is called. */ @CanIgnoreReturnValue public ServiceManager startAsync() { for (Service service : services) { State state = service.state(); checkState(state == NEW, "Service %s is %s, cannot start it.", service, state); } for (Service service : services) { try { state.tryStartTiming(service); service.startAsync(); } catch (IllegalStateException e) { // This can happen if the service has already been started or stopped (e.g. by another // service or listener). Our contract says it is safe to call this method if // all services were NEW when it was called, and this has already been verified above, so we // don't propagate the exception. logger.log(Level.WARNING, "Unable to start Service " + service, e); } } return this; }
/** * Marks the {@link State} as ready to receive transitions. Returns true if no transitions have * been observed yet. */ void markReady() { monitor.enter(); try { if (!transitioned) { // nothing has transitioned since construction, good. ready = true; } else { // This should be an extremely rare race condition. List<Service> servicesInBadStates = Lists.newArrayList(); for (Service service : servicesByState().values()) { if (service.state() != NEW) { servicesInBadStates.add(service); } } throw new IllegalArgumentException( "Services started transitioning asynchronously before " + "the ServiceManager was constructed: " + servicesInBadStates); } } finally { monitor.leave(); } }
checkArgument(service.state() == NEW, "Can only manage NEW services, %s", service);
@Override public synchronized void terminated(State from) { assertEquals(from, Iterables.getLast(stateHistory, State.NEW)); stateHistory.add(State.TERMINATED); assertEquals(State.TERMINATED, service.state()); if (from == State.NEW) { try { service.awaitRunning(); fail(); } catch (IllegalStateException expected) { assertThat(expected).hasCauseThat().isNull(); assertThat(expected) .hasMessageThat() .isEqualTo("Expected the service " + service + " to be RUNNING, but was TERMINATED"); } } completionLatch.countDown(); }
/** * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It * is only valid to call this method if all of the services are {@linkplain State#NEW new}. * * @return this * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the * method is called. */ @CanIgnoreReturnValue public ServiceManager startAsync() { for (Service service : services) { State state = service.state(); checkState(state == NEW, "Service %s is %s, cannot start it.", service, state); } for (Service service : services) { try { state.tryStartTiming(service); service.startAsync(); } catch (IllegalStateException e) { // This can happen if the service has already been started or stopped (e.g. by another // service or listener). Our contract says it is safe to call this method if // all services were NEW when it was called, and this has already been verified above, so we // don't propagate the exception. logger.log(Level.WARNING, "Unable to start Service " + service, e); } } return this; }
/** * Marks the {@link State} as ready to receive transitions. Returns true if no transitions have * been observed yet. */ void markReady() { monitor.enter(); try { if (!transitioned) { // nothing has transitioned since construction, good. ready = true; } else { // This should be an extremely rare race condition. List<Service> servicesInBadStates = Lists.newArrayList(); for (Service service : servicesByState().values()) { if (service.state() != NEW) { servicesInBadStates.add(service); } } throw new IllegalArgumentException( "Services started transitioning asynchronously before " + "the ServiceManager was constructed: " + servicesInBadStates); } } finally { monitor.leave(); } }
@Override public synchronized void stopping(State from) { assertEquals(from, Iterables.getLast(stateHistory)); stateHistory.add(State.STOPPING); if (from == State.STARTING) { try { service.awaitRunning(); fail(); } catch (IllegalStateException expected) { assertThat(expected).hasCauseThat().isNull(); assertThat(expected) .hasMessageThat() .isEqualTo("Expected the service " + service + " to be RUNNING, but was STOPPING"); } } assertNotSame(from, service.state()); }
@Override public synchronized void failed(State from, Throwable failure) { assertEquals(from, Iterables.getLast(stateHistory)); stateHistory.add(State.FAILED); assertEquals(State.FAILED, service.state()); assertEquals(failure, service.failureCause()); if (from == State.STARTING) { try { service.awaitRunning(); fail(); } catch (IllegalStateException e) { assertThat(e).hasCauseThat().isEqualTo(failure); } } try { service.awaitTerminated(); fail(); } catch (IllegalStateException e) { assertThat(e).hasCauseThat().isEqualTo(failure); } completionLatch.countDown(); } }
private static void assertState( ServiceManager manager, Service.State state, Service... services) { Collection<Service> managerServices = manager.servicesByState().get(state); for (Service service : services) { assertEquals(service.toString(), state, service.state()); assertEquals(service.toString(), service.isRunning(), state == Service.State.RUNNING); assertTrue(managerServices + " should contain " + service, managerServices.contains(service)); } }