Class PeriodicTaskExecutor


  • public final class PeriodicTaskExecutor
    extends java.lang.Object
    This class wraps a 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.

    • 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PeriodicTaskExecutor

        public PeriodicTaskExecutor​(java.lang.String taskId,
                                    java.lang.Runnable task,
                                    long secondsBeforeFirstExec,
                                    long secondsBetweenExec)
        Builds an executor for a single task.
        Parameters:
        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.
    • Method Detail

      • shutdown

        public void shutdown()
        Shuts down the executor, attempting to stop any ongoing task execution.