ca.uhn.hl7v2.concurrent
Class Service

java.lang.Object
  extended by ca.uhn.hl7v2.concurrent.Service
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
HL7Service, Receiver

public abstract class Service
extends Object
implements Runnable

Base class for a unified management of threads with a defined lifecycle. It uses a keepRunning flag to regularly terminate a thread. Classes implementing this class must implement handle() to do the main processing. afterStartup() and afterTermination() can be overridden to acquire and release resources required for processing.


Constructor Summary
Service(String name, ExecutorService executorService)
           
 
Method Summary
protected  void afterStartup()
          Prepare any resources before entering the main thread.
protected  void afterTermination()
          Clean up any resources initialized in afterStartup().
 ExecutorService getExecutorService()
           
 Throwable getServiceExitedWithException()
          If this service exited with an exception, ths method returns that exception.
protected abstract  void handle()
          The main task of the thread, called in a loop as long as isRunning() returns true.
 boolean isRunning()
           
protected  void prepareTermination()
          Prepare thread to leave its main loop.
 void run()
          Runs the thread.
protected  void setServiceExitedWithException(Throwable theThreadExitedWithException)
          Provide the exception which caused this service to fail
 void setShutdownTimeout(long shutdownTimeout)
          Sets the time in milliseconds how long stopAndWait() should wait for the thread to terminate.
 void start()
          Starts the server listening for connections in a new thread.
 void stop()
          Advises the thread to leave its main loop.
 void stopAndWait()
          Stops the thread by leaving its main loop.
 void waitForTermination()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Service

public Service(String name,
               ExecutorService executorService)
Method Detail

isRunning

public boolean isRunning()
Returns:
Returns true if the server has been started, and has not yet been stopped.

getExecutorService

public ExecutorService getExecutorService()

setShutdownTimeout

public void setShutdownTimeout(long shutdownTimeout)
Sets the time in milliseconds how long stopAndWait() should wait for the thread to terminate. Defaults to 3000ms.

Parameters:
shutdownTimeout -

start

public void start()
Starts the server listening for connections in a new thread. This continues until stop() is called.


afterStartup

protected void afterStartup()
Prepare any resources before entering the main thread.

Throws:
RuntimeException - if resources could not acquired. In this case, the thread will shutdown. Note that afterTermination() is called before.

handle

protected abstract void handle()
The main task of the thread, called in a loop as long as isRunning() returns true. Overridden methods are responsible for yielding or pausing the thread when it's idle. The method must also not block indefinitely so that a call to stop() is able to gracefully terminate the thread.

Throws:
Throwable - any exception thrown will terminate the thread.

stop

public void stop()
Advises the thread to leave its main loop. #beforeTermination() is called before this method returns. afterTermination() is called after the thread has left its main loop.


waitForTermination

public void waitForTermination()

stopAndWait

public final void stopAndWait()
Stops the thread by leaving its main loop. afterTermination() is called before the thread is terminated. The method waits until the thread has stopped.

Throws:
Exception - if the thread has not finished within shutdownTimeout

afterTermination

protected void afterTermination()
Clean up any resources initialized in afterStartup().


prepareTermination

protected void prepareTermination()
Prepare thread to leave its main loop. By default sets keepRunning to false, but some implementations may need to do additional stuff.


run

public final void run()
Runs the thread.

Specified by:
run in interface Runnable
See Also:
Runnable.run()

setServiceExitedWithException

protected void setServiceExitedWithException(Throwable theThreadExitedWithException)
Provide the exception which caused this service to fail


getServiceExitedWithException

public Throwable getServiceExitedWithException()
If this service exited with an exception, ths method returns that exception. This is useful for detecting if the service failed unexpectedly



Copyright © 2001-2012 University Health Network. All Rights Reserved.