Class ServiceManager.ServiceManagerState
- java.lang.Object
-
- com.google.common.util.concurrent.ServiceManager.ServiceManagerState
-
- Enclosing class:
- ServiceManager
private static final class ServiceManager.ServiceManagerState extends java.lang.ObjectAn encapsulation of all the mutable state of theServiceManagerthat needs to be accessed by instances ofServiceManager.ServiceListener.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classServiceManager.ServiceManagerState.AwaitHealthGuard(package private) classServiceManager.ServiceManagerState.StoppedGuard
-
Field Summary
Fields Modifier and Type Field Description (package private) Monitor.GuardawaitHealthGuardControls how long to wait for all the services to either become healthy or reach a state from which it is guaranteed that it can never become healthy.(package private) ListenerCallQueue<ServiceManager.Listener>listenersThe listeners to notify during a state transition.(package private) Monitormonitor(package private) intnumberOfServices(package private) booleanreadyThese two booleans are used to mark the state as ready to start.(package private) SetMultimap<Service.State,Service>servicesByState(package private) java.util.Map<Service,Stopwatch>startupTimers(package private) Multiset<Service.State>states(package private) Monitor.GuardstoppedGuardControls how long to wait for all services to reach a terminal state.(package private) booleantransitioned
-
Constructor Summary
Constructors Constructor Description ServiceManagerState(ImmutableCollection<Service> services)It is implicitly assumed that all the services are NEW and that they will all remain NEW until all the Listeners are installed andmarkReady()is called.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddListener(ServiceManager.Listener listener, java.util.concurrent.Executor executor)(package private) voidawaitHealthy()(package private) voidawaitHealthy(long timeout, java.util.concurrent.TimeUnit unit)(package private) voidawaitStopped()(package private) voidawaitStopped(long timeout, java.util.concurrent.TimeUnit unit)(package private) voidcheckHealthy()(package private) voiddispatchListenerEvents()Attempts to execute all the listeners inlisteners.(package private) voidenqueueFailedEvent(Service service)(package private) voidenqueueHealthyEvent()(package private) voidenqueueStoppedEvent()(package private) voidmarkReady()Marks theService.Stateas ready to receive transitions.(package private) ImmutableSetMultimap<Service.State,Service>servicesByState()(package private) ImmutableMap<Service,java.lang.Long>startupTimes()(package private) voidtransitionService(Service service, Service.State from, Service.State to)Updates the state with the given service transition.(package private) voidtryStartTiming(Service service)Attempts to start the timer immediately prior to the service being started viaService.startAsync().
-
-
-
Field Detail
-
monitor
final Monitor monitor
-
servicesByState
final SetMultimap<Service.State,Service> servicesByState
-
states
final Multiset<Service.State> states
-
ready
boolean ready
These two booleans are used to mark the state as ready to start.ready: is set bymarkReady()to indicate that all listeners have been correctly installedtransitioned: is set bytransitionService(com.google.common.util.concurrent.Service, com.google.common.util.concurrent.Service.State, com.google.common.util.concurrent.Service.State)to indicate that some transition has been performed.Together, they allow us to enforce that all services have their listeners installed prior to any service performing a transition, then we can fail in the ServiceManager constructor rather than in a Service.Listener callback.
-
transitioned
boolean transitioned
-
numberOfServices
final int numberOfServices
-
awaitHealthGuard
final Monitor.Guard awaitHealthGuard
Controls how long to wait for all the services to either become healthy or reach a state from which it is guaranteed that it can never become healthy.
-
stoppedGuard
final Monitor.Guard stoppedGuard
Controls how long to wait for all services to reach a terminal state.
-
listeners
final ListenerCallQueue<ServiceManager.Listener> listeners
The listeners to notify during a state transition.
-
-
Constructor Detail
-
ServiceManagerState
ServiceManagerState(ImmutableCollection<Service> services)
It is implicitly assumed that all the services are NEW and that they will all remain NEW until all the Listeners are installed andmarkReady()is called. It is our caller's responsibility to only callmarkReady()if all services were new at the time this method was called and when all the listeners were installed.
-
-
Method Detail
-
tryStartTiming
void tryStartTiming(Service service)
Attempts to start the timer immediately prior to the service being started viaService.startAsync().
-
markReady
void markReady()
Marks theService.Stateas ready to receive transitions. Returns true if no transitions have been observed yet.
-
addListener
void addListener(ServiceManager.Listener listener, java.util.concurrent.Executor executor)
-
awaitHealthy
void awaitHealthy()
-
awaitHealthy
void awaitHealthy(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException- Throws:
java.util.concurrent.TimeoutException
-
awaitStopped
void awaitStopped()
-
awaitStopped
void awaitStopped(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException- Throws:
java.util.concurrent.TimeoutException
-
servicesByState
ImmutableSetMultimap<Service.State,Service> servicesByState()
-
startupTimes
ImmutableMap<Service,java.lang.Long> startupTimes()
-
transitionService
void transitionService(Service service, Service.State from, Service.State to)
Updates the state with the given service transition.This method performs the main logic of ServiceManager in the following steps.
- Update the
servicesByState() - Update the
startupTimers - Based on the new state queue listeners to run
- Run the listeners (outside of the lock)
- Update the
-
enqueueStoppedEvent
void enqueueStoppedEvent()
-
enqueueHealthyEvent
void enqueueHealthyEvent()
-
enqueueFailedEvent
void enqueueFailedEvent(Service service)
-
dispatchListenerEvents
void dispatchListenerEvents()
Attempts to execute all the listeners inlisteners.
-
checkHealthy
void checkHealthy()
-
-