Class PeriodicHelper


  • public class PeriodicHelper
    extends Helper
    A helper class which adds a background thread when the helper class is activated and removes the background thread when it is deactivated. The thread loops, calling a specific trigger method, periodicTrigger() and then waiting for a fixed time. Rule sets which employ the helper class can attach rules to the periodic trigger method in order to perform operations which should happen at regular intervalsx. This is useful, for example, for rule sets which collect statistical information. A periodically triggered rule can read and then rezero a set of stats counters, allowing it to provide regular interval statistics. The basic way to use this class is to employ attach a rule to method periodicTrigger(). The rule will be triggered at 10 second intervals. If the period needs to be altered then a rule attached to method getPeriod() can be used to return an alternative value. getPeriod() is called once when the periodic trigger thread is created. If the wait interval needs to be recomputed before each wait then a rule attached to method resetPeriod(long) can be used to return the desired value. The input argument is the value returned by the call to getPeriod().
    • Field Detail

      • DEFAULT_PERIOD

        public static final long DEFAULT_PERIOD
        the default period which the helper will wait for between calls to periodicTrigger in milliseconds. this can be redefined either by overriding defaultPeriod
        See Also:
        Constant Field Values
      • theHelper

        private static PeriodicHelper theHelper
        singleton instance holding the current periodic helper
      • shutDown

        private boolean shutDown
        flag which enforces shutdown
    • Constructor Detail

      • PeriodicHelper

        public PeriodicHelper​(Rule rule)
    • Method Detail

      • periodicTrigger

        protected void periodicTrigger()
        a method which is called at regular intervals by the periodic helper thread to trigger rule processing. This can be redefined by attaching one or more rules to the method. It is also possible to override this method in a subclass
      • getPeriod

        protected long getPeriod()
        a method which is called when the periodic helper thread is started to compute the interval in milliseconds for which the thread should wait between calls to the trigger method which by default returns the default interval of 10 seconds. This can either be overridden or redefined by attaching a rule to the method.
        Returns:
        the wait period
      • resetPeriod

        protected long resetPeriod​(long initialPeriod)
        a method which is called when the periodic helper thread is about to wait which by default returns the input value. this can be overridden or redefined by attaching a rule to the method.
        Parameters:
        initialPeriod - the initial wait time returned by getPeriod when the periodic thread was created
        Returns:
        the wait period
      • activated

        public static void activated()
        helper activation method which creates a periodic helper thread to perform periodic calls to the trigger method. should only be called when synchronized on PeriodicHelper.class.
      • deactivated

        public static void deactivated()
        helper deactivation method which shuts down the periodic helper thread. will only be called when synchronized on PeriodicHelper.class
      • start

        private void start()
        method called in activate to create and run the shutdown thread. will only be called when synchronized on PeriodicHelper.class
      • shutdown

        private void shutdown()
        method called in deactivate the helper thread. will only be called when synchronized on PeriodicHelper.class
      • doWait

        private boolean doWait​(long periodMilliSecs)
        method called by the periodic helper thread to wait between calls to the trigger method