001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.util.concurrent;
018
019import com.google.common.annotations.Beta;
020
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.Executor;
023
024/**
025 * An object with an operational state, plus asynchronous {@link #start()} and {@link #stop()}
026 * lifecycle methods to transition between states. Example services include webservers, RPC servers
027 * and timers.
028 *
029 * <p>The normal lifecycle of a service is:
030 * <ul>
031 *   <li>{@linkplain State#NEW NEW} -&gt;
032 *   <li>{@linkplain State#STARTING STARTING} -&gt;
033 *   <li>{@linkplain State#RUNNING RUNNING} -&gt;
034 *   <li>{@linkplain State#STOPPING STOPPING} -&gt;
035 *   <li>{@linkplain State#TERMINATED TERMINATED}
036 * </ul>
037 *
038 * <p>There are deviations from this if there are failures or if {@link Service#stop} is called 
039 * before the {@link Service} reaches the {@linkplain State#RUNNING RUNNING} state. The set of legal
040 * transitions form a <a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph">DAG</a>, 
041 * therefore every method of the listener will be called at most once. N.B. The {@link State#FAILED}
042 * and {@link State#TERMINATED} states are terminal states, once a service enters either of these
043 * states it cannot ever leave them.
044 *
045 * <p>Implementors of this interface are strongly encouraged to extend one of the abstract classes 
046 * in this package which implement this interface and make the threading and state management 
047 * easier.
048 *
049 * @author Jesse Wilson
050 * @author Luke Sandberg
051 * @since 9.0 (in 1.0 as {@code com.google.common.base.Service})
052 */
053@Beta
054public interface Service {
055  /**
056   * If the service state is {@link State#NEW}, this initiates service startup and returns
057   * immediately. If the service has already been started, this method returns immediately without
058   * taking action. A stopped service may not be restarted.
059   *
060   * @return a future for the startup result, regardless of whether this call initiated startup.
061   *         Calling {@link ListenableFuture#get} will block until the service has finished
062   *         starting, and returns one of {@link State#RUNNING}, {@link State#STOPPING} or
063   *         {@link State#TERMINATED}. If the service fails to start, {@link ListenableFuture#get}
064   *         will throw an {@link ExecutionException}, and the service's state will be
065   *         {@link State#FAILED}. If it has already finished starting, {@link ListenableFuture#get}
066   *         returns immediately. Cancelling this future has no effect on the service.
067   */
068  ListenableFuture<State> start();
069
070  /**
071   * Initiates service startup (if necessary), returning once the service has finished starting.
072   * Unlike calling {@code start().get()}, this method throws no checked exceptions, and it cannot
073   * be {@linkplain Thread#interrupt interrupted}.
074   *
075   * @throws UncheckedExecutionException if startup failed
076   * @return the state of the service when startup finished.
077   */
078  State startAndWait();
079
080  /**
081   * Returns {@code true} if this service is {@linkplain State#RUNNING running}.
082   */
083  boolean isRunning();
084
085  /**
086   * Returns the lifecycle state of the service.
087   */
088  State state();
089
090  /**
091   * If the service is {@linkplain State#STARTING starting} or {@linkplain State#RUNNING running},
092   * this initiates service shutdown and returns immediately. If the service is
093   * {@linkplain State#NEW new}, it is {@linkplain State#TERMINATED terminated} without having been
094   * started nor stopped. If the service has already been stopped, this method returns immediately
095   * without taking action.
096   *
097   * @return a future for the shutdown result, regardless of whether this call initiated shutdown.
098   *         Calling {@link ListenableFuture#get} will block until the service has finished shutting
099   *         down, and either returns {@link State#TERMINATED} or throws an
100   *         {@link ExecutionException}. If it has already finished stopping,
101   *         {@link ListenableFuture#get} returns immediately. Cancelling this future has no effect
102   *         on the service.
103   */
104  ListenableFuture<State> stop();
105
106  /**
107   * Initiates service shutdown (if necessary), returning once the service has finished stopping. If
108   * this is {@link State#STARTING}, startup will be cancelled. If this is {@link State#NEW}, it is
109   * {@link State#TERMINATED terminated} without having been started nor stopped. Unlike calling
110   * {@code stop().get()}, this method throws no checked exceptions.
111   *
112   * @throws UncheckedExecutionException if the service has failed or fails during shutdown
113   * @return the state of the service when shutdown finished.
114   */
115  State stopAndWait();
116
117  /**
118   * Returns the {@link Throwable} that caused this service to fail.
119   * 
120   * @throws IllegalStateException if this service's state isn't {@linkplain State#FAILED FAILED}.
121   *
122   * @since 14.0
123   */
124  Throwable failureCause();
125  
126  /**
127   * Registers a {@link Listener} to be {@linkplain Executor#execute executed} on the given 
128   * executor.  The listener will have the corresponding transition method called whenever the 
129   * service changes state. The listener will not have previous state changes replayed, so it is 
130   * suggested that listeners are added before the service starts.
131   *
132   * <p>There is no guaranteed ordering of execution of listeners, but any listener added through 
133   * this method is guaranteed to be called whenever there is a state change.
134   *
135   * <p>Exceptions thrown by a listener will be propagated up to the executor. Any exception thrown 
136   * during {@code Executor.execute} (e.g., a {@code RejectedExecutionException} or an exception 
137   * thrown by {@linkplain MoreExecutors#sameThreadExecutor inline execution}) will be caught and
138   * logged.
139   * 
140   * @param listener the listener to run when the service changes state is complete
141   * @param executor the executor in which the the listeners callback methods will be run. For fast,
142   *     lightweight listeners that would be safe to execute in any thread, consider 
143   *     {@link MoreExecutors#sameThreadExecutor}.
144   * @since 13.0
145   */
146  void addListener(Listener listener, Executor executor);
147  
148  /**
149   * The lifecycle states of a service.
150   *
151   * @since 9.0 (in 1.0 as {@code com.google.common.base.Service.State})
152   */
153  @Beta // should come out of Beta when Service does
154  enum State {
155    /**
156     * A service in this state is inactive. It does minimal work and consumes
157     * minimal resources.
158     */
159    NEW,
160
161    /**
162     * A service in this state is transitioning to {@link #RUNNING}.
163     */
164    STARTING,
165
166    /**
167     * A service in this state is operational.
168     */
169    RUNNING,
170
171    /**
172     * A service in this state is transitioning to {@link #TERMINATED}.
173     */
174    STOPPING,
175
176    /**
177     * A service in this state has completed execution normally. It does minimal work and consumes
178     * minimal resources.
179     */
180    TERMINATED,
181
182    /**
183     * A service in this state has encountered a problem and may not be operational. It cannot be
184     * started nor stopped.
185     */
186    FAILED
187  }
188  
189  /**
190   * A listener for the various state changes that a {@link Service} goes through in its lifecycle.
191   *
192   * @author Luke Sandberg
193   * @since 13.0
194   */
195  @Beta // should come out of Beta when Service does
196  interface Listener {
197    /**
198     * Called when the service transitions from {@linkplain State#NEW NEW} to 
199     * {@linkplain State#STARTING STARTING}. This occurs when {@link Service#start} or 
200     * {@link Service#startAndWait} is called the first time.
201     */
202    void starting();
203    
204    /**
205     * Called when the service transitions from {@linkplain State#STARTING STARTING} to 
206     * {@linkplain State#RUNNING RUNNING}. This occurs when a service has successfully started.
207     */
208    void running();
209    
210    /**
211     * Called when the service transitions to the {@linkplain State#STOPPING STOPPING} state. The 
212     * only valid values for {@code from} are {@linkplain State#STARTING STARTING} or 
213     * {@linkplain State#RUNNING RUNNING}.  This occurs when {@link Service#stop} is called.
214     * 
215     * @param from The previous state that is being transitioned from.  
216     */
217    void stopping(State from);
218    
219    /**
220     * Called when the service transitions to the {@linkplain State#TERMINATED TERMINATED} state. 
221     * The {@linkplain State#TERMINATED TERMINATED} state is a terminal state in the transition
222     * diagram.  Therefore, if this method is called, no other methods will be called on the 
223     * {@link Listener}.
224     * 
225     * @param from The previous state that is being transitioned from.  The only valid values for 
226     *     this are {@linkplain State#NEW NEW}, {@linkplain State#RUNNING RUNNING} or 
227     *     {@linkplain State#STOPPING STOPPING}.
228     */
229    void terminated(State from);
230    
231    /**
232     * Called when the service transitions to the {@linkplain State#FAILED FAILED} state. The 
233     * {@linkplain State#FAILED FAILED} state is a terminal state in the transition diagram.  
234     * Therefore, if this method is called, no other methods will be called on the {@link Listener}.
235     * 
236     * @param from The previous state that is being transitioned from.  Failure can occur in any 
237     *     state with the exception of {@linkplain State#NEW NEW} or 
238     *     {@linkplain State#TERMINATED TERMINATED}.
239     * @param failure The exception that caused the failure.
240     */
241    void failed(State from, Throwable failure);
242  }
243}