com.google.common.util.concurrent
Class AbstractService

java.lang.Object
  extended by com.google.common.util.concurrent.AbstractService
All Implemented Interfaces:
Service

@Beta
public abstract class AbstractService
extends java.lang.Object
implements Service

Base class for implementing services that can handle doStart() and doStop() requests, responding to them with notifyStarted() and notifyStopped() callbacks. Its subclasses must manage threads manually; consider AbstractExecutionThreadService if you need only a single execution thread.

Since:
1
Author:
Jesse Wilson

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.common.base.Service
Service.State
 
Constructor Summary
AbstractService()
           
 
Method Summary
protected abstract  void doStart()
          This method is called by start() to initiate service startup.
protected abstract  void doStop()
          This method should be used to initiate service shutdown.
 boolean isRunning()
           
protected  void notifyFailed(java.lang.Throwable cause)
          Invoke this method to transition the service to the Service.State.FAILED.
protected  void notifyStarted()
          Implementing classes should invoke this method once their service has started.
protected  void notifyStopped()
          Implementing classes should invoke this method once their service has stopped.
 java.util.concurrent.Future<Service.State> start()
           
 Service.State startAndWait()
           
 Service.State state()
           
 java.util.concurrent.Future<Service.State> stop()
           
 Service.State stopAndWait()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractService

public AbstractService()
Method Detail

doStart

protected abstract void doStart()
This method is called by start() to initiate service startup. The invocation of this method should cause a call to notifyStarted(), either during this method's run, or after it has returned. If startup fails, the invocation should cause a call to notifyFailed(Throwable) instead.

This method should return promptly; prefer to do work on a different thread where it is convenient. It is invoked exactly once on service startup, even when start() is called multiple times.


doStop

protected abstract void doStop()
This method should be used to initiate service shutdown. The invocation of this method should cause a call to notifyStopped(), either during this method's run, or after it has returned. If shutdown fails, the invocation should cause a call to notifyFailed(Throwable) instead.

This method should return promptly; prefer to do work on a different thread where it is convenient. It is invoked exactly once on service shutdown, even when stop() is called multiple times.


start

public final java.util.concurrent.Future<Service.State> start()
Specified by:
start in interface Service

stop

public final java.util.concurrent.Future<Service.State> stop()
Specified by:
stop in interface Service

startAndWait

public Service.State startAndWait()
Specified by:
startAndWait in interface Service

stopAndWait

public Service.State stopAndWait()
Specified by:
stopAndWait in interface Service

notifyStarted

protected final void notifyStarted()
Implementing classes should invoke this method once their service has started. It will cause the service to transition from Service.State.STARTING to Service.State.RUNNING.

Throws:
java.lang.IllegalStateException - if the service is not Service.State.STARTING.

notifyStopped

protected final void notifyStopped()
Implementing classes should invoke this method once their service has stopped. It will cause the service to transition from Service.State.STOPPING to Service.State.TERMINATED.

Throws:
java.lang.IllegalStateException - if the service is neither Service.State.STOPPING nor Service.State.RUNNING.

notifyFailed

protected final void notifyFailed(java.lang.Throwable cause)
Invoke this method to transition the service to the Service.State.FAILED. The service will not be stopped if it is running. Invoke this method when a service has failed critically or otherwise cannot be started nor stopped.


isRunning

public final boolean isRunning()
Specified by:
isRunning in interface Service

state

public final Service.State state()
Specified by:
state in interface Service

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2010 Google. All Rights Reserved.