public final class PeriodicTaskExecutor extends Object
ScheduledThreadPoolExecutor
, allowing to periodically run one or several Runnable
tasks (fixed rate execution). It actively monitors task execution in a separate "checker" thread, allowing to catch
and process any RuntimeException
that would be thrown during task execution, which cannot be done by simply
overriding ThreadPoolExecutor.afterExecute(java.lang.Runnable, java.lang.Throwable)
.
TODO Currently RuntimeException
are only caught and logged, but the executor stops scheduling future
executions. We should implement a configurable restart mechanism, possibly with exception filtering.
Modifier and Type | Class and Description |
---|---|
static class |
PeriodicTaskExecutor.PeriodicTask
Represents a periodic task.
|
Constructor and Description |
---|
PeriodicTaskExecutor(PeriodicTaskExecutor.PeriodicTask... tasks)
Builds an executor for a set of tasks.
|
PeriodicTaskExecutor(String taskId,
Runnable task,
long secondsBeforeFirstExec,
long secondsBetweenExec)
Builds an executor for a single task.
|
Modifier and Type | Method and Description |
---|---|
void |
shutdown()
Shuts down the executor, attempting to stop any ongoing task execution.
|
public PeriodicTaskExecutor(String taskId, Runnable task, long secondsBeforeFirstExec, long secondsBetweenExec)
taskId
- the task id string (should be unique)task
- the actual Runnable
object.secondsBeforeFirstExec
- the delay in seconds between starting the executor and the initial task execution.secondsBetweenExec
- the delay in seconds between two successive task executions.public PeriodicTaskExecutor(PeriodicTaskExecutor.PeriodicTask... tasks)
tasks
- the task definitions.public void shutdown()
Copyright © 2005–2016 The Royal Danish Library, the Danish State and University Library, the National Library of France and the Austrian National Library.. All rights reserved.