public class ProgressReporterSignalHandler extends AbstractSignalHandler implements ProgressReporter
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"); } }
ThreadSignalHandler
Modifier and Type | Field and Description |
---|---|
private ProgressReporter |
forwardTo
Original progress reporter that progress updates should be
forwarded to.
|
private int |
limit
When the progress has reached this limit signal
checking becomes disabled.
|
private static org.slf4j.Logger |
logger
Log signals processing.
|
private int |
percent
The current percentage status.
|
private java.util.List<Signal> |
received |
Constructor and Description |
---|
ProgressReporterSignalHandler(java.util.Collection<Signal> supported)
Create a new progress reporter signal handler that supports the specified
signals.
|
Modifier and Type | Method and Description |
---|---|
void |
append(java.lang.String message)
Append a message to the previous one.
|
void |
checkForSignals()
If at least one signal has been received a
SignalReceivedException
is thrown. |
void |
display(int percent,
java.lang.String message)
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.
|
java.util.List<Signal> |
getReceivedSignals()
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 |
hasReceivedSignals()
Check if any signals has been received by this handler.
|
void |
setLimit(int limit)
Set the percentage limit.
|
addSignal, getSupportedSignals, removeSignal, supports
private static final org.slf4j.Logger logger
private ProgressReporter forwardTo
private java.util.List<Signal> received
private int percent
private int limit
public ProgressReporterSignalHandler(java.util.Collection<Signal> supported)
supported
- A collection with the signals that are initially supported.
More signals can be added with AbstractSignalHandler.addSignal(Signal)
public void handleSignal(Signal signal)
display(int, String)
or append(String)
a SignalReceivedException
is throw if the list is not
empty. Once the exception has been thrown the internal list
is cleared.handleSignal
in interface SignalHandler
signal
- The signal to handleUnsupportedSignalException
- If the signal is not
supportedpublic void append(java.lang.String message)
ProgressReporter
append
in interface ProgressReporter
message
- The messagepublic void display(int percent, java.lang.String message)
ProgressReporter
display
in interface ProgressReporter
percent
- How many percent of the task that is completed
or -1 if not knownmessage
- A message, or nullpublic void forwardTo(ProgressReporter forwardTo)
forwardTo
- The progress reporter, or null if progress should
not be forwardedpublic boolean hasReceivedSignals()
public java.util.List<Signal> getReceivedSignals()
public int getLimit()
public void setLimit(int limit)
limit
- The new limitpublic void checkForSignals()
SignalReceivedException
is thrown. This method clears the received signals.