Class AWorkerQueue<T>

  • Direct Known Subclasses:
    AccessSqlLogger, ErrorSqlLogger, LoginSqlLogger, LogoutSqlLogger, PageSqlLogger, SessionExpiredSqlLogger, SessionSqlLogger, SqlToDatabaseLogger

    public abstract class AWorkerQueue<T>
    extends java.lang.Object

    This is a worker with idle support and a queue allows asynchronous processing of objects, supports multi-object for batch processing and idle state to release resource if appropriate. The default maximum queue size is DEFAULT_MAX_QUEUE_SIZE and if exceeded, new objects are not put into the queue but discarded. This worker has a Timer which is triggers queue-processing every queueDelayMs after a queue has been populated via process(Object) or process(Collection), you implement the processObjects(Collection) method to process batched objects gathered into the queue during queueDelayMs. In a container environment you can gracefully #shutdownQueues() all queues by calling the method i.e. from context listener, in a standalone application you can do it via a shutdown hook or you can shutdown(boolean) s single instance.

    This class uses Executor for task execution which is shared among all instances of all concrete implementations of this class. By design, every single instance of implementation class posseses a single queue which is ought to be processed by a single thread on behalf of the executor, such as 5 instances will allow the executor to create up to 5 threads. Initially, the executor pool size is set to EXECUTOR_MIN_POOL_SIZE. The core pool size is changed according to number of instances of this class ranging from [ EXECUTOR_MIN_POOL_SIZE .. EXECUTOR_MAX_POOL_SIZE ]. If the queue is idling, the executor will shrink the thread pool size down to 0 after EXECUTOR_KEEPALIVE_MINS. A shutdown(boolean) on single instance will reduce the thread pool size.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_MAX_QUEUE_SIZE
      max queue size before discarding objects: 500
      protected org.apache.logging.log4j.Logger log  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AWorkerQueue​(int maxQueueSize, long idleThresholdMs, long queueDelayMs)
      Creates a queue
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void enqueue​(java.util.Collection<T> t)
      enqueue new objects for processing, does NOT block until process ends returns immediatelly
      protected void onIdle()
      implement this method to realease resources if worker is idle
      void process​(java.util.Collection<T> t)
      processing objects
      void process​(T t)
      conv. method to process only one element, see process(Collection)
      protected abstract void processObjects​(java.util.Collection<T> objects)
      implement the method to process the objects polled from queue (FIFO)
      static void shutdownQueues​(boolean discardQueuedObjects)
      shuts down all queues
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_MAX_QUEUE_SIZE

        public static final int DEFAULT_MAX_QUEUE_SIZE
        max queue size before discarding objects: 500
        See Also:
        Constant Field Values
      • log

        protected org.apache.logging.log4j.Logger log
    • Constructor Detail

      • AWorkerQueue

        protected AWorkerQueue​(int maxQueueSize,
                               long idleThresholdMs,
                               long queueDelayMs)
        Creates a queue
        Parameters:
        maxQueueSize - when set to 0 then default value is used (which is DEFAULT_MAX_QUEUE_SIZE ). A positive value sets a maximum queue size before discarding objects. A negative value removes any limit, which may cause memory issues in case the queue grows faster than it is processed.
        idleThresholdMs - the threshold for idle event or 0 for DISABLED, if disabled, the onIdle() is never called, otherwise it is called after the queue has been idling for given amount of time.
        queueDelayMs - delay in ms or 0 for DISABLED, this is a waiting period between adding a task to process into the queue and beginning of processing the queue, if 0 the queue is processed immediately.
    • Method Detail

      • shutdownQueues

        public static void shutdownQueues​(boolean discardQueuedObjects)
        shuts down all queues
        Parameters:
        discardQueuedObjects - - if set to false, processes queued objects ( this happens in current thread ).
      • onIdle

        protected void onIdle()
        implement this method to realease resources if worker is idle
      • processObjects

        protected abstract void processObjects​(java.util.Collection<T> objects)
        implement the method to process the objects polled from queue (FIFO)
        Parameters:
        objects - to process
      • process

        public void process​(java.util.Collection<T> t)
        processing objects
        Parameters:
        t - Objects to process
      • process

        public void process​(T t)
        conv. method to process only one element, see process(Collection)
        Parameters:
        t -
      • enqueue

        protected final void enqueue​(java.util.Collection<T> t)
        enqueue new objects for processing, does NOT block until process ends returns immediatelly
        Parameters:
        t - object to queue