public class ConcurrentQueryLoader
extends java.lang.Object
implements java.io.Closeable
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.
Modifier and Type | Class and Description |
---|---|
private class |
ConcurrentQueryLoader.Worker |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_QUEUE_SIZE |
private java.util.List<java.io.IOException> |
errors |
private java.util.concurrent.ExecutorService |
executor |
private Monitor |
monitor |
private java.util.concurrent.BlockingQueue<MonitorQuery> |
queue |
private boolean |
shutdown |
private java.util.concurrent.CountDownLatch |
shutdownLatch |
Constructor and Description |
---|
ConcurrentQueryLoader(Monitor monitor)
Create a new ConcurrentQueryLoader for a
Monitor |
ConcurrentQueryLoader(Monitor monitor,
int threads,
int queueSize)
Create a new ConcurrentQueryLoader
|
Modifier and Type | Method and 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. |
private final Monitor monitor
private final java.util.concurrent.ExecutorService executor
private final java.util.concurrent.CountDownLatch shutdownLatch
private final java.util.concurrent.BlockingQueue<MonitorQuery> queue
private boolean shutdown
private java.util.List<java.io.IOException> errors
public static final int DEFAULT_QUEUE_SIZE
public ConcurrentQueryLoader(Monitor monitor)
Monitor
monitor
- Monitorpublic ConcurrentQueryLoader(Monitor monitor, int threads, int queueSize)
monitor
- the Monitor to load queries tothreads
- the number of threads to usequeueSize
- the size of the buffer to hold queries inpublic void add(MonitorQuery mq) throws java.lang.InterruptedException
If the buffer is full, this will block until there is room to add the MonitorQuery
mq
- the monitor queryjava.lang.InterruptedException
- if interrupted while waitingpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
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
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
E
- the type of the queueq
- the blocking queue to be drainedbuffer
- where to add the transferred elementsnumElements
- the number of elements to be waited fortimeout
- how long to wait before giving up, in units of unit
unit
- a TimeUnit
determining how to interpret the timeout parameterjava.lang.InterruptedException
- if interrupted while waiting