com.google.common.util.concurrent
Class AbstractExecutionThreadService

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

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

Base class for services that can implement startUp(), run() and shutDown() methods. This class uses a single thread to execute the service; consider AbstractService if you would like to manage any threading manually.

Since:
1
Author:
Jesse Wilson

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.common.base.Service
Service.State
 
Constructor Summary
AbstractExecutionThreadService()
           
 
Method Summary
protected  java.util.concurrent.Executor executor()
          Returns the Executor that will be used to run this service.
 boolean isRunning()
           
protected abstract  void run()
          Run the service.
protected  void shutDown()
          Stop the service.
 java.util.concurrent.Future<Service.State> start()
           
 Service.State startAndWait()
           
protected  void startUp()
          Start the service.
 Service.State state()
           
 java.util.concurrent.Future<Service.State> stop()
           
 Service.State stopAndWait()
           
 java.lang.String toString()
           
protected  void triggerShutdown()
          Invoked to request the service to stop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractExecutionThreadService

public AbstractExecutionThreadService()
Method Detail

startUp

protected void startUp()
                throws java.lang.Exception
Start the service. This method is invoked on the execution thread.

Throws:
java.lang.Exception

run

protected abstract void run()
                     throws java.lang.Exception
Run the service. This method is invoked on the execution thread. Implementations must respond to stop requests. You could poll for lifecycle changes in a work loop:
   public void run() {
     while (isRunning()) {
       // perform a unit of work
     }
   }
 
...or you could respond to stop requests by implementing triggerShutdown(), which should cause run() to return.

Throws:
java.lang.Exception

shutDown

protected void shutDown()
                 throws java.lang.Exception
Stop the service. This method is invoked on the execution thread.

Throws:
java.lang.Exception

triggerShutdown

protected void triggerShutdown()
Invoked to request the service to stop.


executor

protected java.util.concurrent.Executor executor()
Returns the Executor that will be used to run this service. Subclasses may override this method to use a custom Executor, which may configure its worker thread with a specific name, thread group or priority. The returned executor's execute() method is called when this service is started, and should return promptly.


toString

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

start

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

startAndWait

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

isRunning

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

state

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

stop

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

stopAndWait

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


Copyright © 2010 Google. All Rights Reserved.