OneToManyRingBuffer implementation in agrona

Viewed 190

This may be a stupid question but there's a OneToOneRingBuffer and a ManyToOneRingBuffer available in aeron (agrona). I have a single producer and many consumers I'm wondering how to achieve the equivalent effect of OneToManyRingBuffer?

1 Answers

BroadcastTransmitter and BroadcastReceiver in Agrona is one approach that would give you one-to-many semantics.

Worth noting that slow consumers may see messages dropped. If this is not desired then you could also look at Aeron IPC which would exert back-pressure on the publisher in this scenario.

Example

Top-level, here's how to use it:

// Create broadcast buffer
int capacity = 1 << 10; // Must be power of two
int bufferSize = capacity + BroadcastBufferDescriptor.TRAILER_LENGTH;
UnsafeBuffer broadcastBuffer = new UnsafeBuffer(new byte[bufferSize]);

// Create transmitter
BroadcastTransmitter transmitter = new BroadcastTransmitter(broadcastBuffer);

// Create receiver (can create many of these)
BroadcastReceiver broadcastReceiver = new BroadcastReceiver(broadcastBuffer);
CopyBroadcastReceiver copyBroadcastReceiver = new CopyBroadcastReceiver(broadcastReceiver);

// Send message
int msgTypeId = 1;
MutableDirectBuffer msgBuffer = new ExpandableArrayBuffer();
int msgLength = msgBuffer.putStringWithoutLengthAscii(0, "Hello World!");
transmitter.transmit(msgTypeId, msgBuffer, 0, msgLength);

// Receive message
copyBroadcastReceiver.receive(
  (msgType, buffer, offset, length) -> System.out.println(buffer.getStringWithoutLengthAscii(offset, length)));
Related