public class ProgressListener extends java.lang.Object implements QueryBatchListener
Reports on progress as batches are processed by sending an instance of the nested ProgressUpdate interface to instances of java.util.function.Consumer.
As an example, one approach would be to log the progress as a string:
QueryBatcher queryBatcher = moveMgr.newQueryBatcher(query)
.withConsistentSnapshot()
.onUrisReady(
new ProgressListener()
.onProgressUpdate(progressUpdate -> {
System.out.println(progressUpdate.getProgressAsString());
})
);
JobTicket ticket = moveMgr.startJob(queryBatcher);
queryBatcher.awaitCompletion();
moveMgr.stopJob(ticket);
The method withTotalResults
can be used to inform the listener of the total number
of expected results; this would likely have been determined by first using a QueryManager with a page length of zero
to determine the number of results for a query. Given the total results, each ProgressUpdate object will be aware of
how close to complete the QueryBatcher is.Modifier and Type | Class and Description |
---|---|
static interface |
ProgressListener.ProgressUpdate
Captures data of interest for a progress update.
|
static class |
ProgressListener.SimpleProgressUpdate
Simple implementation of ProgressUpdate; only real thing of interest in here is how it generates the progress
as a string for display purposes.
|
Constructor and Description |
---|
ProgressListener() |
ProgressListener(java.util.function.Consumer<ProgressListener.ProgressUpdate>... consumers)
Use this constructor for when the total number of results isn't known ahead of time.
|
ProgressListener(long totalResults,
java.util.function.Consumer<ProgressListener.ProgressUpdate>... consumers)
Use this constructor for when the total number of results is known ahead of time.
|
Modifier and Type | Method and Description |
---|---|
void |
initializeListener(QueryBatcher queryBatcher)
Initializes the start time so that each ProgressUpdate knows how long it occurred after the job was started.
|
protected void |
invokeConsumer(java.util.function.Consumer<ProgressListener.ProgressUpdate> consumer,
ProgressListener.ProgressUpdate progressUpdate)
Protected so that a subclass can override how a consumer is invoked, particularly how an exception is handled.
|
protected ProgressListener.ProgressUpdate |
newProgressUpdate(QueryBatch batch,
long startTime,
long totalForThisUpdate,
double timeSoFar)
A subclass can override this to provide a different implementation of ProgressUpdate.
|
ProgressListener |
onProgressUpdate(java.util.function.Consumer<ProgressListener.ProgressUpdate> consumer)
Configures a callback for progress updates.
|
void |
processEvent(QueryBatch batch)
Batches arrive in random order, so a ProgressUpdate is created and sent to each Consumer only if the
value of "getJobResultsSoFar" on the QueryBatch exceeds the number of results seen so far.
|
ProgressListener |
withTotalResults(long totalResults)
Configures the total number of results that progress listeners can expect.
|
public ProgressListener()
public ProgressListener(java.util.function.Consumer<ProgressListener.ProgressUpdate>... consumers)
consumers
- one or more callbacks for progress updatespublic ProgressListener(long totalResults, java.util.function.Consumer<ProgressListener.ProgressUpdate>... consumers)
totalResults
- the total number of results that is the processing goalconsumers
- one or more callbacks for progress updatespublic ProgressListener withTotalResults(long totalResults)
totalResults
- the total number of results that is the processing goalpublic ProgressListener onProgressUpdate(java.util.function.Consumer<ProgressListener.ProgressUpdate> consumer)
consumer
- a callback for progress updatespublic void initializeListener(QueryBatcher queryBatcher)
initializeListener
in interface QueryBatchListener
queryBatcher
- the query batcher retrieving the resultspublic void processEvent(QueryBatch batch)
For example, if there are 2 batches, and batch 2 is processed first by this listener followed by batch 1, a ProgressUpdate is only created when batch 2 is processed.
processEvent
in interface BatchListener<QueryBatch>
processEvent
in interface QueryBatchListener
batch
- the query batcher retrieving the resultsprotected ProgressListener.ProgressUpdate newProgressUpdate(QueryBatch batch, long startTime, long totalForThisUpdate, double timeSoFar)
batch
- the query batcher producing the resultsstartTime
- the time when the processing startedtotalForThisUpdate
- the number of results in this updatetimeSoFar
- the time taken by processing up to this updateprotected void invokeConsumer(java.util.function.Consumer<ProgressListener.ProgressUpdate> consumer, ProgressListener.ProgressUpdate progressUpdate)
consumer
- the callback for the progress updateprogressUpdate
- the object with progress update informationCopyright © 2013-2020 MarkLogic Corporation.