|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.core.signal.AbstractSignalHandler net.sf.basedb.core.signal.ProgressReporterSignalHandler
public class ProgressReporterSignalHandler
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"); } }
ThreadSignalHandler
Field Summary | |
---|---|
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 Logger |
logger
Log signals processing. |
private int |
percent
The current percentage status. |
private List<Signal> |
received
|
Constructor Summary | |
---|---|
ProgressReporterSignalHandler(Collection<Signal> supported)
Create a new progress reporter signal handler that supports the specified signals. |
Method Summary | |
---|---|
void |
append(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,
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. |
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. |
Methods inherited from class net.sf.basedb.core.signal.AbstractSignalHandler |
---|
addSignal, getSupportedSignals, removeSignal, supports |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final Logger logger
private ProgressReporter forwardTo
private List<Signal> received
private int percent
private int limit
Constructor Detail |
---|
public ProgressReporterSignalHandler(Collection<Signal> supported)
supported
- A collection with the signals that are initially supported.
More signals can be added with AbstractSignalHandler.addSignal(Signal)
Method Detail |
---|
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 handle
UnsupportedSignalException
- If the signal is not
supportedpublic void append(String message)
ProgressReporter
append
in interface ProgressReporter
message
- The messagepublic void display(int percent, 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 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.
|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |