/** * Poll for new messages in a stream. If new messages are found beyond the last consumed position then they * will be delivered to the {@link FileBlockHandler} up to a limited number of bytes. * * @param fileBlockHandler to which block is delivered. * @param blockLengthLimit up to which a block may be in length. * @return the number of bytes that have been consumed. */ public int filePoll(FileBlockHandler fileBlockHandler, int blockLengthLimit) { if (isClosed) { return(0); } long position = subscriberPosition.get(); int termOffset = (int)position & termLengthMask; int activeIndex = indexByPosition(position, positionBitsToShift); DirectBuffer termBuffer = termBuffers[activeIndex]; int capacity = termBuffer.capacity(); int limit = Math.min(termOffset + blockLengthLimit, capacity); int resultingOffset = TermBlockScanner.scan(termBuffer, termOffset, limit); int bytesConsumed = resultingOffset - termOffset; if (resultingOffset > termOffset) { try { long offset = ((long)capacity * activeIndex) + termOffset; int termId = termBuffer.getInt(termOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN); fileBlockHandler.onBlock(logBuffers.fileChannel(), offset, bytesConsumed, sessionId, termId); } catch (Throwable t) { errorHandler.onError(t); } subscriberPosition.setOrdered(position + bytesConsumed); } return(bytesConsumed); }
/** * Poll for new messages in a stream. If new messages are found beyond the last consumed position then they * will be delivered to the {@link BlockHandler} up to a limited number of bytes. * * @param blockHandler to which block is delivered. * @param blockLengthLimit up to which a block may be in length. * @return the number of bytes that have been consumed. */ public int blockPoll(BlockHandler blockHandler, int blockLengthLimit) { if (isClosed) { return(0); } long position = subscriberPosition.get(); int termOffset = (int)position & termLengthMask; DirectBuffer termBuffer = activeTermBuffer(position); int limit = Math.min(termOffset + blockLengthLimit, termBuffer.capacity()); int resultingOffset = TermBlockScanner.scan(termBuffer, termOffset, limit); int bytesConsumed = resultingOffset - termOffset; if (resultingOffset > termOffset) { try { int termId = termBuffer.getInt(termOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN); blockHandler.onBlock(termBuffer, termOffset, bytesConsumed, sessionId, termId); } catch (Throwable t) { errorHandler.onError(t); } subscriberPosition.setOrdered(position + bytesConsumed); } return(bytesConsumed); }