Package ca.uhn.hl7v2.concurrent
Class Service
java.lang.Object
ca.uhn.hl7v2.concurrent.Service
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
HL7Service,Receiver
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidPrepare any resources before entering the main thread.protected voidClean up any resources initialized inafterStartup().If this service exited with an exception, ths method returns that exception.protected abstract voidhandle()The main task of the thread, called in a loop as long asisRunning()returns true.booleanprotected voidPrepare thread to leave its main loop.final voidrun()Runs the thread.protected voidsetServiceExitedWithException(Throwable theThreadExitedWithException) Provide the exception which caused this service to failvoidsetShutdownTimeout(long shutdownTimeout) Sets the time in milliseconds how longstopAndWait()should wait for the thread to terminate.voidstart()Starts the server listening for connections in a new thread.voidStarts the server listening for connections in a new thread.voidstop()Advises the thread to leave its main loop.final voidStops the thread by leaving its main loop.void
-
Constructor Details
-
Service
-
-
Method Details
-
isRunning
- Returns:
- Returns
trueif the server has been started, and has not yet been stopped.
-
getExecutorService
-
setShutdownTimeout
Sets the time in milliseconds how longstopAndWait()should wait for the thread to terminate. Defaults to 3000ms.- Parameters:
shutdownTimeout- timout in milliseconds
-
start
Starts the server listening for connections in a new thread. This continues untilstop()is called.- Throws:
IllegalStateException- If the service is already running (i.e. start() has already been called.
-
startAndWait
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
Prepare any resources before entering the main thread.- Throws:
RuntimeException- if resources could not acquired. In this case, the thread will shutdown. Note thatafterTermination()is called before.
-
handle
The main task of the thread, called in a loop as long asisRunning()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 tostop()is able to gracefully terminate the thread. -
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
-
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
Clean up any resources initialized inafterStartup(). -
prepareTermination
Prepare thread to leave its main loop. By default setskeepRunningto false, but some implementations may need to do additional stuff. -
run
Runs the thread. -
setServiceExitedWithException
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
-