org.agrona.concurrent.MessageHandler Java Examples
The following examples show how to use
org.agrona.concurrent.MessageHandler.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OneToOneRingBufferConcurrentTest.java From agrona with Apache License 2.0 | 6 votes |
@Test public void shouldExchangeMessagesViaTryClaimAbort() { new ClaimAbort().start(); final MutableInteger count = new MutableInteger(); final MessageHandler handler = (msgTypeId, buffer, index, length) -> { final int iteration = buffer.getInt(index); assertEquals(count.get(), iteration); assertEquals(MSG_TYPE_ID, buffer.getInt(typeOffset(index - HEADER_LENGTH))); count.increment(); }; while (count.get() < REPETITIONS) { final int readCount = ringBuffer.read(handler); if (0 == readCount) { Thread.yield(); } } }
Example #2
Source File: OneToOneRingBufferConcurrentTest.java From agrona with Apache License 2.0 | 6 votes |
@Test public void shouldExchangeMessages() { new Producer().start(); final MutableInteger count = new MutableInteger(); final MessageHandler handler = (msgTypeId, buffer, index, length) -> { final int iteration = buffer.getInt(index); assertEquals(count.get(), iteration); count.increment(); }; while (count.get() < REPETITIONS) { final int readCount = ringBuffer.read(handler); if (0 == readCount) { Thread.yield(); } } }
Example #3
Source File: ManyToOneRingBufferTest.java From agrona with Apache License 2.0 | 6 votes |
@Test public void shouldLimitReadOfMessages() { final int msgLength = 16; final int recordLength = HEADER_LENGTH + msgLength; final int alignedRecordLength = align(recordLength, ALIGNMENT); final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> times[0]++; final int limit = 1; final int messagesRead = ringBuffer.read(handler, limit); assertThat(messagesRead, is(1)); assertThat(times[0], is(1)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).setMemory(headIndex, alignedRecordLength, (byte)0); inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, head + alignedRecordLength); }
Example #4
Source File: ManyToOneRingBufferTest.java From agrona with Apache License 2.0 | 6 votes |
@Test public void shouldNotReadSingleMessagePartWayThroughWriting() { final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(0); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> times[0]++; final int messagesRead = ringBuffer.read(handler); assertThat(messagesRead, is(0)); assertThat(times[0], is(0)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).getIntVolatile(lengthOffset(headIndex)); inOrder.verify(buffer, times(0)).setMemory(headIndex, 0, (byte)0); inOrder.verify(buffer, times(0)).putLongOrdered(HEAD_COUNTER_INDEX, headIndex); }
Example #5
Source File: OneToOneRingBufferTest.java From agrona with Apache License 2.0 | 6 votes |
@Test public void shouldLimitReadOfMessages() { final int msgLength = 16; final int recordLength = HEADER_LENGTH + msgLength; final int alignedRecordLength = align(recordLength, ALIGNMENT); final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> times[0]++; final int limit = 1; final int messagesRead = ringBuffer.read(handler, limit); assertThat(messagesRead, is(1)); assertThat(times[0], is(1)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, head + alignedRecordLength); inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte()); }
Example #6
Source File: OneToOneRingBufferTest.java From agrona with Apache License 2.0 | 6 votes |
@Test public void shouldNotReadSingleMessagePartWayThroughWriting() { final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(0); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> times[0]++; final int messagesRead = ringBuffer.read(handler); assertThat(messagesRead, is(0)); assertThat(times[0], is(0)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).getIntVolatile(lengthOffset(headIndex)); inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte()); inOrder.verify(buffer, times(0)).putLongOrdered(HEAD_COUNTER_INDEX, headIndex); }
Example #7
Source File: OneToOneRingBufferTest.java From agrona with Apache License 2.0 | 5 votes |
@Test public void shouldReadTwoMessages() { final int msgLength = 16; final int recordLength = HEADER_LENGTH + msgLength; final int alignedRecordLength = align(recordLength, ALIGNMENT); final long tail = alignedRecordLength * 2; final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength); when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> times[0]++; final int messagesRead = ringBuffer.read(handler); assertThat(messagesRead, is(2)); assertThat(times[0], is(2)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail); inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte()); }
Example #8
Source File: OneToOneRingBufferConcurrentTest.java From agrona with Apache License 2.0 | 5 votes |
@Test public void shouldExchangeMessagesViaTryClaimCommit() { new ClaimCommit().start(); final MutableInteger count = new MutableInteger(); final MessageHandler handler = (msgTypeId, buffer, index, length) -> { final int iteration = buffer.getInt(index); final long longVal = buffer.getLong(index + SIZE_OF_INT); assertEquals(count.get(), iteration); assertEquals(count.get() * 20L, longVal); count.increment(); }; while (count.get() < REPETITIONS) { final int readCount = ringBuffer.read(handler); if (0 == readCount) { Thread.yield(); } } }
Example #9
Source File: ManyToOneRingBufferTest.java From agrona with Apache License 2.0 | 5 votes |
@Test public void shouldReadTwoMessages() { final int msgLength = 16; final int recordLength = HEADER_LENGTH + msgLength; final int alignedRecordLength = align(recordLength, ALIGNMENT); final long tail = alignedRecordLength * 2; final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength); when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> times[0]++; final int messagesRead = ringBuffer.read(handler); assertThat(messagesRead, is(2)); assertThat(times[0], is(2)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).setMemory(headIndex, alignedRecordLength * 2, (byte)0); inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail); }
Example #10
Source File: ManyToOneRingBufferTest.java From agrona with Apache License 2.0 | 5 votes |
@Test public void shouldReadNothingFromEmptyBuffer() { final long head = 0L; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); final MessageHandler handler = (msgTypeId, buffer, index, length) -> fail("should not be called"); final int messagesRead = ringBuffer.read(handler); assertThat(messagesRead, is(0)); }
Example #11
Source File: OneToOneRingBufferTest.java From agrona with Apache License 2.0 | 5 votes |
@Test public void shouldReadNothingFromEmptyBuffer() { final long head = 0L; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); final MessageHandler handler = (msgTypeId, buffer, index, length) -> fail("should not be called"); final int messagesRead = ringBuffer.read(handler); assertThat(messagesRead, is(0)); }
Example #12
Source File: CopyBroadcastReceiver.java From agrona with Apache License 2.0 | 5 votes |
/** * Receive one message from the broadcast buffer. * * @param handler to be called for each message received. * @return the number of messages that have been received. */ public int receive(final MessageHandler handler) { int messagesReceived = 0; final BroadcastReceiver receiver = this.receiver; final long lastSeenLappedCount = receiver.lappedCount(); if (receiver.receiveNext()) { if (lastSeenLappedCount != receiver.lappedCount()) { throw new IllegalStateException("unable to keep up with broadcast"); } final int length = receiver.length(); final int capacity = scratchBuffer.capacity(); if (length > capacity && !scratchBuffer.isExpandable()) { throw new IllegalStateException( "buffer required length of " + length + " but only has " + capacity); } final int msgTypeId = receiver.typeId(); scratchBuffer.putBytes(0, receiver.buffer(), receiver.offset(), length); if (!receiver.validate()) { throw new IllegalStateException("unable to keep up with broadcast"); } handler.onMessage(msgTypeId, scratchBuffer, 0, length); messagesReceived = 1; } return messagesReceived; }
Example #13
Source File: DriverProxyTest.java From aeron with Apache License 2.0 | 4 votes |
private void assertReadsOneMessage(final MessageHandler handler) { final int messageCount = conductorBuffer.read(handler); assertEquals(1, messageCount); }
Example #14
Source File: OneToOneRingBufferTest.java From agrona with Apache License 2.0 | 4 votes |
@Test public void shouldCopeWithExceptionFromHandler() { final int msgLength = 16; final int recordLength = HEADER_LENGTH + msgLength; final int alignedRecordLength = align(recordLength, ALIGNMENT); final long tail = alignedRecordLength * 2; final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength); when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> { times[0]++; if (times[0] == 2) { throw new RuntimeException(); } }; try { ringBuffer.read(handler); } catch (final RuntimeException ignore) { assertThat(times[0], is(2)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail); inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte()); return; } fail("Should have thrown exception"); }
Example #15
Source File: ManyToOneRingBuffer.java From agrona with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public int read(final MessageHandler handler, final int messageCountLimit) { int messagesRead = 0; final AtomicBuffer buffer = this.buffer; final int headPositionIndex = this.headPositionIndex; final long head = buffer.getLong(headPositionIndex); final int capacity = this.capacity; final int headIndex = (int)head & (capacity - 1); final int maxBlockLength = capacity - headIndex; int bytesRead = 0; try { while ((bytesRead < maxBlockLength) && (messagesRead < messageCountLimit)) { final int recordIndex = headIndex + bytesRead; final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex)); if (recordLength <= 0) { break; } bytesRead += align(recordLength, ALIGNMENT); final int messageTypeId = buffer.getInt(typeOffset(recordIndex)); if (PADDING_MSG_TYPE_ID == messageTypeId) { continue; } ++messagesRead; handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH); } } finally { if (bytesRead != 0) { buffer.setMemory(headIndex, bytesRead, (byte)0); buffer.putLongOrdered(headPositionIndex, head + bytesRead); } } return messagesRead; }
Example #16
Source File: ManyToOneRingBuffer.java From agrona with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public int read(final MessageHandler handler) { return read(handler, Integer.MAX_VALUE); }
Example #17
Source File: ManyToOneRingBufferTest.java From agrona with Apache License 2.0 | 4 votes |
@Test public void shouldCopeWithExceptionFromHandler() { final int msgLength = 16; final int recordLength = HEADER_LENGTH + msgLength; final int alignedRecordLength = align(recordLength, ALIGNMENT); final long tail = alignedRecordLength * 2; final long head = 0L; final int headIndex = (int)head; when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head); when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength); when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID); when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength); final int[] times = new int[1]; final MessageHandler handler = (msgTypeId, buffer, index, length) -> { times[0]++; if (times[0] == 2) { throw new RuntimeException(); } }; try { ringBuffer.read(handler); } catch (final RuntimeException ignore) { assertThat(times[0], is(2)); final InOrder inOrder = inOrder(buffer); inOrder.verify(buffer, times(1)).setMemory(headIndex, alignedRecordLength * 2, (byte)0); inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail); return; } fail("Should have thrown exception"); }
Example #18
Source File: OneToOneRingBuffer.java From agrona with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public int read(final MessageHandler handler, final int messageCountLimit) { int messagesRead = 0; final AtomicBuffer buffer = this.buffer; final int headPositionIndex = this.headPositionIndex; final long head = buffer.getLong(headPositionIndex); int bytesRead = 0; final int capacity = this.capacity; final int headIndex = (int)head & (capacity - 1); final int contiguousBlockLength = capacity - headIndex; try { while ((bytesRead < contiguousBlockLength) && (messagesRead < messageCountLimit)) { final int recordIndex = headIndex + bytesRead; final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex)); if (recordLength <= 0) { break; } bytesRead += align(recordLength, ALIGNMENT); final int messageTypeId = buffer.getInt(typeOffset(recordIndex)); if (PADDING_MSG_TYPE_ID == messageTypeId) { continue; } ++messagesRead; handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH); } } finally { if (bytesRead != 0) { buffer.putLongOrdered(headPositionIndex, head + bytesRead); } } return messagesRead; }
Example #19
Source File: OneToOneRingBuffer.java From agrona with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public int read(final MessageHandler handler) { return read(handler, Integer.MAX_VALUE); }
Example #20
Source File: RingBuffer.java From agrona with Apache License 2.0 | 2 votes |
/** * Read as many messages as are available to end of the ring buffer to up a supplied maximum. * <p> * If the ring buffer wraps or encounters a type of record, such a a padding record, then an implementation * may choose to return and expect the caller to try again. The {@link #size()} method may be called to * determine of a backlog of message bytes remains in the ring buffer. * * @param handler to be called for processing each message in turn. * @param messageCountLimit the number of messages will be read in a single invocation. * @return the number of messages that have been processed. */ int read(MessageHandler handler, int messageCountLimit);
Example #21
Source File: RingBuffer.java From agrona with Apache License 2.0 | 2 votes |
/** * Read as many messages as are available to the end of the ring buffer. * <p> * If the ring buffer wraps or encounters a type of record, such a a padding record, then an implementation * may choose to return and expect the caller to try again. The {@link #size()} method may be called to * determine of a backlog of message bytes remains in the ring buffer. * * @param handler to be called for processing each message in turn. * @return the number of messages that have been processed. */ int read(MessageHandler handler);