Package net.sf.basedb.core.signal
Class ThreadSignalHandler
java.lang.Object
net.sf.basedb.core.signal.AbstractSignalHandler
net.sf.basedb.core.signal.ThreadSignalHandler
- All Implemented Interfaces:
SignalHandler
,Action
An implementation of a signal handler that uses the
Thread
class
to communicate signals back to the SignalTarget
. This handler only
supports the Signal.ABORT
signal. When this signal is received,
Thread.interrupt()
is called on the thread that was registered with
this handler. This should typically be a worker thread for some process.
The worker thread should regularly check Thread.interrupted()
to see if it has been interrupted. If the thread may be waiting for a blocking
call to end, it should be able to handle the resulting InterruptedException
that is thrown if the thread is interrupted.
If the worker thread becomes interrupted it should stop what it is doing, cleanup any allocated resources and exit in a timely fasion. Here is a general outline:
// ... code in worker thread threadSignalHandler.setWorkerThread(null); beginTransaction(); boolean done = false; boolean interrupted = false; while (!done && !interrupted) { try { done = doSomeWork(); // NOTE! This must not take forever! interrupted = Thread.interrupted(); } catch (InterruptedException ex) { // NOTE! Try-catch is only needed if thread calls a blocking method interrupted = true; } } if (interrupted) { rollbackTransaction(); } else { commitTransaction(); }
- Version:
- 2.6
- Author:
- nicklas
- Last modified
- $Date: 2019-06-17 09:36:09 +0200 (mån, 17 juni 2019) $
-
Field Summary
Modifier and TypeFieldDescriptionprivate boolean
If TRUE, call Thread.stop() instead of Thread.interrupt()private boolean
If TRUE, at least one ABORT signal has been recieved.private static ThreadLocal<InterruptHandler>
Keep per-thread interupt handlers.private static final Logger
Log signals processing.private Thread
The worker thread that should be interrupted whenSignal.ABORT
is received. -
Constructor Summary
ConstructorDescriptionCreate a new thread signal handler.ThreadSignalHandler
(Thread workerThread) Create a new thread signal handler. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Utility method to check if the current thread has been interrupted and (may) throw a SignalException if it has.void
handleSignal
(Signal signal) Only handles theSignal.ABORT
signal.boolean
hasReceived
(Signal signal) Check if the given signal has been received by this signal handler.void
setForceStop
(boolean forceStop) Deprecated.In 3.14.static void
setInterruptHandler
(InterruptHandler handler) Register an interrupt handler with the current thread.void
setWorkerThread
(Thread workerThread) Set the worker thread that should be interrupted when a signal is receiver.Methods inherited from class net.sf.basedb.core.signal.AbstractSignalHandler
addSignal, getSupportedSignals, removeSignal, supports
-
Field Details
-
interruptHandler
Keep per-thread interupt handlers. The default handler isExceptionInterruptHandler
which simply throws aSignalException
if the current thread has been interrupted.- Since:
- 2.16
-
logger
Log signals processing. -
supported
-
workerThread
The worker thread that should be interrupted whenSignal.ABORT
is received. -
forceStop
private boolean forceStopIf TRUE, call Thread.stop() instead of Thread.interrupt() -
hasAborted
private boolean hasAbortedIf TRUE, at least one ABORT signal has been recieved.- Since:
- 3.10.1
-
-
Constructor Details
-
ThreadSignalHandler
public ThreadSignalHandler()Create a new thread signal handler. It will interrupt the current thread when it recieves theSignal.ABORT
signal. -
ThreadSignalHandler
Create a new thread signal handler.- Parameters:
workerThread
- The worker thread to interrupt when it receieves theSignal.ABORT
signal, or null to interrupt the current thread
-
-
Method Details
-
checkInterrupted
public static void checkInterrupted()Utility method to check if the current thread has been interrupted and (may) throw a SignalException if it has. NOTE! Since BASE 2.16 the actual behaviour of this method is determined by theInterruptHandler
that has been registered for the current thread bysetInterruptHandler(InterruptHandler)
. The default handler always throws aSignalException
. -
setInterruptHandler
Register an interrupt handler with the current thread. The handler is used by thecheckInterrupted()
method when the thread has been interrupted. If no handler has been registered with a thread the default action is to throw aSignalException
.- Parameters:
handler
- An interrupt handler or null to remove a previously registered handler- Since:
- 2.16
-
handleSignal
Only handles theSignal.ABORT
signal. When receieved,Thread.interrupt()
is called on the worker thread that was registered when this object was created. Note that this method called from a different thread. The worker thread is busy doing something else. If the worker thread has already ended, this method does nothing.- Parameters:
signal
- The signal, onlySignal.ABORT
is accepted- Throws:
UnsupportedSignalException
- If signal is anything else thanSignal.ABORT
-
setWorkerThread
Set the worker thread that should be interrupted when a signal is receiver.- Parameters:
workerThread
- The worker thread, or null to use the current thread
-
setForceStop
Deprecated.In 3.14. Calling this is ignored and the signal handler will always use the interrupt method.Thread.stop() has been removed from the Java API. -
hasReceived
Check if the given signal has been received by this signal handler.- Parameters:
signal
- The signal to check for- Returns:
- TRUE if the signal has been received, FALSE otherwise
- Since:
- 3.10.1
-