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;
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 (in version 1 as {@code com.google.common.base.Service})
046 */
047@Beta // TODO(kevinb): make abstract class?
048public interface Service {
049  /**
050   * If the service state is {@link State#NEW}, this initiates service startup
051   * and returns immediately. If the service has already been started, this
052   * method returns immediately without taking action. A stopped service may not
053   * be restarted.
054   *
055   * @return a future for the startup result, regardless of whether this call
056   *     initiated startup. Calling {@link ListenableFuture#get} will block
057   *     until the service has finished starting, and returns one of {@link
058   *     State#RUNNING}, {@link State#STOPPING} or {@link State#TERMINATED}. If
059   *     the service fails to start, {@link ListenableFuture#get} will throw an
060   *     {@link ExecutionException}, and the service's state will be {@link
061   *     State#FAILED}. If it has already finished starting, {@link
062   *     ListenableFuture#get} returns immediately. Cancelling the returned
063   *     future is unsupported and always returns {@code false}.
064   */
065  ListenableFuture<State> start();
066
067  /**
068   * Initiates service startup (if necessary), returning once the service has
069   * finished starting. Unlike calling {@code start().get()}, this method throws
070   * no checked exceptions, and it cannot be {@linkplain Thread#interrupt
071   * interrupted}.
072   *
073   * @throws RuntimeException if startup failed
074   * @return the state of the service when startup finished.
075   */
076  State startAndWait();
077
078  /**
079   * Returns {@code true} if this service is {@linkplain State#RUNNING running}.
080   */
081  boolean isRunning();
082
083  /**
084   * Returns the lifecycle state of the service.
085   */
086  State state();
087
088  /**
089   * If the service is {@linkplain State#STARTING starting} or {@linkplain
090   * State#RUNNING running}, this initiates service shutdown and returns
091   * immediately. If the service is {@linkplain State#NEW new}, it is
092   * {@linkplain State#TERMINATED terminated} without having been started nor
093   * stopped.  If the service has already been stopped, this method returns
094   * immediately without taking action.
095   *
096   * @return a future for the shutdown result, regardless of whether this call
097   *     initiated shutdown. Calling {@link ListenableFuture#get} will block
098   *     until the service has finished shutting down, and either returns
099   *     {@link State#TERMINATED} or throws an {@link ExecutionException}. If
100   *     it has already finished stopping, {@link ListenableFuture#get} returns
101   *     immediately.  Cancelling this future is unsupported and always returns
102   *     {@code false}.
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 RuntimeException 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 (in version 1 as {@code com.google.common.base.Service.State})
122   */
123  @Beta // should come out of Beta when Service does
124  enum State {
125    /**
126     * A service in this state is inactive. It does minimal work and consumes
127     * minimal resources.
128     */
129    NEW,
130
131    /**
132     * A service in this state is transitioning to {@link #RUNNING}.
133     */
134    STARTING,
135
136    /**
137     * A service in this state is operational.
138     */
139    RUNNING,
140
141    /**
142     * A service in this state is transitioning to {@link #TERMINATED}.
143     */
144    STOPPING,
145
146    /**
147     * A service in this state has completed execution normally. It does minimal
148     * work and consumes minimal resources.
149     */
150    TERMINATED,
151
152    /**
153     * A service in this state has encountered a problem and may not be
154     * operational. It cannot be started nor stopped.
155     */
156    FAILED
157  }
158}