While it is recommended that service lifecycles be managed via this class, state transitions
initiated via other mechanisms do not impact the correctness of its methods. For example, if the
services are started by some mechanism besides startAsync, the listeners will be invoked
when appropriate and awaitHealthy will still work as expected.
Here is a simple example of how to use a ServiceManager to start a server.
class Server {
public static void main(String[] args) {
Set<Service> services = ...;
ServiceManager manager = new ServiceManager(services);
manager.addListener(new Listener() {
public void stopped() {}
public void healthy() {
// Services have been initialized and are healthy, start accepting requests...
}
public void failure(Service service) {
// Something failed, at this point we could log it, notify a load balancer, or take
// some other action. For now we will just exit.
System.exit(1);
}
},
MoreExecutors.directExecutor());
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
// Give the services 5 seconds to stop to ensure that we are responsive to shutdown
// requests.
try {
manager.stopAsync().awaitStopped(5, TimeUnit.SECONDS);
} catch (TimeoutException timeout) {
// stopping timed out
}
}
});
manager.startAsync(); // start all the services asynchronously
}
}
This class uses the ServiceManager's methods to start all of its services, to respond to service failure and to ensure that when the JVM is shutting down all the services are stopped.
| Modifier and Type | Class and Description |
|---|---|
| private static class | ServiceManager.
This is never thrown but only used for logging. |
| private static class | |
| public abstract static class | ServiceManager.
A listener for the aggregate state changes of the services that are under management. |
| private static class | ServiceManager.
A |
| private static class | ServiceManager.
A |
| private static class | ServiceManager.
An encapsulation of all the mutable state of the |
| Modifier and Type | Field and Description |
|---|---|
| private static final ListenerCallQueue. | |
| private static final LazyLogger | |
| private final ImmutableList | |
| private final ServiceManager. | state
An encapsulation of all of the state that is accessed by the service listeners. |
| private static final ListenerCallQueue. |
| Access | Constructor and Description |
|---|---|
| public | ServiceManager(Iterable<? extends Service>
The services to manage services)Constructs a new instance for managing the given services. |
| Modifier and Type | Method and Description |
|---|---|
| public void | addListener(ServiceManager.
the listener to run when the manager changes state listener,the executor in which the listeners callback methods will be run. executor) |
| public void | |
| public void | awaitHealthy(Duration
the maximum time to wait timeout)Waits for the |
| public void | awaitHealthy(long
the maximum time to wait timeout, TimeUnit the time unit of the timeout argument unit)Waits for the |
| public void | |
| public void | awaitStopped(Duration
the maximum time to wait timeout)Waits for the all the services to reach a terminal state for no more than the given time. |
| public void | awaitStopped(long
the maximum time to wait timeout, TimeUnit the time unit of the timeout argument unit)Waits for the all the services to reach a terminal state for no more than the given time. |
| public boolean | |
| public ImmutableSetMultimap | servicesByState()
Implements com. |
| public ServiceManager | |
| public ImmutableMap | Returns: Map of services and their corresponding startup time, the map entries will be ordered by startup time.Returns the service load times. |
| public ImmutableMap | Returns: Map of services and their corresponding startup time in millis, the map entries will be ordered by startup time.Returns the service load times. |
| public ServiceManager | |
| public String |