BASE includes a simple signalling system that can be used to send signals to plug-ins. The system was primarly developed to allow a user to kill a plug-in when it is executing. Therfore, the focus of this chapter will be how to implement a plug-in to make it possible to kill it during it's execution.
Since we don't want to do this by brute force such as destroying the
process or stopping thread the plug-in executes in, cooperation is needed
by the plug-in. First, the plug-in must implement the
SignalTarget
SignalHandler
ThreadSignalHandler
ABORT
signal.
This is a simple implementation that just calls Thread.interrupt()
on the plug-in worker thread. This may cause two different effects:
The Thread.interrupted()
flag is set. The plug-in must check this
at regular intervals and if the flag is set it must cleanup, rollback
open transactions and exit as soon as possible.
If the plug-in is waiting in a blocking call that is interruptable, for
example Thread.sleep()
, an InterruptedException
is thrown. This should cause the same actions as if the flag was set to happen.
Not all blocking calls are interruptable | |
---|---|
For example calling |
Here is a general outline for a plug-in that uses the
ThreadSignalHandler
.
private ThreadSignalHandler signalHandler; public SignalHandler getSignalHandler() { signalHandler = new ThreadSignalHandler(); return signalHandler; } public void run(Request request, Response response, ProgressReporter progress) { if (signalHandler != null) signalHandler.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 that is interruptable interrupted = true; } } if (interrupted) { rollbackTransaction(); response.setError("Aborted by user", null); } else { commitTransaction(); response.setDone("Done"); } }
Other signal handler implementations are
ProgressReporterSignalHandler
EnhancedThreadSignalHandler
SHUTDOWN
signal
which is sent to plug-in when the system is shutting down. Clever plug-ins may use
this to enable them to be restarted when the system is up and running again.
See that javadoc for information about how to use it. For more information about the
signalling system as a whole, see Section 29.3.2, “Sending signals (to plug-ins)”.