Package org.agrona.concurrent.broadcast
Class BroadcastReceiver
- java.lang.Object
-
- org.agrona.concurrent.broadcast.BroadcastReceiver
-
public class BroadcastReceiver extends java.lang.Object
Receive messages broadcast from aBroadcastTransmitter
via an underlying buffer. Receivers can join a transmission stream at any point by consuming the latest message at the point of joining and forward.If a Receiver cannot keep up with the transmission stream then loss will be experienced. Loss is not an error condition.
Note: Each Receiver is not threadsafe but there can be zero or many receivers to a transmission stream.
-
-
Field Summary
Fields Modifier and Type Field Description private AtomicBuffer
buffer
private int
capacity
private long
cursor
private java.util.concurrent.atomic.AtomicLong
lappedCount
private int
latestCounterIndex
private long
nextRecord
private int
recordOffset
private int
tailCounterIndex
private int
tailIntentCounterIndex
-
Constructor Summary
Constructors Constructor Description BroadcastReceiver(AtomicBuffer buffer)
Construct a new broadcast receiver based on an underlyingAtomicBuffer
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MutableDirectBuffer
buffer()
The underlying buffer containing the broadcast message stream.int
capacity()
Get the capacity of the underlying broadcast buffer.long
lappedCount()
Get the number of times the transmitter has lapped this receiver around the buffer.int
length()
The length of the next message in the transmission stream.int
offset()
The offset for the beginning of the next message in the transmission stream.boolean
receiveNext()
Non-blocking receive of next message from the transmission stream.int
typeId()
Type of the message received.boolean
validate()
Validate that the current received record is still valid and has not been overwritten.private boolean
validate(long cursor, AtomicBuffer buffer, int capacity)
-
-
-
Field Detail
-
cursor
private long cursor
-
nextRecord
private long nextRecord
-
recordOffset
private int recordOffset
-
capacity
private final int capacity
-
tailIntentCounterIndex
private final int tailIntentCounterIndex
-
tailCounterIndex
private final int tailCounterIndex
-
latestCounterIndex
private final int latestCounterIndex
-
buffer
private final AtomicBuffer buffer
-
lappedCount
private final java.util.concurrent.atomic.AtomicLong lappedCount
-
-
Constructor Detail
-
BroadcastReceiver
public BroadcastReceiver(AtomicBuffer buffer)
Construct a new broadcast receiver based on an underlyingAtomicBuffer
. The underlying buffer must a power of 2 in size plus sufficient space for theBroadcastBufferDescriptor.TRAILER_LENGTH
.- Parameters:
buffer
- via which messages will be exchanged.- Throws:
java.lang.IllegalStateException
- if the buffer capacity is not a power of 2 plusBroadcastBufferDescriptor.TRAILER_LENGTH
in capacity.
-
-
Method Detail
-
capacity
public int capacity()
Get the capacity of the underlying broadcast buffer.- Returns:
- the capacity of the underlying broadcast buffer.
-
lappedCount
public long lappedCount()
Get the number of times the transmitter has lapped this receiver around the buffer. On each lap as least a buffer's worth of loss will be experienced.Note: This method is threadsafe for calling from an external monitoring thread.
- Returns:
- the capacity of the underlying broadcast buffer.
-
typeId
public int typeId()
Type of the message received.- Returns:
- typeId of the message received.
-
offset
public int offset()
The offset for the beginning of the next message in the transmission stream.- Returns:
- offset for the beginning of the next message in the transmission stream.
-
length
public int length()
The length of the next message in the transmission stream.- Returns:
- length of the next message in the transmission stream.
-
buffer
public MutableDirectBuffer buffer()
The underlying buffer containing the broadcast message stream.- Returns:
- the underlying buffer containing the broadcast message stream.
-
receiveNext
public boolean receiveNext()
Non-blocking receive of next message from the transmission stream.If loss has occurred then
lappedCount()
will be incremented.
-
validate
public boolean validate()
Validate that the current received record is still valid and has not been overwritten.If the receiver is not consuming messages fast enough to keep up with the transmitter then loss can be experienced resulting in messages being overwritten thus making them no longer valid.
- Returns:
- true if still valid otherwise false.
-
validate
private boolean validate(long cursor, AtomicBuffer buffer, int capacity)
-
-