Class DelayedOcassionalProcessor<T>

java.lang.Object
net.solarnetwork.central.support.DelayedOcassionalProcessor<T>
All Implemented Interfaces:
Runnable, AsyncProcessor<T>, net.solarnetwork.service.PingTest, net.solarnetwork.service.ServiceLifecycleObserver

public abstract class DelayedOcassionalProcessor<T> extends Object implements AsyncProcessor<T>, Runnable, net.solarnetwork.service.ServiceLifecycleObserver, net.solarnetwork.service.PingTest
Asynchronously process ocassionally-appearing items.

The goal of this class is to help asynchronously process "bursty" items that come into being inconsistently, and sometimes in a duplicate manner, after a brief delay. The delay allows de-duplication to occur within the delay period.

Different processing styles can be achieved via different Queue implementations, such as LinkedHashSetBlockingQueue for de-duplication or DelayQueueSet for consistently delayed de-duplication.

  • Field Details

    • DEFAULT_DELAY

      public static final Duration DEFAULT_DELAY
      The delay property default value.
    • DEFAULT_QUEUE_SIZE_ALERT_THRESHOLD

      public static final int DEFAULT_QUEUE_SIZE_ALERT_THRESHOLD
      The queueSizeAlertThreshold default value.
      See Also:
    • clock

      protected final Clock clock
      The clock to use.
    • stats

      protected final net.solarnetwork.util.StatTracker stats
      The statistics tracker.
  • Constructor Details

    • DelayedOcassionalProcessor

      public DelayedOcassionalProcessor(Clock clock, net.solarnetwork.util.StatTracker stats, org.springframework.scheduling.TaskScheduler scheduler, Queue<T> items)
      Constructor.
      Parameters:
      clock - the clock
      stats - the stats
      scheduler - the scheduler
      items - the item buffer; this must support concurrent access
      delegate - the delegate
      Throws:
      IllegalArgumentException - if any argument is null
  • Method Details

    • serviceDidStartup

      public void serviceDidStartup()
      Specified by:
      serviceDidStartup in interface net.solarnetwork.service.ServiceLifecycleObserver
    • serviceDidShutdown

      public void serviceDidShutdown()
      Specified by:
      serviceDidShutdown in interface net.solarnetwork.service.ServiceLifecycleObserver
    • asyncProcessItem

      public void asyncProcessItem(T item)
      Description copied from interface: AsyncProcessor
      Asynchronously process an item.
      Specified by:
      asyncProcessItem in interface AsyncProcessor<T>
      Parameters:
      item - the item to asynchronously process
    • cancelAsyncProcessItem

      public boolean cancelAsyncProcessItem(T item)
      Description copied from interface: AsyncProcessor
      Cancel a pending task previously submitted to AsyncProcessor.asyncProcessItem(T).
      Specified by:
      cancelAsyncProcessItem in interface AsyncProcessor<T>
      Parameters:
      item - the item to cancel processing for
    • processItemInternal

      protected abstract void processItemInternal(T item)
      Process a delayed item.
      Parameters:
      item - the item to process
    • run

      public final void run()
      Specified by:
      run in interface Runnable
    • getPingTestId

      public String getPingTestId()
      Specified by:
      getPingTestId in interface net.solarnetwork.service.PingTest
    • getPingTestName

      public String getPingTestName()
      Specified by:
      getPingTestName in interface net.solarnetwork.service.PingTest
    • getPingTestMaximumExecutionMilliseconds

      public long getPingTestMaximumExecutionMilliseconds()
      Specified by:
      getPingTestMaximumExecutionMilliseconds in interface net.solarnetwork.service.PingTest
    • performPingTest

      public net.solarnetwork.service.PingTest.Result performPingTest() throws Exception
      Specified by:
      performPingTest in interface net.solarnetwork.service.PingTest
      Throws:
      Exception
    • getDelay

      public final Duration getDelay()
      Get the delay.
      Returns:
      the delay; defaults to DEFAULT_DELAY
    • setDelay

      public final void setDelay(Duration delay)
      Set the delay.
      Parameters:
      delay - the delay to set
    • getQueueSizeAlertThreshold

      public final int getQueueSizeAlertThreshold()
      Set the queue size alert threshold.
      Returns:
      the queue size threshold after which the ping test should fail; defaults to DEFAULT_QUEUE_SIZE_ALERT_THRESHOLD
    • setQueueSizeAlertThreshold

      public final void setQueueSizeAlertThreshold(int queueSizeAlertThreshold)
      Set the queue size alert threshold.
      Parameters:
      queueSizeAlertThreshold - the queue size threshold after which the ping test should fail