001 /* 002 * Copyright (C) 2009 Google Inc. 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.base; 018 019 import com.google.common.annotations.Beta; 020 021 import java.util.concurrent.ExecutionException; 022 import java.util.concurrent.Future; 023 024 /** 025 * An object with an operational state, plus asynchronous {@link #start()} and 026 * {@link #stop()} lifecycle methods to transfer into and out of this state. 027 * Example services include webservers, RPC servers and timers. The normal 028 * lifecycle of a service is: 029 * <ul> 030 * <li>{@link State#NEW} -></li> 031 * <li>{@link State#STARTING} -></li> 032 * <li>{@link State#RUNNING} -></li> 033 * <li>{@link State#STOPPING} -></li> 034 * <li>{@link State#TERMINATED}</li> 035 * </ul> 036 * 037 * If the service fails while starting, running or stopping, its state will be 038 * {@link State#FAILED}, and its behavior is undefined. Such a service cannot be 039 * started nor stopped. 040 * 041 * <p>Implementors of this interface are strongly encouraged to extend {@link 042 * com.google.common.util.concurrent.AbstractService} or {@link 043 * com.google.common.util.concurrent.AbstractExecutionThreadService}, which make 044 * the threading and state management easier. 045 * 046 * @author Jesse Wilson 047 * @since 1 048 */ 049 @Beta // TODO: make it an abstract class and move to common.util.concurrent 050 public interface Service { 051 /** 052 * If the service state is {@link State#NEW}, this initiates service startup 053 * and returns immediately. If the service has already been started, this 054 * method returns immediately without taking action. A stopped service may not 055 * be restarted. 056 * 057 * @return a future for the startup result, regardless of whether this call 058 * initiated startup. Calling {@link Future#get} will block until the 059 * service has finished starting, and returns one of {@link 060 * State#RUNNING}, {@link State#STOPPING} or {@link State#TERMINATED}. If 061 * the service fails to start, {@link Future#get} will throw an {@link 062 * ExecutionException}, and the service's state will be {@link 063 * State#FAILED}. If it has already finished starting, {@link Future#get} 064 * returns immediately. Cancelling the returned future is unsupported and 065 * always returns {@code false}. 066 */ 067 Future<State> start(); 068 069 /** 070 * Initiates service startup (if necessary), returning once the service has 071 * finished starting. Unlike calling {@code start().get()}, this method throws 072 * no checked exceptions. 073 * 074 * @throws InterruptedRuntimeException if the thread was interrupted while 075 * waiting for the service to finish starting up. 076 * @throws RuntimeException if startup failed 077 * @return the state of the service when startup finished. 078 */ 079 State startAndWait(); 080 081 /** 082 * Returns {@code true} if this service is {@link State#RUNNING running}. 083 */ 084 boolean isRunning(); 085 086 /** 087 * Returns the lifecycle state of the service. 088 */ 089 State state(); 090 091 /** 092 * If the service is {@link State#STARTING} or {@link State#RUNNING}, this 093 * initiates service shutdown and returns immediately. If this is {@link 094 * State#NEW}, it is {@link State#TERMINATED terminated} without having been 095 * started nor stopped. If the service has already been stopped, this 096 * method returns immediately without taking action. 097 * 098 * @return a future for the shutdown result, regardless of whether this call 099 * initiated shutdown. Calling {@link Future#get} will block until the 100 * service has finished shutting down, and either returns {@link 101 * State#TERMINATED} or throws an {@link ExecutionException}. If it has 102 * already finished stopping, {@link Future#get} returns immediately. 103 * Cancelling this future is unsupported and always returns {@code 104 * false}. 105 */ 106 Future<State> stop(); 107 108 /** 109 * Initiates service shutdown (if necessary), returning once the service has 110 * finished stopping. If this is {@link State#STARTING}, startup will be 111 * cancelled. If this is {@link State#NEW}, it is {@link State#TERMINATED 112 * terminated} without having been started nor stopped. Unlike calling {@code 113 * stop().get()}, this method throws no checked exceptions. 114 * 115 * @throws InterruptedRuntimeException if the thread was interrupted while 116 * waiting for the service to finish shutting down. 117 * @throws RuntimeException if shutdown failed 118 * @return the state of the service when shutdown finished. 119 */ 120 State stopAndWait(); 121 122 /** 123 * The lifecycle states of a service. 124 */ 125 public enum State { 126 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 }