Class ConcurrentQueryLoader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ConcurrentQueryLoader
    extends java.lang.Object
    implements java.io.Closeable
    Utility class for concurrently loading queries into a Monitor.

    This is useful to speed up startup times for a Monitor. You can use multiple threads to parse and index queries before starting matches.

    Use as follows:

         List<QueryError> errors = new ArrayList<>();
         try (ConcurrentQueryLoader loader = new ConcurrentQueryLoader(monitor, errors)) {
             for (MonitorQuery mq : getQueries()) {
                 loader.add(mq);
             }
         }
     

    The Monitor's MonitorQueryParser must be thread-safe for this to work correctly.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(MonitorQuery mq)
      Add a MonitorQuery to the loader's internal buffer
      void close()  
      private static <E> int drain​(java.util.concurrent.BlockingQueue<E> q, java.util.Collection<? super E> buffer, int numElements, long timeout, java.util.concurrent.TimeUnit unit)
      Drains the queue as BlockingQueue.drainTo(Collection, int), but if the requested numElements elements are not available, it will wait for them up to the specified timeout.
      • Methods inherited from class java.lang.Object

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

      • monitor

        private final Monitor monitor
      • executor

        private final java.util.concurrent.ExecutorService executor
      • shutdownLatch

        private final java.util.concurrent.CountDownLatch shutdownLatch
      • queue

        private final java.util.concurrent.BlockingQueue<MonitorQuery> queue
      • shutdown

        private boolean shutdown
      • errors

        private java.util.List<java.io.IOException> errors
    • Constructor Detail

      • ConcurrentQueryLoader

        public ConcurrentQueryLoader​(Monitor monitor)
        Create a new ConcurrentQueryLoader for a Monitor
        Parameters:
        monitor - Monitor
      • ConcurrentQueryLoader

        public ConcurrentQueryLoader​(Monitor monitor,
                                     int threads,
                                     int queueSize)
        Create a new ConcurrentQueryLoader
        Parameters:
        monitor - the Monitor to load queries to
        threads - the number of threads to use
        queueSize - the size of the buffer to hold queries in
    • Method Detail

      • add

        public void add​(MonitorQuery mq)
                 throws java.lang.InterruptedException
        Add a MonitorQuery to the loader's internal buffer

        If the buffer is full, this will block until there is room to add the MonitorQuery

        Parameters:
        mq - the monitor query
        Throws:
        java.lang.InterruptedException - if interrupted while waiting
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • drain

        private static <E> int drain​(java.util.concurrent.BlockingQueue<E> q,
                                     java.util.Collection<? super E> buffer,
                                     int numElements,
                                     long timeout,
                                     java.util.concurrent.TimeUnit unit)
                              throws java.lang.InterruptedException
        Drains the queue as BlockingQueue.drainTo(Collection, int), but if the requested numElements elements are not available, it will wait for them up to the specified timeout.

        Taken from Google Guava 18.0 Queues

        Type Parameters:
        E - the type of the queue
        Parameters:
        q - the blocking queue to be drained
        buffer - where to add the transferred elements
        numElements - the number of elements to be waited for
        timeout - how long to wait before giving up, in units of unit
        unit - a TimeUnit determining how to interpret the timeout parameter
        Returns:
        the number of elements transferred
        Throws:
        java.lang.InterruptedException - if interrupted while waiting