Interface ApiService (2.33.0) Stay organized with collections Save and categorize content based on your preferences.
publicinterfaceApiServiceAn 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 | |
|---|---|
| Name | Description |
listener | ApiService.Listenerthe listener to run when the service changes state is complete |
executor | Executorthe 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 | |
|---|---|
| Name | Description |
timeout | longthe maximum time to wait |
unit | TimeUnitthe time unit of the timeout argument |
| Exceptions | |
|---|---|
| Type | Description |
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 | |
|---|---|
| Name | Description |
timeout | longthe maximum time to wait |
unit | TimeUnitthe time unit of the timeout argument |
| Exceptions | |
|---|---|
| Type | Description |
TimeoutException | if the service has not reached the given state within the deadline |
failureCause()
publicabstractThrowablefailureCause()Returns theThrowable that caused this service to fail.
| Returns | |
|---|---|
| Type | Description |
Throwable | |
isRunning()
publicabstractbooleanisRunning()Returnstrue if this service isrunning.
| Returns | |
|---|---|
| Type | Description |
boolean | |
startAsync()
publicabstractApiServicestartAsync()If the service state isState#NEW, this initiates service startup and returns immediately. A stopped service may not be restarted.
| Returns | |
|---|---|
| Type | Description |
ApiService | this |
state()
publicabstractApiService.Statestate()Returns the lifecycle state of the service.
| Returns | |
|---|---|
| Type | Description |
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 | |
|---|---|
| Type | Description |
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.