use of net.openhft.chronicle.bytes.MethodWriterBuilder in project Chronicle-Queue by OpenHFT.
the class ChronicleReader method execute.
public void execute() {
long lastObservedTailIndex;
long highestReachedIndex = 0L;
boolean isFirstIteration = true;
boolean retryLastOperation;
boolean queueHasBeenModified;
final AtomicLong matchCounter = new AtomicLong(0L);
do {
try (final ChronicleQueue queue = createQueue();
final QueueEntryHandler messageConverter = entryHandlerFactory.get()) {
final ExcerptTailer tailer = queue.createTailer();
MessageHistory.set(new VanillaMessageHistory());
tlTailer = ThreadLocal.withInitial(queue::createTailer);
do {
if (highestReachedIndex != 0L) {
tailer.moveToIndex(highestReachedIndex);
}
final Bytes textConversionTarget = Bytes.elasticByteBuffer();
try {
moveToSpecifiedPosition(queue, tailer, isFirstIteration);
lastObservedTailIndex = tailer.index();
Consumer<String> messageConsumer = text -> applyFiltersAndLog(text, tailer.index(), matchCounter);
BooleanSupplier readOne;
if (methodReaderInterface == null) {
readOne = () -> readOne(messageConverter, tailer, messageConsumer);
} else {
// TODO: consider unifying this with messageConverter
Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(256);
Wire wire = wireType.apply(bytes);
if (wire instanceof TextWire)
((TextWire) wire).useTextDocuments();
MethodWriterBuilder<?> mwb = wire.methodWriterBuilder(methodReaderInterface);
if (showMessageHistory)
mwb.updateInterceptor((methodName, t) -> {
MessageHistory messageHistory = MessageHistory.get();
// this is an attempt to recognise that no MH was read and instead the method reader called reset(...) on it
if (messageHistory.sources() != 1 || messageHistory.timings() != 1)
bytes.append(messageHistory + System.lineSeparator());
return true;
});
MethodReader methodReader = tailer.methodReader(mwb.build());
readOne = () -> {
boolean found = methodReader.readOne();
if (found)
messageConsumer.accept(bytes.toString());
bytes.clear();
return found;
};
}
while (!Thread.currentThread().isInterrupted()) {
boolean found = readOne.getAsBoolean();
if (!found) {
if (tailInputSource) {
pauser.pause();
}
break;
} else {
if (matchLimitReached(matchCounter.get())) {
break;
}
}
pauser.reset();
}
} finally {
textConversionTarget.releaseLast();
highestReachedIndex = tailer.index();
isFirstIteration = false;
}
queueHasBeenModified = queueHasBeenModifiedSinceLastCheck(lastObservedTailIndex);
retryLastOperation = false;
if (!running || matchLimitReached(matchCounter.get()))
return;
} while (tailerDirection != BACKWARD && (tailInputSource || queueHasBeenModified));
} catch (final RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof DateTimeParseException) {
// ignore this error - due to a race condition between
// the reader creating a Queue (with default roll-cycle due to no files on disk)
// and the writer appending to the Queue with a non-default roll-cycle
retryLastOperation = true;
} else {
throw e;
}
}
} while (retryLastOperation);
}
Aggregations