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.Object
An encapsulation of all the mutable state of theServiceManager
that needs to be accessed by instances ofServiceManager.ServiceListener
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
ServiceManager.ServiceManagerState.AwaitHealthGuard
(package private) class
ServiceManager.ServiceManagerState.StoppedGuard
-
Field Summary
Fields Modifier and Type Field Description (package private) 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.(package private) java.util.List<ListenerCallQueue<ServiceManager.Listener>>
listeners
The listeners to notify during a state transition.(package private) Monitor
monitor
(package private) int
numberOfServices
(package private) boolean
ready
These 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.Guard
stoppedGuard
Controls how long to wait for all services to reach a terminal state.(package private) boolean
transitioned
-
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) void
addListener(ServiceManager.Listener listener, java.util.concurrent.Executor executor)
(package private) void
awaitHealthy()
(package private) void
awaitHealthy(long timeout, java.util.concurrent.TimeUnit unit)
(package private) void
awaitStopped()
(package private) void
awaitStopped(long timeout, java.util.concurrent.TimeUnit unit)
(package private) void
checkHealthy()
(package private) void
executeListeners()
Attempts to execute all the listeners inlisteners
.(package private) void
fireFailedListeners(Service service)
(package private) void
fireHealthyListeners()
(package private) void
fireStoppedListeners()
(package private) void
markReady()
Marks theService.State
as ready to receive transitions.(package private) ImmutableMultimap<Service.State,Service>
servicesByState()
(package private) ImmutableMap<Service,java.lang.Long>
startupTimes()
(package private) void
transitionService(Service service, Service.State from, Service.State to)
Updates the state with the given service transition.(package private) void
tryStartTiming(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 java.util.List<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.State
as 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
ImmutableMultimap<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
-
fireStoppedListeners
void fireStoppedListeners()
-
fireHealthyListeners
void fireHealthyListeners()
-
fireFailedListeners
void fireFailedListeners(Service service)
-
executeListeners
void executeListeners()
Attempts to execute all the listeners inlisteners
.
-
checkHealthy
void checkHealthy()
-
-