Interface ApiService (2.33.0)

publicinterfaceApiService

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:

  • NEW ->
  • STARTING ->
  • RUNNING ->
  • STOPPING ->
  • TERMINATED

There are deviations from this if there are failures or ifApiService#stopAsync is called before theApiService reaches theRUNNING state. The set of legal transitions form aDAG, therefore every method of the listener will be called at most once. N.B. TheState#FAILED andState#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 extendAbstractApiService which implement this interface and make the threading and state management easier.

Similar to Guava'sService, but redeclared so that Guava could be shaded.

Methods

addListener(ApiService.Listener listener, Executor executor)

publicabstractvoidaddListener(ApiService.Listenerlistener,Executorexecutor)

Registers aListener to beexecuted 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 duringExecutor.execute (e.g., aRejectedExecutionException) will be caught and logged.

Parameters
NameDescription
listenerApiService.Listener

the listener to run when the service changes state is complete

executorExecutor

the executor in which the listeners callback methods will be run.

awaitRunning()

publicabstractvoidawaitRunning()

Waits for theApiService to reach therunning state.

awaitRunning(long timeout, TimeUnit unit)

publicabstractvoidawaitRunning(longtimeout,TimeUnitunit)

Waits for theApiService to reach therunning state for no more than the given time.

Parameters
NameDescription
timeoutlong

the maximum time to wait

unitTimeUnit

the time unit of the timeout argument

Exceptions
TypeDescription
TimeoutException

if the service has not reached the given state within the deadline

awaitTerminated()

publicabstractvoidawaitTerminated()

Waits for theApiService to reach theterminated state.

awaitTerminated(long timeout, TimeUnit unit)

publicabstractvoidawaitTerminated(longtimeout,TimeUnitunit)

Waits for theApiService to reach a terminal state (eitherterminated orfailed) for no more than the given time.

Parameters
NameDescription
timeoutlong

the maximum time to wait

unitTimeUnit

the time unit of the timeout argument

Exceptions
TypeDescription
TimeoutException

if the service has not reached the given state within the deadline

failureCause()

publicabstractThrowablefailureCause()

Returns theThrowable that caused this service to fail.

Returns
TypeDescription
Throwable

isRunning()

publicabstractbooleanisRunning()

Returnstrue if this service isrunning.

Returns
TypeDescription
boolean

startAsync()

publicabstractApiServicestartAsync()

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

Returns
TypeDescription
ApiService

this

state()

publicabstractApiService.Statestate()

Returns the lifecycle state of the service.

Returns
TypeDescription
ApiService.State

stopAsync()

publicabstractApiServicestopAsync()

If the service isstarting orrunning, this initiates service shutdown and returns immediately. If the service isnew, it isterminated without having been started nor stopped. If the service has already been stopped, this method returns immediately without taking action.

Returns
TypeDescription
ApiService

this

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-01-31 UTC.