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 {@link
041     * AbstractService}, {@link AbstractExecutionThreadService}, or {@link
042     * AbstractIdleService}, which 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 the returned
064       *     future is unsupported and always returns {@code false}.
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 is unsupported and always returns
103       *     {@code false}.
104       */
105      ListenableFuture<State> stop();
106    
107      /**
108       * Initiates service shutdown (if necessary), returning once the service has
109       * finished stopping. If this is {@link State#STARTING}, startup will be
110       * cancelled. If this is {@link State#NEW}, it is {@link State#TERMINATED
111       * terminated} without having been started nor stopped. Unlike calling {@code
112       * stop().get()}, this method throws no checked exceptions.
113       *
114       * @throws UncheckedExecutionException if shutdown failed
115       * @return the state of the service when shutdown finished.
116       */
117      State stopAndWait();
118    
119      /**
120       * The lifecycle states of a service.
121       *
122       * @since 9.0 (in 1.0 as
123       *     {@code com.google.common.base.Service.State})
124       */
125      @Beta // should come out of Beta when Service does
126      enum State {
127        /**
128         * A service in this state is inactive. It does minimal work and consumes
129         * minimal resources.
130         */
131        NEW,
132    
133        /**
134         * A service in this state is transitioning to {@link #RUNNING}.
135         */
136        STARTING,
137    
138        /**
139         * A service in this state is operational.
140         */
141        RUNNING,
142    
143        /**
144         * A service in this state is transitioning to {@link #TERMINATED}.
145         */
146        STOPPING,
147    
148        /**
149         * A service in this state has completed execution normally. It does minimal
150         * work and consumes minimal resources.
151         */
152        TERMINATED,
153    
154        /**
155         * A service in this state has encountered a problem and may not be
156         * operational. It cannot be started nor stopped.
157         */
158        FAILED
159      }
160    }