java.lang.Object
org.apache.lucene.monitor.ConcurrentQueryLoader
- All Implemented Interfaces:
Closeable
,AutoCloseable
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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
private List
<IOException> private final ExecutorService
private final Monitor
private final BlockingQueue
<MonitorQuery> private boolean
private final CountDownLatch
-
Constructor Summary
ConstructorsConstructorDescriptionConcurrentQueryLoader
(Monitor monitor) Create a new ConcurrentQueryLoader for aMonitor
ConcurrentQueryLoader
(Monitor monitor, int threads, int queueSize) Create a new ConcurrentQueryLoader -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(MonitorQuery mq) Add a MonitorQuery to the loader's internal buffervoid
close()
private static <E> int
drain
(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit) Drains the queue asBlockingQueue.drainTo(Collection, int)
, but if the requestednumElements
elements are not available, it will wait for them up to the specified timeout.
-
Field Details
-
monitor
-
executor
-
shutdownLatch
-
queue
-
shutdown
private boolean shutdown -
errors
-
DEFAULT_QUEUE_SIZE
public static final int DEFAULT_QUEUE_SIZE- See Also:
-
-
Constructor Details
-
ConcurrentQueryLoader
Create a new ConcurrentQueryLoader for aMonitor
- Parameters:
monitor
- Monitor
-
ConcurrentQueryLoader
Create a new ConcurrentQueryLoader- Parameters:
monitor
- the Monitor to load queries tothreads
- the number of threads to usequeueSize
- the size of the buffer to hold queries in
-
-
Method Details
-
add
Add a MonitorQuery to the loader's internal bufferIf the buffer is full, this will block until there is room to add the MonitorQuery
- Parameters:
mq
- the monitor query- Throws:
InterruptedException
- if interrupted while waiting
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
drain
private static <E> int drain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit) throws InterruptedException Drains the queue asBlockingQueue.drainTo(Collection, int)
, but if the requestednumElements
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 drainedbuffer
- where to add the transferred elementsnumElements
- the number of elements to be waited fortimeout
- how long to wait before giving up, in units ofunit
unit
- aTimeUnit
determining how to interpret the timeout parameter- Returns:
- the number of elements transferred
- Throws:
InterruptedException
- if interrupted while waiting
-