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

public abstract Class AbstractIdleService

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 do not need a thread while "running" but may need one during startup and shutdown. Subclasses can implement startUp and shutDown methods, each which run in an executor which by default uses a separate thread for each method.
Author
Chris Nokleberg
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class

Field Summary

Modifier and TypeField and Description
private final Service
private final Supplier<String>

Constructor Summary

AccessConstructor and Description
protected
AbstractIdleService()

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 String
serviceName()

Returns the name of this service.

protected abstract 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 abstract 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.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

delegateback to summary
private final Service delegate
threadNameSupplierback to summary
private final Supplier<String> threadNameSupplier

Constructor Detail

AbstractIdleServiceback to summary
protected AbstractIdleService()

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 stopped, and should return promptly.

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
serviceNameback to summary
protected String serviceName()

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

Since
14.0
shutDownback to summary
protected abstract void shutDown() throws Exception

Stop the service.

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 abstract void startUp() throws Exception

Start the service.

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
com.google.common.util.concurrent back to summary

private final Class AbstractIdleService.DelegateService

extends AbstractService
Class Inheritance
Annotations
@WeakOuter

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
protected final void
doStart()

Implements abstract com.google.common.util.concurrent.AbstractService.doStart.

This method is called by startAsync to initiate service startup.
protected final void
doStop()

Implements abstract com.google.common.util.concurrent.AbstractService.doStop.

This method should be used to initiate service shutdown.
public String
toString()

Overrides com.google.common.util.concurrent.AbstractService.toString.

Returns a string representation of the object.
Inherited from com.google.common.util.concurrent.AbstractService:
addListenerawaitRunningawaitRunningawaitRunningawaitTerminatedawaitTerminatedawaitTerminateddoCancelStartfailureCauseisRunningnotifyFailednotifyStartednotifyStoppedstartAsyncstatestopAsync

Constructor Detail

DelegateServiceback to summary
private DelegateService()

Method Detail

doStartback to summary
protected final void doStart()

Implements abstract com.google.common.util.concurrent.AbstractService.doStart.

Doc from com.google.common.util.concurrent.AbstractService.doStart.

This method is called by startAsync to initiate service startup. The invocation of this method should cause a call to notifyStarted(), either during this method's run, or after it has returned. If startup fails, the invocation should cause a call to notifyFailed(Throwable) instead.

This method should return promptly; prefer to do work on a different thread where it is convenient. It is invoked exactly once on service startup, even when startAsync is called multiple times.

Annotations
@Override
doStopback to summary
protected final void doStop()

Implements abstract com.google.common.util.concurrent.AbstractService.doStop.

Doc from com.google.common.util.concurrent.AbstractService.doStop.

This method should be used to initiate service shutdown. The invocation of this method should cause a call to notifyStopped(), either during this method's run, or after it has returned. If shutdown fails, the invocation should cause a call to notifyFailed(Throwable) instead.

This method should return promptly; prefer to do work on a different thread where it is convenient. It is invoked exactly once on service shutdown, even when stopAsync is called multiple times.

If stopAsync is called on a State#STARTING service, this method is not invoked immediately. Instead, it will be deferred until after the service is State#RUNNING. Services that need to cancel startup work can override doCancelStart.

Annotations
@Override
toStringback to summary
public String toString()

Overrides com.google.common.util.concurrent.AbstractService.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
com.google.common.util.concurrent back to summary

private final Class AbstractIdleService.ThreadNameSupplier

extends Object
implements Supplier<String>
Class Inheritance
  • java.lang.Object
  • com.google.common.util.concurrent.AbstractIdleService.ThreadNameSupplier
All Implemented Interfaces
com.google.common.base.Supplier, java.util.function.Supplier
Annotations
@WeakOuter

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public String
get()

Implements com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ThreadNameSupplierback to summary
private ThreadNameSupplier()

Method Detail

getback to summary
public String get()

Implements com.google.common.base.Supplier.get.

Doc from com.google.common.base.Supplier.get.

Retrieves an instance of the appropriate type. The returned object may or may not be a new instance, depending on the implementation.

Returns:String

an instance of the appropriate type

Annotations
@Override