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    
023    /**
024     * An object with an operational state, plus asynchronous {@link #start()} and
025     * {@link #stop()} lifecycle methods to transfer into and out of this state.
026     * Example services include webservers, RPC servers and timers. The normal
027     * lifecycle of a service is:
028     * <ul>
029     *   <li>{@link State#NEW} -&gt;</li>
030     *   <li>{@link State#STARTING} -&gt;</li>
031     *   <li>{@link State#RUNNING} -&gt;</li>
032     *   <li>{@link State#STOPPING} -&gt;</li>
033     *   <li>{@link State#TERMINATED}</li>
034     * </ul>
035     *
036     * If the service fails while starting, running or stopping, its state will be
037     * {@link State#FAILED}, and its behavior is undefined. Such a service cannot be
038     * started nor stopped.
039     *
040     * <p>Implementors of this interface are strongly encouraged to extend one of 
041     * the abstract classes in this package which implement this interface and 
042     * make the threading and state management easier.
043     *
044     * @author Jesse Wilson
045     * @since 9.0 (in 1.0 as
046     *     {@code com.google.common.base.Service})
047     */
048    @Beta // TODO(kevinb): make abstract class?
049    public interface Service {
050      /**
051       * If the service state is {@link State#NEW}, this initiates service startup
052       * and returns immediately. If the service has already been started, this
053       * method returns immediately without taking action. A stopped service may not
054       * be restarted.
055       *
056       * @return a future for the startup result, regardless of whether this call
057       *     initiated startup. Calling {@link ListenableFuture#get} will block
058       *     until the service has finished starting, and returns one of {@link
059       *     State#RUNNING}, {@link State#STOPPING} or {@link State#TERMINATED}. If
060       *     the service fails to start, {@link ListenableFuture#get} will throw an
061       *     {@link ExecutionException}, and the service's state will be {@link
062       *     State#FAILED}. If it has already finished starting, {@link
063       *     ListenableFuture#get} returns immediately. Cancelling this future has
064       *     no effect on the service.
065       */
066      ListenableFuture<State> start();
067    
068      /**
069       * Initiates service startup (if necessary), returning once the service has
070       * finished starting. Unlike calling {@code start().get()}, this method throws
071       * no checked exceptions, and it cannot be {@linkplain Thread#interrupt
072       * interrupted}.
073       *
074       * @throws UncheckedExecutionException if startup failed
075       * @return the state of the service when startup finished.
076       */
077      State startAndWait();
078    
079      /**
080       * Returns {@code true} if this service is {@linkplain State#RUNNING running}.
081       */
082      boolean isRunning();
083    
084      /**
085       * Returns the lifecycle state of the service.
086       */
087      State state();
088    
089      /**
090       * If the service is {@linkplain State#STARTING starting} or {@linkplain
091       * State#RUNNING running}, this initiates service shutdown and returns
092       * immediately. If the service is {@linkplain State#NEW new}, it is
093       * {@linkplain State#TERMINATED terminated} without having been started nor
094       * stopped.  If the service has already been stopped, this method returns
095       * immediately without taking action.
096       *
097       * @return a future for the shutdown result, regardless of whether this call
098       *     initiated shutdown. Calling {@link ListenableFuture#get} will block
099       *     until the service has finished shutting down, and either returns
100       *     {@link State#TERMINATED} or throws an {@link ExecutionException}. If
101       *     it has already finished stopping, {@link ListenableFuture#get} returns
102       *     immediately.  Cancelling this future has no effect on the service.
103       */
104      ListenableFuture<State> stop();
105    
106      /**
107       * Initiates service shutdown (if necessary), returning once the service has
108       * finished stopping. If this is {@link State#STARTING}, startup will be
109       * cancelled. If this is {@link State#NEW}, it is {@link State#TERMINATED
110       * terminated} without having been started nor stopped. Unlike calling {@code
111       * stop().get()}, this method throws no checked exceptions.
112       *
113       * @throws UncheckedExecutionException if shutdown failed
114       * @return the state of the service when shutdown finished.
115       */
116      State stopAndWait();
117    
118      /**
119       * The lifecycle states of a service.
120       *
121       * @since 9.0 (in 1.0 as
122       *     {@code com.google.common.base.Service.State})
123       */
124      @Beta // should come out of Beta when Service does
125      enum State {
126        /**
127         * A service in this state is inactive. It does minimal work and consumes
128         * minimal resources.
129         */
130        NEW,
131    
132        /**
133         * A service in this state is transitioning to {@link #RUNNING}.
134         */
135        STARTING,
136    
137        /**
138         * A service in this state is operational.
139         */
140        RUNNING,
141    
142        /**
143         * A service in this state is transitioning to {@link #TERMINATED}.
144         */
145        STOPPING,
146    
147        /**
148         * A service in this state has completed execution normally. It does minimal
149         * work and consumes minimal resources.
150         */
151        TERMINATED,
152    
153        /**
154         * A service in this state has encountered a problem and may not be
155         * operational. It cannot be started nor stopped.
156         */
157        FAILED
158      }
159    }