Class Service

java.lang.Object
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 Details

  • Method Details

    • isRunning

      public boolean isRunning()
      Returns:
      Returns true if the server has been started, and has not yet been stopped.
    • 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 - timout in milliseconds
    • start

      public void start()
      Starts the server listening for connections in a new thread. This continues until stop() is called.
      Throws:
      IllegalStateException - If the service is already running (i.e. start() has already been called.
    • startAndWait

      public void startAndWait() throws InterruptedException

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

      Unlike start(), this method will not return until the processing loop has completed at least once. This does not imply any kind of successful processing, but should at least provide a guarantee that the service has finished initializing itself.

      Throws:
      InterruptedException
    • 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.
    • stop

      public void stop()
      Advises the thread to leave its main loop. prepareTermination() 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.
    • 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:
    • setServiceExitedWithException

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

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