Class TaskDaemon

  • All Implemented Interfaces:
    Guardable, Runnable
    Direct Known Subclasses:
    SessionHelper.SessionReaperDaemon

    public class TaskDaemon
    extends Daemon
    A Daemon thread handler that asynchronously executes Runnable tasks, either at a scheduled time or "as soon as possible".
    Author:
    cp 2003.10.09, cp 2006.02.23 (Coherence 3.2) bulletproofing for use in the CQC
    • Constructor Detail

      • TaskDaemon

        public TaskDaemon()
        Default constructor. Creates a TaskDaemon using default settings. The daemon will not be automatically started.
      • TaskDaemon

        public TaskDaemon​(String sName)
        Creates a TaskDaemon with the specified name. The daemon will not be automatically started.
        Parameters:
        sName - the thread name (may be null)
      • TaskDaemon

        public TaskDaemon​(String sName,
                          int nPriority,
                          boolean fStart)
        Creates a TaskDaemon with a specified name and priority.
        Parameters:
        sName - the thread name (may be null)
        nPriority - the thread priority, between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY inclusive
        fStart - pass true to start the thread immediately
      • TaskDaemon

        public TaskDaemon​(String sName,
                          int nPriority,
                          boolean fStart,
                          boolean fFinish,
                          int cMillisTimeout)
        Creates a TaskDaemon with a specified name and priority.
        Parameters:
        sName - the thread name (may be null)
        nPriority - the thread priority, between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY inclusive
        fStart - pass true to start the thread immediately
        fFinish - pass true to makes sure ripe tasks are run before the daemon shuts down
        cMillisTimeout - the number of milliseconds to wait after the previous task finished for a new task to be submitted before automatically shutting down the daemon thread
    • Method Detail

      • run

        public void run()
        The task processing loop.
        Specified by:
        run in interface Runnable
        Specified by:
        run in class Daemon
      • stop

        public void stop​(boolean fFinish)
        Request the daemon to stop, optionally completing tasks that have already been scheduled and are ready to be run.
        Parameters:
        fFinish - pass true if the daemon should finish any tasks that have already been scheduled before stopping
      • isFinishing

        public boolean isFinishing()
        Determine if the daemon will finish those scheduled tasks that are ripe (presently due to be run) before stopping.
        Returns:
        true if the daemon is configured to finish any ripe scheduled tasks before stopping
      • setFinishing

        public void setFinishing​(boolean fFinish)
        Specify whether the daemon will finish scheduled tasks before stopping.
        Parameters:
        fFinish - pass true to force the daemon to finish any scheduled tasks before stopping
      • getIdleTimeout

        public long getIdleTimeout()
        Determine the length of time that the daemon will live without any activity before it stops itself.
        Returns:
        the timeout for the TaskDaemon's thread to live before being shut down
      • setIdleTimeout

        public void setIdleTimeout​(long cMillis)
        Configure the daemon's timeout. Note that if the daemon shuts itself down, it will automatically restart when something is added to the queue.
        Parameters:
        cMillis - if greater than zero, the number of milliseconds that the daemon will wait with nothing in the queue before shutting itself down
      • executeTask

        public void executeTask​(Runnable task)
        Schedule a task to be run by the daemon "as soon as possible".
        Parameters:
        task - a Runnable object to invoke
      • scheduleTask

        public void scheduleTask​(Runnable task,
                                 long ldt)
        Schedule a task to be run at the specified time, or as soon after as possible.
        Parameters:
        task - a Runnable object to invoke
        ldt - a datetime value at which to run the task
      • executePeriodicTask

        public void executePeriodicTask​(Runnable task,
                                        long cMillisInterval)
        Schedule a periodic task to be run "as soon as possible", and to repeat at the specified interval.
        Parameters:
        task - a Runnable object to invoke
        cMillisInterval - the number of milliseconds to wait after the task is run before running it again
      • schedulePeriodicTask

        public void schedulePeriodicTask​(Runnable task,
                                         long ldtFirst,
                                         long cMillisInterval)
        Schedule a periodic task to be run at the specified time, and to repeat at the specified interval.
        Parameters:
        task - a Runnable object to invoke
        ldtFirst - a datetime value at which to first run the task
        cMillisInterval - the number of milliseconds to wait after the task is run before running it again
      • getTasks

        protected LongArray getTasks()
        Obtain the pending tasks.
        Returns:
        a LongArray keyed by SafeTimeMillis with a corresponding value being a List of tasks scheduled at that time
      • takeNextRipeTask

        protected Runnable takeNextRipeTask()
                                     throws InterruptedException
        Wait for the next scheduled task is ripe (due or overdue), then remove it from the pending schedule and return it.
        Returns:
        a task that is ripe to be run, or null if the TaskDaemon is shutting down and no task should be run
        Throws:
        InterruptedException - if this thread is interrupted while waiting for the next task
      • run

        protected void run​(Runnable task)
        Execute a Runnable task.
        Parameters:
        task - a Runnable object
      • getMostRecentTaskTime

        protected long getMostRecentTaskTime()
        Determine when the most recent task was run.
        Returns:
        the date/time at which the most recent task was run
      • updateMostRecentTaskTime

        protected void updateMostRecentTaskTime()
        Set the time that the most recent task was run to the current time.
      • instantiatePeriodicTask

        protected Runnable instantiatePeriodicTask​(Runnable task,
                                                   long cMillisInterval)
        Create a task that will automatically be run on a periodic basis.
        Parameters:
        task - the actual task to run
        cMillisInterval - the period of time, in milliseconds, to wait between runs of the task
        Returns:
        a task that will run itself periodically
      • toString

        public String toString()
        Return a human-readable String representation of the Daemon.
        Overrides:
        toString in class Daemon
        Returns:
        a String describing the Daemon
      • getDescription

        protected String getDescription()
        Format the Daemon attributes into a String for inclusion in the String returned from the Daemon.toString() method.
        Overrides:
        getDescription in class Daemon
        Returns:
        a String listing the attributes of the Daemon
      • onException

        protected void onException​(Throwable e,
                                   Runnable task)
        Process an exception that is thrown during a task execution. The default implementation logs the exception and continues.
        Parameters:
        e - Throwable object (a RuntimeException or an Error)
        task - the task that caused the exception