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    
017    package com.google.common.util.concurrent;
018    
019    import com.google.common.annotations.Beta;
020    
021    import java.util.concurrent.ExecutionException;
022    import 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
054    public 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       * @since 13.0
122       */
123      Throwable failureCause();
124      
125      /**
126       * Registers a {@link Listener} to be {@linkplain Executor#execute executed} on the given 
127       * executor.  The listener will have the corresponding transition method called whenever the 
128       * service changes state. The listener will not have previous state changes replayed, so it is 
129       * suggested that listeners are added before the service starts.
130       *
131       * <p>There is no guaranteed ordering of execution of listeners, but any listener added through 
132       * this method is guaranteed to be called whenever there is a state change.
133       *
134       * <p>Exceptions thrown by a listener will be propagated up to the executor. Any exception thrown 
135       * during {@code Executor.execute} (e.g., a {@code RejectedExecutionException} or an exception 
136       * thrown by {@linkplain MoreExecutors#sameThreadExecutor inline execution}) will be caught and
137       * logged.
138       * 
139       * @param listener the listener to run when the service changes state is complete
140       * @param executor the executor in which the the listeners callback methods will be run. For fast,
141       *     lightweight listeners that would be safe to execute in any thread, consider 
142       *     {@link MoreExecutors#sameThreadExecutor}.
143       * @since 13.0
144       */
145      void addListener(Listener listener, Executor executor);
146      
147      /**
148       * The lifecycle states of a service.
149       *
150       * @since 9.0 (in 1.0 as {@code com.google.common.base.Service.State})
151       */
152      @Beta // should come out of Beta when Service does
153      enum State {
154        /**
155         * A service in this state is inactive. It does minimal work and consumes
156         * minimal resources.
157         */
158        NEW,
159    
160        /**
161         * A service in this state is transitioning to {@link #RUNNING}.
162         */
163        STARTING,
164    
165        /**
166         * A service in this state is operational.
167         */
168        RUNNING,
169    
170        /**
171         * A service in this state is transitioning to {@link #TERMINATED}.
172         */
173        STOPPING,
174    
175        /**
176         * A service in this state has completed execution normally. It does minimal work and consumes
177         * minimal resources.
178         */
179        TERMINATED,
180    
181        /**
182         * A service in this state has encountered a problem and may not be operational. It cannot be
183         * started nor stopped.
184         */
185        FAILED
186      }
187      
188      /**
189       * A listener for the various state changes that a {@link Service} goes through in its lifecycle.
190       *
191       * @author Luke Sandberg
192       * @since 13.0
193       */
194      @Beta // should come out of Beta when Service does
195      interface Listener {
196        /**
197         * Called when the service transitions from {@linkplain State#NEW NEW} to 
198         * {@linkplain State#STARTING STARTING}. This occurs when {@link Service#start} or 
199         * {@link Service#startAndWait} is called the first time.
200         */
201        void starting();
202        
203        /**
204         * Called when the service transitions from {@linkplain State#STARTING STARTING} to 
205         * {@linkplain State#RUNNING RUNNING}. This occurs when a service has successfully started.
206         */
207        void running();
208        
209        /**
210         * Called when the service transitions to the {@linkplain State#STOPPING STOPPING} state. The 
211         * only valid values for {@code from} are {@linkplain State#STARTING STARTING} or 
212         * {@linkplain State#RUNNING RUNNING}.  This occurs when {@link Service#stop} is called.
213         * 
214         * @param from The previous state that is being transitioned from.  
215         */
216        void stopping(State from);
217        
218        /**
219         * Called when the service transitions to the {@linkplain State#TERMINATED TERMINATED} state. 
220         * The {@linkplain State#TERMINATED TERMINATED} state is a terminal state in the transition
221         * diagram.  Therefore, if this method is called, no other methods will be called on the 
222         * {@link Listener}.
223         * 
224         * @param from The previous state that is being transitioned from.  The only valid values for 
225         *     this are {@linkplain State#NEW NEW} or {@linkplain State#STOPPING STOPPING}.
226         */
227        void terminated(State from);
228        
229        /**
230         * Called when the service transitions to the {@linkplain State#FAILED FAILED} state. The 
231         * {@linkplain State#FAILED FAILED} state is a terminal state in the transition diagram.  
232         * Therefore, if this method is called, no other methods will be called on the {@link Listener}.
233         * 
234         * @param from The previous state that is being transitioned from.  Failure can occur in any 
235         *     state with the exception of {@linkplain State#NEW NEW}.
236         * @param failure The exception that caused the failure.
237         */
238        void failed(State from, Throwable failure);
239      }
240    }