Top Description Inners Methods
com.google.common.util.concurrent

public Interface Service

Known Direct Implementers
com.google.common.util.concurrent.AbstractExecutionThreadService, com.google.common.util.concurrent.AbstractIdleService, com.google.common.util.concurrent.AbstractScheduledService, com.google.common.util.concurrent.AbstractService
Annotations
@DoNotMock:Create an AbstractIdleService
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.util.concurrent.Internal.toNanosSaturated

An object with an operational state, plus asynchronous 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.

Authors
Jesse Wilson, Luke Sandberg
Since
9.0 (in 1.0 as com.google.common.base.Service)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
Service.Listener

A listener for the various state changes that a Service goes through in its lifecycle.

public static enum
Service.State

The lifecycle states of a service.

Method Summary

Modifier and TypeMethod and Description
public void
addListener(Service.Listener
the listener to run when the service changes state is complete
listener
,
Executor
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 MoreExecutors#directExecutor.
executor
)

Registers a Listener to be executed on the given executor.

public void
awaitRunning()

Waits for the Service to reach the running state.

public default void
awaitRunning(Duration
the maximum time to wait
timeout
)

Waits for the Service to reach the running state for no more than the given time.

public void
awaitRunning(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Waits for the Service to reach the running state for no more than the given time.

public void
awaitTerminated()

Waits for the Service to reach the terminated state.

public default void
awaitTerminated(Duration
the maximum time to wait
timeout
)

Waits for the Service to reach a terminal state (either terminated or failed) for no more than the given time.

public void
awaitTerminated(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Waits for the Service to reach a terminal state (either terminated or failed) for no more than the given time.

public Throwable
failureCause()

Returns the Throwable that caused this service to fail.

public boolean
isRunning()

Returns true if this service is running.

public Service

Returns:

this
startAsync
()

If the service state is State#NEW, this initiates service startup and returns immediately.

public Service.State
state()

Returns the lifecycle state of the service.

public Service

Returns:

this
stopAsync
()

If the service is starting or running, this initiates service shutdown and returns immediately.

Method Detail

addListenerback to summary
public void addListener(Service.Listener listener, Executor executor)

Registers a Listener to be executed on the given executor. The listener will have the corresponding transition method called whenever the service changes state. The listener will not have previous state changes replayed, so it is suggested that listeners are added before the service starts.

addListener guarantees execution ordering across calls to a given listener but not across calls to multiple listeners. Specifically, a given listener will have its callbacks invoked in the same order as the underlying service enters those states. Additionally, at most one of the listener's callbacks will execute at once. However, multiple listeners' callbacks may execute concurrently, and listeners may execute in an order different from the one in which they were registered.

RuntimeExceptions thrown by a listener will be caught and logged. Any exception thrown during Executor.execute (e.g., a RejectedExecutionException) will be caught and logged.

Parameters
listener:Service.Listener

the listener to run when the service changes state is complete

executor:Executor

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 MoreExecutors#directExecutor.

Since
13.0
awaitRunningback to summary
public void awaitRunning()

Waits for the Service to reach the running state.

Exceptions
IllegalStateException:
if the service reaches a state from which it is not possible to enter the State#RUNNING state. e.g. if the state is State#TERMINATED when this method is called then this will throw an IllegalStateException.
Since
15.0
awaitRunningback to summary
public default void awaitRunning(Duration timeout) throws TimeoutException

Waits for the Service to reach the running state for no more than the given time.

Parameters
timeout:Duration

the maximum time to wait

Exceptions
TimeoutException:
if the service has not reached the given state within the deadline
IllegalStateException:
if the service reaches a state from which it is not possible to enter the RUNNING state. e.g. if the state is State#TERMINATED when this method is called then this will throw an IllegalStateException.
Since
28.0
awaitRunningback to summary
public void awaitRunning(long timeout, TimeUnit unit) throws TimeoutException

Waits for the Service to reach the running state for no more than the given time.

Parameters
timeout:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Annotations
@SuppressWarnings:GoodTime
Exceptions
TimeoutException:
if the service has not reached the given state within the deadline
IllegalStateException:
if the service reaches a state from which it is not possible to enter the RUNNING state. e.g. if the state is State#TERMINATED when this method is called then this will throw an IllegalStateException.
Since
15.0
awaitTerminatedback to summary
public void awaitTerminated()

Waits for the Service to reach the terminated state.

Exceptions
IllegalStateException:
if the service fails.
Since
15.0
awaitTerminatedback to summary
public default void awaitTerminated(Duration timeout) throws TimeoutException

Waits for the Service to reach a terminal state (either terminated or failed) for no more than the given time.

Parameters
timeout:Duration

the maximum time to wait

Exceptions
TimeoutException:
if the service has not reached the given state within the deadline
IllegalStateException:
if the service fails.
Since
28.0
awaitTerminatedback to summary
public void awaitTerminated(long timeout, TimeUnit unit) throws TimeoutException

Waits for the Service to reach a terminal state (either terminated or failed) for no more than the given time.

Parameters
timeout:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Annotations
@SuppressWarnings:GoodTime
Exceptions
TimeoutException:
if the service has not reached the given state within the deadline
IllegalStateException:
if the service fails.
Since
15.0
failureCauseback to summary
public Throwable failureCause()

Returns the Throwable that caused this service to fail.

Exceptions
IllegalStateException:
if this service's state isn't FAILED.
Since
14.0
isRunningback to summary
public boolean isRunning()

Returns true if this service is running.

startAsyncback to summary
public Service startAsync()

If the service state is State#NEW, this initiates service startup and returns immediately. A stopped service may not be restarted.

Returns:Service

this

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalStateException:
if the service is not State#NEW
Since
15.0
stateback to summary
public Service.State state()

Returns the lifecycle state of the service.

stopAsyncback 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.

Returns:Service

this

Annotations
@CanIgnoreReturnValue
Since
15.0
com.google.common.util.concurrent back to summary

public abstract Class Service.Listener

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.util.concurrent.ServiceManager.ServiceListener

A listener for the various state changes that a Service goes through in its lifecycle.

All methods are no-ops by default, implementors should override the ones they care about.

Author
Luke Sandberg
Since
15.0 (present as an interface in 13.0)

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
failed(Service.State
The previous state that is being transitioned from. Failure can occur in any state with the exception of NEW or TERMINATED.
from
,
Throwable
The exception that caused the failure.
failure
)

Called when the service transitions to the FAILED state.

public void
running()

Called when the service transitions from STARTING to RUNNING.

public void
starting()

Called when the service transitions from NEW to STARTING.

public void
stopping(Service.State
The previous state that is being transitioned from.
from
)

Called when the service transitions to the STOPPING state.

public void
terminated(Service.State
The previous state that is being transitioned from. Failure can occur in any state with the exception of FAILED and TERMINATED.
from
)

Called when the service transitions to the TERMINATED state.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Listenerback to summary
public Listener()

Method Detail

failedback to summary
public void failed(Service.State from, Throwable failure)

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 Listener.

Parameters
from:Service.State

The previous state that is being transitioned from. Failure can occur in any state with the exception of NEW or TERMINATED.

failure:Throwable

The exception that caused the failure.

runningback to summary
public void running()

Called when the service transitions from STARTING to RUNNING. This occurs when a service has successfully started.

startingback to summary
public void starting()

Called when the service transitions from NEW to STARTING. This occurs when Service#startAsync is called the first time.

stoppingback to summary
public void stopping(Service.State from)

Called when the service transitions to the STOPPING state. The only valid values for from are STARTING or RUNNING. This occurs when Service#stopAsync is called.

Parameters
from:Service.State

The previous state that is being transitioned from.

terminatedback to summary
public void terminated(Service.State from)

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 Listener.

Parameters
from:Service.State

The previous state that is being transitioned from. Failure can occur in any state with the exception of FAILED and TERMINATED.

com.google.common.util.concurrent back to summary

public final Enum Service.State

extends Enum<Service.State>
Class Inheritance

The lifecycle states of a service.

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.

Since
9.0 (in 1.0 as com.google.common.base.Service.State)

Field Summary

Modifier and TypeField and Description
public static final Service.State
FAILED

A service in this state has encountered a problem and may not be operational.

public static final Service.State
NEW

A service in this state is inactive.

public static final Service.State
RUNNING

A service in this state is operational.

public static final Service.State
STARTING

A service in this state is transitioning to RUNNING.

public static final Service.State
STOPPING

A service in this state is transitioning to TERMINATED.

public static final Service.State
TERMINATED

A service in this state has completed execution normally.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Service.State
public static Service.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

FAILEDback to summary
public static final Service.State FAILED

A service in this state has encountered a problem and may not be operational. It cannot be started nor stopped.

NEWback to summary
public static final Service.State NEW

A service in this state is inactive. It does minimal work and consumes minimal resources.

RUNNINGback to summary
public static final Service.State RUNNING

A service in this state is operational.

STARTINGback to summary
public static final Service.State STARTING

A service in this state is transitioning to RUNNING.

STOPPINGback to summary
public static final Service.State STOPPING

A service in this state is transitioning to TERMINATED.

TERMINATEDback to summary
public static final Service.State TERMINATED

A service in this state has completed execution normally. It does minimal work and consumes minimal resources.

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static Service.State valueOf(String name)
valuesback to summary
public static Service.State[] values()