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 if ApiService#stopAsync is
called before the ApiService 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 AbstractApiService
which implement this interface and make the threading and state management easier.
Similar to Guava's Service, but redeclared so that Guava could be shaded.
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.
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThis webpage details the \u003ccode\u003eApiService\u003c/code\u003e interface, a Java object with operational states and lifecycle methods for asynchronous start and stop actions.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eApiService\u003c/code\u003e transitions through distinct states: NEW, STARTING, RUNNING, STOPPING, and TERMINATED, with potential deviations leading to FAILED, and once in FAILED or TERMINATED it cannot leave these states.\u003c/p\u003e\n"],["\u003cp\u003eListeners can be registered to monitor state changes, with callbacks invoked upon transitions, but previous state changes will not be replayed to them, and listeners may execute concurrently.\u003c/p\u003e\n"],["\u003cp\u003eThe page lists various versions of the \u003ccode\u003eApiService\u003c/code\u003e, with 2.46.1 being the latest, and provides links to documentation for each version.\u003c/p\u003e\n"],["\u003cp\u003eThe interface offers methods for awaiting the RUNNING or TERMINATED states, as well as checking if it is running and stopping or starting the service.\u003c/p\u003e\n"]]],[],null,["# Interface ApiService (2.52.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.52.0 (latest)](/java/docs/reference/api-common/latest/com.google.api.core.ApiService)\n- [2.51.2](/java/docs/reference/api-common/2.51.2/com.google.api.core.ApiService)\n- [2.50.0](/java/docs/reference/api-common/2.50.0/com.google.api.core.ApiService)\n- [2.49.0](/java/docs/reference/api-common/2.49.0/com.google.api.core.ApiService)\n- [2.48.0](/java/docs/reference/api-common/2.48.0/com.google.api.core.ApiService)\n- [2.46.1](/java/docs/reference/api-common/2.46.1/com.google.api.core.ApiService)\n- [2.45.0](/java/docs/reference/api-common/2.45.0/com.google.api.core.ApiService)\n- [2.44.0](/java/docs/reference/api-common/2.44.0/com.google.api.core.ApiService)\n- [2.43.0](/java/docs/reference/api-common/2.43.0/com.google.api.core.ApiService)\n- [2.42.1](/java/docs/reference/api-common/2.42.1/com.google.api.core.ApiService)\n- [2.41.0](/java/docs/reference/api-common/2.41.0/com.google.api.core.ApiService)\n- [2.40.0](/java/docs/reference/api-common/2.40.0/com.google.api.core.ApiService)\n- [2.38.0](/java/docs/reference/api-common/2.38.0/com.google.api.core.ApiService)\n- [2.37.1](/java/docs/reference/api-common/2.37.1/com.google.api.core.ApiService)\n- [2.36.0](/java/docs/reference/api-common/2.36.0/com.google.api.core.ApiService)\n- [2.35.0](/java/docs/reference/api-common/2.35.0/com.google.api.core.ApiService)\n- [2.34.0](/java/docs/reference/api-common/2.34.0/com.google.api.core.ApiService)\n- [2.33.0](/java/docs/reference/api-common/2.33.0/com.google.api.core.ApiService)\n- [2.32.0](/java/docs/reference/api-common/2.32.0/com.google.api.core.ApiService)\n- [2.31.1](/java/docs/reference/api-common/2.31.1/com.google.api.core.ApiService)\n- [2.30.0](/java/docs/reference/api-common/2.30.0/com.google.api.core.ApiService)\n- [2.29.1](/java/docs/reference/api-common/2.29.1/com.google.api.core.ApiService)\n- [2.28.0](/java/docs/reference/api-common/2.28.0/com.google.api.core.ApiService)\n- [2.26.0](/java/docs/reference/api-common/2.26.0/com.google.api.core.ApiService)\n- [2.25.0](/java/docs/reference/api-common/2.25.0/com.google.api.core.ApiService)\n- [2.24.0](/java/docs/reference/api-common/2.24.0/com.google.api.core.ApiService)\n- [2.22.0](/java/docs/reference/api-common/2.22.0/com.google.api.core.ApiService)\n- [2.21.0](/java/docs/reference/api-common/2.21.0/com.google.api.core.ApiService)\n- [2.20.0](/java/docs/reference/api-common/2.20.0/com.google.api.core.ApiService)\n- [2.19.0](/java/docs/reference/api-common/2.19.0/com.google.api.core.ApiService)\n- [2.18.0](/java/docs/reference/api-common/2.18.0/com.google.api.core.ApiService)\n- [2.17.1](/java/docs/reference/api-common/2.17.1/com.google.api.core.ApiService)\n- [2.16.0](/java/docs/reference/api-common/2.16.0/com.google.api.core.ApiService)\n- [2.15.1](/java/docs/reference/api-common/2.15.1/com.google.api.core.ApiService)\n- [2.14.1](/java/docs/reference/api-common/2.14.1/com.google.api.core.ApiService)\n- [2.13.1](/java/docs/reference/api-common/2.13.1/com.google.api.core.ApiService)\n- [2.7.0](/java/docs/reference/api-common/2.7.0/com.google.api.core.ApiService)\n- [2.6.3](/java/docs/reference/api-common/2.6.3/com.google.api.core.ApiService)\n- [2.5.0](/java/docs/reference/api-common/2.5.0/com.google.api.core.ApiService)\n- [2.4.0](/java/docs/reference/api-common/2.4.0/com.google.api.core.ApiService)\n- [2.3.1](/java/docs/reference/api-common/2.3.1/com.google.api.core.ApiService)\n- [2.1.2](/java/docs/reference/api-common/2.1.2/com.google.api.core.ApiService) \n\n public interface ApiService\n\nAn object with an operational state, plus asynchronous [#startAsync()](/java/docs/reference/api-common/latest/com.google.api.core.ApiService#com_google_api_core_ApiService_startAsync__) and [#stopAsync()](/java/docs/reference/api-common/latest/com.google.api.core.ApiService#com_google_api_core_ApiService_stopAsync__) lifecycle methods to transition between states. Example services include\nwebservers, RPC servers and timers.\n\nThe normal lifecycle of a service is:\n\n- NEW -\\\u003e\n- STARTING -\\\u003e\n- RUNNING -\\\u003e\n- STOPPING -\\\u003e\n- TERMINATED\n\nThere are deviations from this if there are failures or if [ApiService#stopAsync](/java/docs/reference/api-common/latest/com.google.api.core.ApiService#com_google_api_core_ApiService_stopAsync_) is\ncalled before the [ApiService](/java/docs/reference/api-common/latest/com.google.api.core.ApiService) reaches the RUNNING state. The\nset of legal transitions form a [DAG](http://en.wikipedia.org/wiki/Directed_acyclic_graph), therefore every method of the\nlistener will be called at most once. N.B. The State#FAILED and State#TERMINATED\nstates are terminal states, once a service enters either of these states it cannot ever leave\nthem.\n\nImplementors of this interface are strongly encouraged to extend [AbstractApiService](/java/docs/reference/api-common/latest/com.google.api.core.AbstractApiService)\nwhich implement this interface and make the threading and state management easier.\n\nSimilar to Guava's `Service`, but redeclared so that Guava could be shaded.\n\nMethods\n-------\n\n### addListener(ApiService.Listener listener, Executor executor)\n\n public abstract void addListener(ApiService.Listener listener, Executor executor)\n\nRegisters a Listener to be executed on the given\nexecutor. The listener will have the corresponding transition method called whenever the\nservice changes state. The listener will not have previous state changes replayed, so it is\nsuggested that listeners are added before the service starts.\n\n`addListener` guarantees execution ordering across calls to a given listener but not\nacross calls to multiple listeners. Specifically, a given listener will have its callbacks\ninvoked in the same order as the underlying service enters those states. Additionally, at most\none of the listener's callbacks will execute at once. However, multiple listeners' callbacks\nmay execute concurrently, and listeners may execute in an order different from the one in which\nthey were registered.\n\nRuntimeExceptions thrown by a listener will be caught and logged. Any exception thrown\nduring `Executor.execute` (e.g., a `RejectedExecutionException`) will be caught and\nlogged.\n\n### awaitRunning()\n\n public abstract void awaitRunning()\n\nWaits for the [ApiService](/java/docs/reference/api-common/latest/com.google.api.core.ApiService) to reach the running state.\n\n### awaitRunning(long timeout, TimeUnit unit)\n\n public abstract void awaitRunning(long timeout, TimeUnit unit)\n\nWaits for the [ApiService](/java/docs/reference/api-common/latest/com.google.api.core.ApiService) to reach the running state for no\nmore than the given time.\n\n### awaitTerminated()\n\n public abstract void awaitTerminated()\n\nWaits for the [ApiService](/java/docs/reference/api-common/latest/com.google.api.core.ApiService) to reach the terminated state.\n\n### awaitTerminated(long timeout, TimeUnit unit)\n\n public abstract void awaitTerminated(long timeout, TimeUnit unit)\n\nWaits for the [ApiService](/java/docs/reference/api-common/latest/com.google.api.core.ApiService) to reach a terminal state (either terminated or failed) for no more than the given time.\n\n### failureCause()\n\n public abstract Throwable failureCause()\n\nReturns the [Throwable](https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html) that caused this service to fail.\n\n### isRunning()\n\n public abstract boolean isRunning()\n\nReturns `true` if this service is running.\n\n### startAsync()\n\n public abstract ApiService startAsync()\n\nIf the service state is State#NEW, this initiates service startup and returns\nimmediately. A stopped service may not be restarted.\n\n### state()\n\n public abstract ApiService.State state()\n\nReturns the lifecycle state of the service.\n\n### stopAsync()\n\n public abstract ApiService stopAsync()\n\nIf the service is starting or running,\nthis initiates service shutdown and returns immediately. If the service is new, it is terminated without having been started nor\nstopped. If the service has already been stopped, this method returns immediately without\ntaking action."]]