26.7. Enable support for aborting a running a plug-in

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 interface. From this, a SignalHandler can be created. A plug-in may choose to implement it's own signal handler or use an existing implementation. BASE, for example, provides the ThreadSignalHandler implementation that supports the ABORT signal. This is a simple implementation that just calls Thread.interrupt() on the plug-in worker thread. This may cause two different effects:

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 and EnhancedThreadSignalHandler. The latter handler also has support for the 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)”.