public abstract class Service extends Object implements Runnable
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 and Description |
---|
Service(String name,
ExecutorService executorService) |
Modifier and Type | Method and Description |
---|---|
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 |
startAndWait()
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() |
public Service(String name, ExecutorService executorService)
public boolean isRunning()
true
if the server has been started, and has
not yet been stopped.public ExecutorService getExecutorService()
public void setShutdownTimeout(long shutdownTimeout)
stopAndWait()
should wait
for the thread to terminate. Defaults to 3000ms.shutdownTimeout
- timout in millisecondspublic void start()
stop()
is called.IllegalStateException
- If the service is already running (i.e.
start() has already been called.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.
InterruptedException
protected void afterStartup()
RuntimeException
- if resources could not acquired. In this case, the thread
will shutdown. Note that afterTermination()
is
called before.protected abstract void handle()
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.public void stop()
prepareTermination()
is
called before this method returns. afterTermination()
is
called after the thread has left its main loop.public void waitForTermination()
public final void stopAndWait()
afterTermination()
is
called before the thread is terminated. The method waits until the thread
has stopped.protected void afterTermination()
afterStartup()
.protected void prepareTermination()
keepRunning
to false, but some implementations may need to do additional stuff.public final void run()
run
in interface Runnable
Runnable.run()
protected void setServiceExitedWithException(Throwable theThreadExitedWithException)
public Throwable getServiceExitedWithException()
Copyright © 2001–2017 University Health Network. All rights reserved.