startAsync() and stopAsync() lifecycle methods to transition between states. Example services include
webservers, RPC servers and timers.
The normal lifecycle of a service is:
There are deviations from this if there are failures or if Service#stopAsync is called
before the Service reaches the RUNNING state. The set of legal
transitions form a DAG,
therefore every method of the listener will be called at most once. N.B. The State#FAILED
and State#TERMINATED states are terminal states, once a service enters either of these
states it cannot ever leave them.
Implementors of this interface are strongly encouraged to extend one of the abstract classes in this package which implement this interface and make the threading and state management easier.
com.google.common.base.Service)
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class | Service.
A listener for the various state changes that a |
| public static enum | Service.
The lifecycle states of a service. |
| Modifier and Type | Method and Description |
|---|---|
| public void | addListener(Service.
the listener to run when the service changes state is complete listener,the executor in which the listeners callback methods will be run. For fast,
lightweight listeners that would be safe to execute in any thread, consider executor)MoreExecutors#directExecutor. |
| public void | |
| public default void | awaitRunning(Duration
the maximum time to wait timeout)Waits for the |
| public void | awaitRunning(long
the maximum time to wait timeout, TimeUnit the time unit of the timeout argument unit)Waits for the |
| public void | |
| public default void | awaitTerminated(Duration
the maximum time to wait timeout)Waits for the |
| public void | awaitTerminated(long
the maximum time to wait timeout, TimeUnit the time unit of the timeout argument unit)Waits for the |
| public Throwable | |
| public boolean | |
| public Service | Returns: thisIf the service state is |
| public Service. | |
| public Service |
| addListener | back to summary |
|---|---|
| public void addListener(Service. Registers a
RuntimeExceptions thrown by a listener will be caught and logged. Any exception thrown
during
| |
| awaitRunning | back to summary |
|---|---|
| public void awaitRunning() Waits for the
| |
| awaitRunning | back to summary |
|---|---|
| public default void awaitRunning(Duration timeout) throws TimeoutException Waits for the
| |
| awaitRunning | back to summary |
|---|---|
| public void awaitRunning(long timeout, TimeUnit unit) throws TimeoutException Waits for the
| |
| awaitTerminated | back to summary |
|---|---|
| public void awaitTerminated() Waits for the
| |
| awaitTerminated | back to summary |
|---|---|
| public default void awaitTerminated(Duration timeout) throws TimeoutException Waits for the
| |
| awaitTerminated | back to summary |
|---|---|
| public void awaitTerminated(long timeout, TimeUnit unit) throws TimeoutException Waits for the
| |
| failureCause | back to summary |
|---|---|
| public Throwable failureCause() Returns the
| |
| isRunning | back to summary |
|---|---|
| public boolean isRunning() Returns | |
| startAsync | back to summary |
|---|---|
| public Service startAsync() If the service state is
| |
| state | back to summary |
|---|---|
| public Service. Returns the lifecycle state of the service. | |
| stopAsync | back to summary |
|---|---|
| public Service stopAsync() If the service is starting or running, this initiates service shutdown and returns immediately. If the service is new, it is terminated without having been started nor stopped. If the service has already been stopped, this method returns immediately without taking action.
| |
Service goes through in its lifecycle.
All methods are no-ops by default, implementors should override the ones they care about.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | |
| public void | |
| public void | terminated(Service.
The previous state that is being transitioned from. Failure can occur in any
state with the exception of FAILED and TERMINATED. fromCalled when the service transitions to the TERMINATED state. |
| Listener | back to summary |
|---|---|
| public Listener() | |
| failed | back to summary |
|---|---|
| public void failed(Service. Called when the service transitions to the FAILED state. The
FAILED state is a terminal state in the transition diagram.
Therefore, if this method is called, no other methods will be called on the
| |
| running | back to summary |
|---|---|
| public void running() Called when the service transitions from STARTING to RUNNING. This occurs when a service has successfully started. | |
| starting | back to summary |
|---|---|
| public void starting() Called when the service transitions from NEW to STARTING. This occurs when | |
| stopping | back to summary |
|---|---|
| public void stopping(Service. Called when the service transitions to the STOPPING state. The
only valid values for | |
| terminated | back to summary |
|---|---|
| public void terminated(Service. Called when the service transitions to the TERMINATED state.
The TERMINATED state is a terminal state in the transition
diagram. Therefore, if this method is called, no other methods will be called on the
| |
The ordering of the State enum is defined such that if there is a state transition
from A -> B then A.compareTo(B) < 0. N.B. The converse is not true, i.e. if
A.compareTo(B) < 0 then there is not guaranteed to be a valid state transition
A -> B.
com.google.common.base.Service.State)
| Modifier and Type | Field and Description |
|---|---|
| public static final Service. | FAILED
A service in this state has encountered a problem and may not be operational. |
| public static final Service. | NEW
A service in this state is inactive. |
| public static final Service. | RUNNING
A service in this state is operational. |
| public static final Service. | |
| public static final Service. | STOPPING
A service in this state is transitioning to |
| public static final Service. | TERMINATED
A service in this state has completed execution normally. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static Service. | |
| public static Service. |
| FAILED | back to summary |
|---|---|
| public static final Service. A service in this state has encountered a problem and may not be operational. It cannot be started nor stopped. | |
| NEW | back to summary |
|---|---|
| public static final Service. A service in this state is inactive. It does minimal work and consumes minimal resources. | |
| RUNNING | back to summary |
|---|---|
| public static final Service. A service in this state is operational. | |
| STARTING | back to summary |
|---|---|
| public static final Service. A service in this state is transitioning to | |
| STOPPING | back to summary |
|---|---|
| public static final Service. A service in this state is transitioning to | |
| TERMINATED | back to summary |
|---|---|
| public static final Service. A service in this state has completed execution normally. It does minimal work and consumes minimal resources. | |
| State | back to summary |
|---|---|
| private State() | |
| valueOf | back to summary |
|---|---|
| public static Service. | |
| values | back to summary |
|---|---|
| public static Service. | |