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\u003eAbstractApiService\u003c/code\u003e class, a base class for \u003ccode\u003eApiService\u003c/code\u003e, offering similar functionality to Guava's \u003ccode\u003eAbstractService\u003c/code\u003e but adapted for shading.\u003c/p\u003e\n"],["\u003cp\u003eThe latest version of \u003ccode\u003eAbstractApiService\u003c/code\u003e is 2.46.1, with numerous previous versions from 2.45.0 all the way back to 2.1.2 listed for reference and access.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eAbstractApiService\u003c/code\u003e includes methods for managing the lifecycle of a service, such as \u003ccode\u003estartAsync()\u003c/code\u003e, \u003ccode\u003estopAsync()\u003c/code\u003e, \u003ccode\u003eawaitRunning()\u003c/code\u003e, and \u003ccode\u003eawaitTerminated()\u003c/code\u003e, in addition to adding a listener.\u003c/p\u003e\n"],["\u003cp\u003eThe class also provides methods for checking the current state of the service (\u003ccode\u003estate()\u003c/code\u003e, \u003ccode\u003eisRunning()\u003c/code\u003e) and retrieving the cause of failure, if applicable, via the \u003ccode\u003efailureCause()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThe methods \u003ccode\u003edoStart()\u003c/code\u003e and \u003ccode\u003edoStop()\u003c/code\u003e are protected and must be implemented by any derived class to define the specific startup and shutdown behavior of the service.\u003c/p\u003e\n"]]],[],null,["# Class AbstractApiService (2.52.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.52.0 (latest)](/java/docs/reference/api-common/latest/com.google.api.core.AbstractApiService)\n- [2.51.2](/java/docs/reference/api-common/2.51.2/com.google.api.core.AbstractApiService)\n- [2.50.0](/java/docs/reference/api-common/2.50.0/com.google.api.core.AbstractApiService)\n- [2.49.0](/java/docs/reference/api-common/2.49.0/com.google.api.core.AbstractApiService)\n- [2.48.0](/java/docs/reference/api-common/2.48.0/com.google.api.core.AbstractApiService)\n- [2.46.1](/java/docs/reference/api-common/2.46.1/com.google.api.core.AbstractApiService)\n- [2.45.0](/java/docs/reference/api-common/2.45.0/com.google.api.core.AbstractApiService)\n- [2.44.0](/java/docs/reference/api-common/2.44.0/com.google.api.core.AbstractApiService)\n- [2.43.0](/java/docs/reference/api-common/2.43.0/com.google.api.core.AbstractApiService)\n- [2.42.1](/java/docs/reference/api-common/2.42.1/com.google.api.core.AbstractApiService)\n- [2.41.0](/java/docs/reference/api-common/2.41.0/com.google.api.core.AbstractApiService)\n- [2.40.0](/java/docs/reference/api-common/2.40.0/com.google.api.core.AbstractApiService)\n- [2.38.0](/java/docs/reference/api-common/2.38.0/com.google.api.core.AbstractApiService)\n- [2.37.1](/java/docs/reference/api-common/2.37.1/com.google.api.core.AbstractApiService)\n- [2.36.0](/java/docs/reference/api-common/2.36.0/com.google.api.core.AbstractApiService)\n- [2.35.0](/java/docs/reference/api-common/2.35.0/com.google.api.core.AbstractApiService)\n- [2.34.0](/java/docs/reference/api-common/2.34.0/com.google.api.core.AbstractApiService)\n- [2.33.0](/java/docs/reference/api-common/2.33.0/com.google.api.core.AbstractApiService)\n- [2.32.0](/java/docs/reference/api-common/2.32.0/com.google.api.core.AbstractApiService)\n- [2.31.1](/java/docs/reference/api-common/2.31.1/com.google.api.core.AbstractApiService)\n- [2.30.0](/java/docs/reference/api-common/2.30.0/com.google.api.core.AbstractApiService)\n- [2.29.1](/java/docs/reference/api-common/2.29.1/com.google.api.core.AbstractApiService)\n- [2.28.0](/java/docs/reference/api-common/2.28.0/com.google.api.core.AbstractApiService)\n- [2.26.0](/java/docs/reference/api-common/2.26.0/com.google.api.core.AbstractApiService)\n- [2.25.0](/java/docs/reference/api-common/2.25.0/com.google.api.core.AbstractApiService)\n- [2.24.0](/java/docs/reference/api-common/2.24.0/com.google.api.core.AbstractApiService)\n- [2.22.0](/java/docs/reference/api-common/2.22.0/com.google.api.core.AbstractApiService)\n- [2.21.0](/java/docs/reference/api-common/2.21.0/com.google.api.core.AbstractApiService)\n- [2.20.0](/java/docs/reference/api-common/2.20.0/com.google.api.core.AbstractApiService)\n- [2.19.0](/java/docs/reference/api-common/2.19.0/com.google.api.core.AbstractApiService)\n- [2.18.0](/java/docs/reference/api-common/2.18.0/com.google.api.core.AbstractApiService)\n- [2.17.1](/java/docs/reference/api-common/2.17.1/com.google.api.core.AbstractApiService)\n- [2.16.0](/java/docs/reference/api-common/2.16.0/com.google.api.core.AbstractApiService)\n- [2.15.1](/java/docs/reference/api-common/2.15.1/com.google.api.core.AbstractApiService)\n- [2.14.1](/java/docs/reference/api-common/2.14.1/com.google.api.core.AbstractApiService)\n- [2.13.1](/java/docs/reference/api-common/2.13.1/com.google.api.core.AbstractApiService)\n- [2.7.0](/java/docs/reference/api-common/2.7.0/com.google.api.core.AbstractApiService)\n- [2.6.3](/java/docs/reference/api-common/2.6.3/com.google.api.core.AbstractApiService)\n- [2.5.0](/java/docs/reference/api-common/2.5.0/com.google.api.core.AbstractApiService)\n- [2.4.0](/java/docs/reference/api-common/2.4.0/com.google.api.core.AbstractApiService)\n- [2.3.1](/java/docs/reference/api-common/2.3.1/com.google.api.core.AbstractApiService)\n- [2.1.2](/java/docs/reference/api-common/2.1.2/com.google.api.core.AbstractApiService) \n\n public abstract class AbstractApiService implements ApiService\n\nBase class for [ApiService](/java/docs/reference/api-common/latest/com.google.api.core.ApiService). Similar to Guava's `AbstractService` but redeclared so\nthat Guava can be shaded. \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e AbstractApiService \n\nImplements\n----------\n\n[ApiService](/java/docs/reference/api-common/latest/com.google.api.core.ApiService) \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nConstructors\n------------\n\n### AbstractApiService()\n\n protected AbstractApiService()\n\nMethods\n-------\n\n### addListener(ApiService.Listener listener, Executor executor)\n\n public 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 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 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 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 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### doStart()\n\n protected abstract void doStart()\n\n### doStop()\n\n protected abstract void doStop()\n\n### failureCause()\n\n public 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 boolean isRunning()\n\nReturns `true` if this service is running.\n\n### notifyFailed(Throwable cause)\n\n protected void notifyFailed(Throwable cause)\n\n### notifyStarted()\n\n protected void notifyStarted()\n\n### notifyStopped()\n\n protected void notifyStopped()\n\n### startAsync()\n\n public 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 ApiService.State state()\n\nReturns the lifecycle state of the service.\n\n### stopAsync()\n\n public 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."]]