use of com.google.common.util.concurrent.Service.State in project guava by google.
the class AbstractService method notifyStopped.
/**
* Implementing classes should invoke this method once their service has stopped. It will cause
* the service to transition from {@link State#STOPPING} to {@link State#TERMINATED}.
*
* @throws IllegalStateException if the service is neither {@link State#STOPPING} nor {@link
* State#RUNNING}.
*/
protected final void notifyStopped() {
monitor.enter();
try {
// We check the internal state of the snapshot instead of state() directly so we don't allow
// notifyStopped() to be called while STARTING, even if stop() has already been called.
State previous = snapshot.state;
if (previous != STOPPING && previous != RUNNING) {
IllegalStateException failure = new IllegalStateException("Cannot notifyStopped() when the service is " + previous);
notifyFailed(failure);
throw failure;
}
snapshot = new StateSnapshot(TERMINATED);
enqueueTerminatedEvent(previous);
} finally {
monitor.leave();
dispatchListenerEvents();
}
}
use of com.google.common.util.concurrent.Service.State in project guava by google.
the class AbstractService method notifyFailed.
/**
* Invoke this method to transition the service to the {@link State#FAILED}. The service will
* <b>not be stopped</b> if it is running. Invoke this method when a service has failed critically
* or otherwise cannot be started nor stopped.
*/
protected final void notifyFailed(Throwable cause) {
checkNotNull(cause);
monitor.enter();
try {
State previous = state();
switch(previous) {
case NEW:
case TERMINATED:
throw new IllegalStateException("Failed while in state:" + previous, cause);
case RUNNING:
case STARTING:
case STOPPING:
snapshot = new StateSnapshot(FAILED, false, cause);
enqueueFailedEvent(previous, cause);
break;
case FAILED:
// Do nothing
break;
default:
throw new AssertionError("Unexpected state: " + previous);
}
} finally {
monitor.leave();
dispatchListenerEvents();
}
}
use of com.google.common.util.concurrent.Service.State in project guava by google.
the class ServiceManager method startAsync.
/**
* 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;
}
use of com.google.common.util.concurrent.Service.State in project guava by google.
the class AbstractServiceTest method testManualServiceStopMultipleTimesWhileStarting.
/**
* This tests for a bug where if {@link Service#stopAsync()} was called while the service was
* {@link State#STARTING} more than once, the {@link Listener#stopping(State)} callback would get
* called multiple times.
*/
public void testManualServiceStopMultipleTimesWhileStarting() throws Exception {
ManualSwitchedService service = new ManualSwitchedService();
final AtomicInteger stopppingCount = new AtomicInteger();
service.addListener(new Listener() {
@Override
public void stopping(State from) {
stopppingCount.incrementAndGet();
}
}, directExecutor());
service.startAsync();
service.stopAsync();
assertEquals(1, stopppingCount.get());
service.stopAsync();
assertEquals(1, stopppingCount.get());
}
use of com.google.common.util.concurrent.Service.State in project guava by google.
the class AbstractScheduledServiceTest method testFailOnErrorFromStartUpListener.
public void testFailOnErrorFromStartUpListener() throws InterruptedException {
final Error error = new Error();
final CountDownLatch latch = new CountDownLatch(1);
TestService service = new TestService();
service.addListener(new Service.Listener() {
@Override
public void running() {
throw error;
}
@Override
public void failed(State from, Throwable failure) {
assertEquals(State.RUNNING, from);
assertEquals(error, failure);
latch.countDown();
}
}, directExecutor());
service.startAsync();
latch.await();
assertEquals(0, service.numberOfTimesRunCalled.get());
assertEquals(Service.State.FAILED, service.state());
}
Aggregations