Home | History | Annotate | Download | only in service
      1 /**
      2  * Copyright (C) 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.inject.service;
     18 
     19 import java.util.concurrent.ExecutionException;
     20 import java.util.concurrent.Future;
     21 
     22 /**
     23  * An object with an operational state, asynchronous {@link #start()} and
     24  * {@link #stop()} lifecycle methods to transition in and out of this state.
     25  * Example services include http servers, RPC systems and timer tasks.
     26  *
     27  * @author dhanji (at) gmail.com (Dhanji R. Prasanna)
     28  */
     29 public interface Service {
     30   /**
     31    * If the service has already been started, this method returns
     32    * immediately without taking action. A stopped service may not be restarted.
     33    *
     34    * @return a future for the startup result, regardless of whether this call
     35    *     initiated startup. Calling {@link Future#get} will block until the
     36    *     service has finished starting, and returns the resultant state. If
     37    *     the service fails to start, {@link Future#get} will throw an {@link
     38    *     ExecutionException}. If it has already finished starting,
     39    *     {@link Future#get} returns immediately.
     40    */
     41   Future<State> start();
     42 
     43   /**
     44    * If the service is {@link State#STARTED} initiates service shutdown and
     45    * returns immediately. If the service has already been stopped, this
     46    * method returns immediately without taking action.
     47    *
     48    * @return a future for the shutdown result, regardless of whether this call
     49    *     initiated shutdown. Calling {@link Future#get} will block until the
     50    *     service has finished shutting down, and either returns {@link
     51    *     State#STOPPED} or throws an {@link ExecutionException}. If it has
     52    *     already finished stopping, {@link Future#get} returns immediately.
     53    */
     54   Future<State> stop();
     55 
     56   /**
     57    * Returns the current state of this service. One of {@link State} possible
     58    * values, or null if this is a brand new object, i.e., has not been put into
     59    * any state yet.
     60    */
     61   State state();
     62 
     63   /**
     64    * The lifecycle states of a service.
     65    */
     66   enum State { STARTED, STOPPED, FAILED }
     67 }
     68