Top Description Fields Constructors Methods
com.google.common.util.concurrent

public abstract Class AbstractExecutionThreadService

extends Object
implements Service
Class Inheritance
All Implemented Interfaces
com.google.common.util.concurrent.Service
Annotations
@GwtIncompatible
@J2ktIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.util.concurrent.Platform.restoreInterruptIfIsInterruptedException

Base class for services that can implement startUp, run and shutDown methods. This class uses a single thread to execute the service; consider AbstractService if you would like to manage any threading manually.
Author
Jesse Wilson
Since
1.0

Field Summary

Modifier and TypeField and Description
private final Service
private static final Logger

Constructor Summary

AccessConstructor and Description
protected
AbstractExecutionThreadService()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public final 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
)

Implements com.google.common.util.concurrent.Service.addListener.

Registers a Listener to be executed on the given executor.
public final void
public final void
awaitRunning(Duration
the maximum time to wait
timeout
)

Overrides default com.google.common.util.concurrent.Service.awaitRunning.

Waits for the Service to reach the running state for no more than the given time.
public final void
awaitRunning(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Implements com.google.common.util.concurrent.Service.awaitRunning.

Waits for the Service to reach the running state for no more than the given time.
public final void
public final void
awaitTerminated(Duration
the maximum time to wait
timeout
)

Overrides default com.google.common.util.concurrent.Service.awaitTerminated.

Waits for the Service to reach a terminal state (either terminated or failed) for no more than the given time.
public final void
awaitTerminated(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Implements com.google.common.util.concurrent.Service.awaitTerminated.

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

Returns the Executor that will be used to run this service.

public final Throwable
failureCause()

Implements com.google.common.util.concurrent.Service.failureCause.

Returns the Throwable that caused this service to fail.
public final boolean
isRunning()

Implements com.google.common.util.concurrent.Service.isRunning.

Returns true if this service is running.
protected abstract void
run()

Run the service.

protected String
serviceName()

Returns the name of this service.

protected void
shutDown()

Stop the service.

public final Service
startAsync()

Implements com.google.common.util.concurrent.Service.startAsync.

If the service state is State#NEW, this initiates service startup and returns immediately.
protected void
startUp()

Start the service.

public final Service.State
state()

Implements com.google.common.util.concurrent.Service.state.

Returns the lifecycle state of the service.
public final Service
stopAsync()

Implements com.google.common.util.concurrent.Service.stopAsync.

If the service is starting or running, this initiates service shutdown and returns immediately.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
protected void
triggerShutdown()

Invoked to request the service to stop.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

delegateback to summary
private final Service delegate
loggerback to summary
private static final Logger logger

Constructor Detail

AbstractExecutionThreadServiceback to summary
protected AbstractExecutionThreadService()

Constructor for use by subclasses.

Method Detail

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

Implements com.google.common.util.concurrent.Service.addListener.

Doc from com.google.common.util.concurrent.Service.addListener.

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.

Annotations
@Override
Since
13.0
awaitRunningback to summary
public final void awaitRunning()

Implements com.google.common.util.concurrent.Service.awaitRunning.

Doc from com.google.common.util.concurrent.Service.awaitRunning.

Waits for the Service to reach the running state.

Annotations
@Override
Since
15.0
awaitRunningback to summary
public final void awaitRunning(Duration timeout) throws TimeoutException

Overrides default com.google.common.util.concurrent.Service.awaitRunning.

Doc from com.google.common.util.concurrent.Service.awaitRunning.

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

Parameters
timeout:Duration

the maximum time to wait

Annotations
@Override
Exceptions
TimeoutException:
if the service has not reached the given state within the deadline
Since
28.0
awaitRunningback to summary
public final void awaitRunning(long timeout, TimeUnit unit) throws TimeoutException

Implements com.google.common.util.concurrent.Service.awaitRunning.

Doc from com.google.common.util.concurrent.Service.awaitRunning.

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
@Override
Exceptions
TimeoutException:
if the service has not reached the given state within the deadline
Since
15.0
awaitTerminatedback to summary
public final void awaitTerminated()

Implements com.google.common.util.concurrent.Service.awaitTerminated.

Doc from com.google.common.util.concurrent.Service.awaitTerminated.

Waits for the Service to reach the terminated state.

Annotations
@Override
Since
15.0
awaitTerminatedback to summary
public final void awaitTerminated(Duration timeout) throws TimeoutException

Overrides default com.google.common.util.concurrent.Service.awaitTerminated.

Doc from com.google.common.util.concurrent.Service.awaitTerminated.

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

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

Implements com.google.common.util.concurrent.Service.awaitTerminated.

Doc from com.google.common.util.concurrent.Service.awaitTerminated.

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
@Override
Exceptions
TimeoutException:
if the service has not reached the given state within the deadline
Since
15.0
executorback to summary
protected Executor executor()

Returns the Executor that will be used to run this service. Subclasses may override this method to use a custom Executor, which may configure its worker thread with a specific name, thread group or priority. The returned executor's execute() method is called when this service is started, and should return promptly.

The default implementation returns a new Executor that sets the name of its threads to the string returned by serviceName

failureCauseback to summary
public final Throwable failureCause()

Implements com.google.common.util.concurrent.Service.failureCause.

Doc from com.google.common.util.concurrent.Service.failureCause.

Returns the Throwable that caused this service to fail.

Annotations
@Override
Since
14.0
isRunningback to summary
public final boolean isRunning()

Implements com.google.common.util.concurrent.Service.isRunning.

Doc from com.google.common.util.concurrent.Service.isRunning.

Returns true if this service is running.

Annotations
@Override
runback to summary
protected abstract void run() throws Exception

Run the service. This method is invoked on the execution thread. Implementations must respond to stop requests. You could poll for lifecycle changes in a work loop:

  public void run() {
    while (isRunning()) {
      // perform a unit of work
    }
  }

...or you could respond to stop requests by implementing triggerShutdown(), which should cause run() to return.

serviceNameback to summary
protected String serviceName()

Returns the name of this service. AbstractExecutionThreadService may include the name in debugging output.

Subclasses may override this method.

Since
14.0 (present in 10.0 as getServiceName)
shutDownback to summary
protected void shutDown() throws Exception

Stop the service. This method is invoked on the execution thread.

By default this method does nothing.

startAsyncback to summary
public final Service startAsync()

Implements com.google.common.util.concurrent.Service.startAsync.

Doc from com.google.common.util.concurrent.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
@Override
Since
15.0
startUpback to summary
protected void startUp() throws Exception

Start the service. This method is invoked on the execution thread.

By default this method does nothing.

stateback to summary
public final Service.State state()

Implements com.google.common.util.concurrent.Service.state.

Doc from com.google.common.util.concurrent.Service.state.

Returns the lifecycle state of the service.

Annotations
@Override
stopAsyncback to summary
public final Service stopAsync()

Implements com.google.common.util.concurrent.Service.stopAsync.

Doc from com.google.common.util.concurrent.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
@Override
Since
15.0
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
triggerShutdownback to summary
protected void triggerShutdown()

Invoked to request the service to stop.

By default this method does nothing.

Currently, this method is invoked while holding a lock. If an implementation of this method blocks, it can prevent this service from changing state. If you need to performing a blocking operation in order to trigger shutdown, consider instead registering a listener and implementing stopping. Note, however, that stopping does not run at exactly the same times as triggerShutdown.