Package net.sf.basedb.core.signal
Class ProgressReporterSignalHandler
java.lang.Object
net.sf.basedb.core.signal.AbstractSignalHandler
net.sf.basedb.core.signal.ProgressReporterSignalHandler
- All Implemented Interfaces:
ProgressReporter
,SignalHandler
,Action
public class ProgressReporterSignalHandler
extends AbstractSignalHandler
implements ProgressReporter
An implementation of a signal handler that uses a
ProgressReporter
to communicate signals back to the target. This handler supports any signals.
This signal handler works by replacing the progress reporter a plug-in
uses with it's own progress reporter. Each time the plug-in calls
ProgressReporter.display(int, String)
or ProgressReporter.append(String)
and the percentage value is less than 100 the signal handler will check if a signal
has been received. If so, a SignalReceivedException
will be thrown.
NOTE! Some parts of the BASE core only uses the Thread.interrupt()
mechanism for aborting a process. For example, long-running queries against the
dynamic database. In this case we recommend that a ThreadSignalHandler
,
possible linked with DelegatingSignalHandler
, is used instead.
Here is a general outline of a plug-in that uses the progress reporter signal handler:
// To keep track of current progress private int percent; private String progressMessage; // Set up signal handling. ABORT is the only supported signal private ProgressReporterSignalHandler signalHandler; public SignalHandler getSignalHandler() { signalHandler = new ProgressReporterSignalHandler(Signal.ABORT); return signalHandler; } public void run(Request request, Response response, ProgressReporter progress) { if (signalHandler != null) { signalHandler.forwardTo(progress); progress = signalHandler; } beginTransaction(); boolean done = false; while (!done && !interrupted) { try { done = doSomeWork(); // NOTE! This must not take forever! if (progress != null) progress.display(percent, progressMessage); } catch (SignalReceivedException ex) { // We only support the abort signal interrupted = true; } } if (interrupted) { rollbackTransaction(); response.setError("Aborted by user", null); } else { commitTransaction(); response.setDone("Done"); } }
- Version:
- 2.6
- Author:
- nicklas
- See Also:
- Last modified
- $Date: 2015-05-12 11:27:08 +0200 (ti, 12 maj 2015) $
-
Field Summary
Modifier and TypeFieldDescriptionprivate ProgressReporter
Original progress reporter that progress updates should be forwarded to.private int
When the progress has reached this limit signal checking becomes disabled.private static final Logger
Log signals processing.private int
The current percentage status. -
Constructor Summary
ConstructorDescriptionProgressReporterSignalHandler
(Collection<Signal> supported) Create a new progress reporter signal handler that supports the specified signals. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Append a message to the previous one.void
If at least one signal has been received aSignalReceivedException
is thrown.void
Display a progress message.void
forwardTo
(ProgressReporter forwardTo) Set the progress reporter that progress information should be forwarded to.int
getLimit()
Get the percentage limit where signal checking becomes disabled.Get the list of received signals.void
handleSignal
(Signal signal) When signals are receieved, they are stored in a temporary list in the order they come in.boolean
Check if any signals has been received by this handler.void
setLimit
(int limit) Set the percentage limit.Methods inherited from class net.sf.basedb.core.signal.AbstractSignalHandler
addSignal, getSupportedSignals, removeSignal, supports
-
Field Details
-
logger
Log signals processing. -
forwardTo
Original progress reporter that progress updates should be forwarded to. -
received
-
percent
private int percentThe current percentage status. -
limit
private int limitWhen the progress has reached this limit signal checking becomes disabled. Default value is 100.
-
-
Constructor Details
-
ProgressReporterSignalHandler
Create a new progress reporter signal handler that supports the specified signals.- Parameters:
supported
- A collection with the signals that are initially supported. More signals can be added withAbstractSignalHandler.addSignal(Signal)
-
-
Method Details
-
handleSignal
When signals are receieved, they are stored in a temporary list in the order they come in. When the signal target callsdisplay(int, String)
orappend(String)
aSignalReceivedException
is throw if the list is not empty. Once the exception has been thrown the internal list is cleared.- Specified by:
handleSignal
in interfaceSignalHandler
- Parameters:
signal
- The signal to handle- Throws:
UnsupportedSignalException
- If the signal is not supported
-
append
Description copied from interface:ProgressReporter
Append a message to the previous one.- Specified by:
append
in interfaceProgressReporter
- Parameters:
message
- The message
-
display
Description copied from interface:ProgressReporter
Display a progress message.- Specified by:
display
in interfaceProgressReporter
- Parameters:
percent
- How many percent of the task that is completed or -1 if not knownmessage
- A message, or null
-
forwardTo
Set the progress reporter that progress information should be forwarded to.- Parameters:
forwardTo
- The progress reporter, or null if progress should not be forwarded
-
hasReceivedSignals
public boolean hasReceivedSignals()Check if any signals has been received by this handler.- Returns:
- TRUE if at least one signal has been received, FALSE otherwise
-
getReceivedSignals
Get the list of received signals. Calling this method clears the internal list and if no signals are received in the meantime, the next call to this method will return null.- Returns:
- A list with signals in the order they were received, or null if no signals has been received
-
getLimit
public int getLimit()Get the percentage limit where signal checking becomes disabled.- Returns:
- The limit in percent
-
setLimit
public void setLimit(int limit) Set the percentage limit. When the progress has reached the limit signal checking will be disable. The default value is 100 since there is no need to abort a process when it has already been finished.- Parameters:
limit
- The new limit
-
checkForSignals
public void checkForSignals()If at least one signal has been received aSignalReceivedException
is thrown. This method clears the received signals.
-