Class ServiceManager
services
. This class provides
methods for starting, stopping and
inspecting a collection of services.
Additionally, users can monitor state transitions with the listener
mechanism.
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.sameThreadExecutor());
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.
- Since:
- 14.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Deprecated.The Google Guava Core Libraries are deprecated and will not be part of the AEM SDK after April 2023 -
Constructor Summary
ConstructorsConstructorDescriptionServiceManager
(Iterable<? extends Service> services) Deprecated.Constructs a new instance for managing the given services. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(ServiceManager.Listener listener) Deprecated.Registers aServiceManager.Listener
to be run when thisServiceManager
changes state.void
addListener
(ServiceManager.Listener listener, Executor executor) Deprecated.Registers aServiceManager.Listener
to be executed on the given executor.void
Deprecated.Waits for theServiceManager
to become healthy.void
awaitHealthy
(long timeout, TimeUnit unit) Deprecated.Waits for theServiceManager
to become healthy for no more than the given time.void
Deprecated.Waits for the all the services to reach a terminal state.void
awaitStopped
(long timeout, TimeUnit unit) Deprecated.Waits for the all the services to reach a terminal state for no more than the given time.boolean
Deprecated.Returns true if all services are currently in the running state.Deprecated.Provides a snapshot of the current state of all the services under management.Deprecated.Initiates service startup on all the services being managed.Deprecated.Returns the service load times.Deprecated.Initiates service shutdown if necessary on all the services being managed.toString()
Deprecated.
-
Constructor Details
-
ServiceManager
Deprecated.Constructs a new instance for managing the given services.- Parameters:
services
- The services to manage- Throws:
IllegalArgumentException
- if not all services arenew
or if there are any duplicate services.
-
-
Method Details
-
addListener
Deprecated.Registers aServiceManager.Listener
to be executed on the given executor. The listener will not have previous state changes replayed, so it is suggested that listeners are added before any of the managed services are started.There is no guaranteed ordering of execution of listeners, but any listener added through this method is guaranteed to be called whenever there is a state change.
Exceptions thrown by a listener will be propagated up to the executor. Any exception thrown during
Executor.execute
(e.g., aRejectedExecutionException
or an exception thrown by inline execution) will be caught and logged.For fast, lightweight listeners that would be safe to execute in any thread, consider calling
addListener(Listener)
.- Parameters:
listener
- the listener to run when the manager changes stateexecutor
- the executor in which the listeners callback methods will be run.
-
addListener
Deprecated.Registers aServiceManager.Listener
to be run when thisServiceManager
changes state. The listener will not have previous state changes replayed, so it is suggested that listeners are added before any of the managed services are started.There is no guaranteed ordering of execution of listeners, but any listener added through this method is guaranteed to be called whenever there is a state change.
Exceptions thrown by a listener will be will be caught and logged.
- Parameters:
listener
- the listener to run when the manager changes state
-
startAsync
Deprecated.Initiates service startup on all the services being managed. It is only valid to call this method if all of the services are new.- Returns:
- this
- Throws:
IllegalStateException
- if any of the Services are notnew
when the method is called.
-
awaitHealthy
public void awaitHealthy()Deprecated.Waits for theServiceManager
to become healthy. The manager will become healthy after all the component services have reached the running state.- Throws:
IllegalStateException
- if the service manager reaches a state from which it cannot become healthy.
-
awaitHealthy
Deprecated.Waits for theServiceManager
to become healthy for no more than the given time. The manager will become healthy after all the component services have reached the running state.- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Throws:
TimeoutException
- if not all of the services have finished starting within the deadlineIllegalStateException
- if the service manager reaches a state from which it cannot become healthy.
-
stopAsync
Deprecated.Initiates service shutdown if necessary on all the services being managed.- Returns:
- this
-
awaitStopped
public void awaitStopped()Deprecated.Waits for the all the services to reach a terminal state. After this method returns all services will either beterminated
orfailed
-
awaitStopped
Deprecated.Waits for the all the services to reach a terminal state for no more than the given time. After this method returns all services will either beterminated
orfailed
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Throws:
TimeoutException
- if not all of the services have stopped within the deadline
-
isHealthy
public boolean isHealthy()Deprecated.Returns true if all services are currently in the running state.Users who want more detailed information should use the
servicesByState()
method to get detailed information about which services are not running. -
servicesByState
Deprecated.Provides a snapshot of the current state of all the services under management.N.B. This snapshot it not guaranteed to be consistent, i.e. the set of states returned may not correspond to any particular point in time view of the services.
-
startupTimes
Deprecated.Returns the service load times. This value will only return startup times for services that have finished starting.- Returns:
- Map of services and their corresponding startup time in millis, the map entries will be ordered by startup time.
-
toString
Deprecated.
-