public class PeriodicTaskExceptionHandler extends Object
PeriodicTask under abnormal circumstances.
Setters provided by this class can be used to establish policies the periodic task
should follow when it throws an exception or runs for an unusually long time.
Callback methods onXXX(PeriodicTask task, ... can be overridden to execute
client code under these circumstances. They should be implemented to finish quickly,
any time consuming or potentially blocking operations should be offloaded to other
threads. Default implementations provided by this class log messages at the level
set through a call to setLevel(...) or inherited from the Scheduler
where the task runs.
Sample Usage. The following code sketch creates a fixed rate task that will skip execution if the previous one has not finished, log a message if an execution is taking longer than the task period, and quit after 2 failures.
Scheduler scheduler = new Scheduler("tester", 1);
...
PeriodicTaskExceptionHandler exceptionHandler = new PeriodicTaskExceptionHandler() {
public void onLongExecution(PeriodicTask task) {
Logger logger = task.getLogger();
if (logger != null) {
logger.log(getLevel(), "Task "+ task.getTaskName() +" is taking more than its period.", (Object[])null);
}
alertOperator();
}
};
exceptionHandler.setSkipOverdueExecutions(true);
exceptionHandler.setDetectLongExecutions(true);
exceptionHandler.setMaxFailures(2);
exceptionHandler.setResetFailureCountOnSuccess(false);
scheduler.scheduleAtFixedRate(command, 0, 1, TimeUnit.SECONDS, "Task name", exceptionHandler);
...
| Constructor and Description |
|---|
PeriodicTaskExceptionHandler() |
| Modifier and Type | Method and Description |
|---|---|
Level |
getLevel() |
int |
getLongExecutionThreshold() |
int |
getMaxFailures() |
boolean |
isDetectLongExecutions() |
boolean |
isResetFailureCountOnSuccess() |
boolean |
isSkipOverdueExecutions() |
void |
onException(PeriodicTask task,
Throwable exception)
Called when a task throws an exception, but is not going to be terminated.
|
void |
onFinalException(PeriodicTask task,
Throwable exception)
Called when a task throws an exception, and is going to be terminated.
|
void |
onLongExecution(PeriodicTask task)
Called when an execution that takes longer than the task period is detected.
|
void |
onSkippedExecutions(PeriodicTask task,
int nSkipped)
Called when a fixed rate task executes after skipping one or more previous executions
that could not start on time.
|
void |
setDetectLongExecutions(boolean detectLongExecutions)
Sets a policy on whether or not executions that take longer than the task period to complete
should be detected and
onLongExecution(...) called. |
void |
setLevel(Level level)
Sets logging level.
|
void |
setLongExecutionThreshold(int longExecutionThreshold,
TimeUnit unit)
Sets a policy on detecting long executions.
|
void |
setMaxFailures(int maxFailures)
Sets the number of times the task can throw an exception before it is terminated.
|
void |
setResetFailureCountOnSuccess(boolean resetFailureCountOnSuccess)
Sets a policy on whether or not only consecutive failures should be counted towards the
allowed maximum.
|
void |
setSkipOverdueExecutions(boolean skipOverdueExecutions)
Sets a policy on skipping executions that cannot start on time.
|
public void setSkipOverdueExecutions(boolean skipOverdueExecutions)
true
to prevent executions from piling up when a previous execution takes a long time,
and then running in rapid succession when it finally finishes. This option is
usually not suitable for task periods less than 100 milliseconds.
The default is false.skipOverdueExecutions - If true, executions that cannot start on time are skipped.public void setDetectLongExecutions(boolean detectLongExecutions)
onLongExecution(...) called. Note that setting this flag to
true adds significant overhead, and is usually not suitable for task periods of less
than 100 milliseconds. The default is false.
Calling this method with true is equivalent to setLongExecutionThreshold(1, null);
Calling this method with false is equivalent to setLongExecutionThreshold(0, null);
detectLongExecutions - true if long executions should be detected.public void setLongExecutionThreshold(int longExecutionThreshold,
TimeUnit unit)
longExecutionThreshold to complete
should be detected and onLongExecution(...) called. Note that detecting
long executions adds significant overhead, and is usually not suitable for task
periods of less than 100 milliseconds.
If the threshold is set to zero (default), long executions are not detected.
If the unit is null, the value of longExecutionThreshold
is interpreted as the number of task periods.
longExecutionThreshold - Threshold.unit - Init for threshold.public void setMaxFailures(int maxFailures)
maxFailures - Allowed number of failures.public void setResetFailureCountOnSuccess(boolean resetFailureCountOnSuccess)
true.resetFailureCountOnSuccess - If true, only consecutive failures are counted towards the allowed maximum.public void setLevel(Level level)
level - Level.public boolean isSkipOverdueExecutions()
public boolean isDetectLongExecutions()
public int getLongExecutionThreshold()
public int getMaxFailures()
public boolean isResetFailureCountOnSuccess()
public Level getLevel()
public void onSkippedExecutions(PeriodicTask task, int nSkipped)
task - Periodic task.nSkipped - Number of skipped executions.public void onLongExecution(PeriodicTask task)
detectLongExecutions property has been set to true.
The default implementation logs a message.task - Periodic task.public void onException(PeriodicTask task, Throwable exception)
task - Periodic task.exception - Thrown exception.public void onFinalException(PeriodicTask task, Throwable exception)
task - Periodic task.exception - Thrown exception.Copyright © 2022 LSST. All rights reserved.