Package dk.netarkivet.common.lifecycle
Class PeriodicTaskExecutor
- java.lang.Object
-
- dk.netarkivet.common.lifecycle.PeriodicTaskExecutor
-
public final class PeriodicTaskExecutor extends Object
This class wraps aScheduledThreadPoolExecutor
, allowing to periodically run one or severalRunnable
tasks (fixed rate execution). It actively monitors task execution in a separate "checker" thread, allowing to catch and process anyRuntimeException
that would be thrown during task execution, which cannot be done by simply overridingThreadPoolExecutor.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PeriodicTaskExecutor.PeriodicTask
Represents a periodic task.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
shutdown()
Shuts down the executor, attempting to stop any ongoing task execution.
-
-
-
Constructor Detail
-
PeriodicTaskExecutor
public PeriodicTaskExecutor(String taskId, Runnable task, long secondsBeforeFirstExec, long secondsBetweenExec)
Builds an executor for a single task.- Parameters:
taskId
- the task id string (should be unique)task
- the actualRunnable
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.
-
PeriodicTaskExecutor
public PeriodicTaskExecutor(PeriodicTaskExecutor.PeriodicTask... tasks)
Builds an executor for a set of tasks.- Parameters:
tasks
- the task definitions.
-
-