M
- message typepublic abstract class MessageBuffer<M extends Message> extends Object
byte channel
and driven by an I/O
loop
.
Implementations are expected to provide means to get and put a message into the provided byte buffers.
Writers feed the data in form of messages via one of the
queue(java.util.List<M>)
methods and the data will get flushed
Modifier and Type | Field and Description |
---|---|
protected static int |
BUFFER_SIZE
Initial buffer size for in-bound & out-bound byte buffers.
|
protected static double |
GROWTH_FACTOR
Default growth factor for the out-bound byte buffer.
|
protected org.slf4j.Logger |
log
Shared logger.
|
protected static int |
MAX_AGE
Default maximum age in milliseconds before the buffer becomes stale.
|
Modifier | Constructor and Description |
---|---|
protected |
MessageBuffer(ByteChannel ch,
IOLoop<M,?> loop)
Creates a message buffer backed by the specified byte channel and
driven by the given I/O loop.
|
protected |
MessageBuffer(ByteChannel ch,
IOLoop<M,?> loop,
SSLContext sslContext)
Creates a message buffer backed by the specified byte channel and
driven by the given I/O loop.
|
Modifier and Type | Method and Description |
---|---|
boolean |
alreadyDiscarded()
Returns true if this buffer has already been discarded.
|
List<M> |
dequeue()
Gets a list of messages from the buffer in non-blocking fashion.
|
void |
discard()
Discards the message buffer.
|
void |
flush()
Flushes any pending writes accumulated in the out-bound buffer to the
backing socket channel.
|
boolean |
flushFailed()
Returns true if a prior flush encountered an error.
|
protected abstract M |
get(ByteBuffer rb)
Gets a single message from the supplied byte buffer.
|
Exception |
getFlushError()
Gets the prior flush error, if one occurred.
|
protected ByteBuffer |
growBuffer(ByteBuffer buffer)
Grows the given buffer by
growthFactor() and returns
a new buffer instance with any remaining data copied into it. |
protected double |
growthFactor()
Returns the out-bound buffer growth-factor.
|
ThroughputTracker |
inBytes()
Returns the in-bound bytes throughput tracker.
|
ThroughputTracker |
inMessages()
Returns the in-bound messages throughput tracker.
|
SelectionKey |
key()
Returns the selection key used for registration of the backing
socket channel.
|
IOLoop<M,?> |
loop()
Returns the IO loop to which this stream is bound.
|
protected int |
maxAge()
Returns the maximum age before buffer is considered stale without any
dequeue/queue operations.
|
ThroughputTracker |
outBytes()
Returns the out-bound bytes throughput tracker.
|
ThroughputTracker |
outMessages()
Returns the out-bound messages throughput tracker.
|
protected abstract void |
put(M message,
ByteBuffer wb)
Puts the specified message into the internal buffer.
|
void |
queue(List<M> messages)
Queues the list of messages to the buffer and flushes to the backing
channel if necessary.
|
void |
queue(M message)
Queues the message to the buffer and flushes to the backing channel if
necessary.
|
void |
setKey(SelectionKey key)
Sets the select key obtained when the backing channel of this stream
was registered with the IO loop selector.
|
protected org.slf4j.Logger log
protected static final int BUFFER_SIZE
protected static final double GROWTH_FACTOR
protected static final int MAX_AGE
protected MessageBuffer(ByteChannel ch, IOLoop<M,?> loop)
ch
- backing socket channelloop
- driver IO loopprotected MessageBuffer(ByteChannel ch, IOLoop<M,?> loop, SSLContext sslContext)
ch
- backing socket channelloop
- driver IO loopsslContext
- TLS secure contextpublic boolean alreadyDiscarded()
discard()
has been called.public void discard()
public void setKey(SelectionKey key)
key
- select key from registration of the backing channelprotected abstract M get(ByteBuffer rb)
Implementations are expected not to flip, reset or clear the buffer.
rb
- read byte bufferprotected abstract void put(M message, ByteBuffer wb)
Implementations are expected not to flip, reset or clear the buffer.
message
- message to be placed to the bufferwb
- write byte bufferpublic List<M> dequeue() throws IOException
IOException
- if data could not be readpublic void queue(List<M> messages) throws IOException
messages
- list of messages to be transferedIOException
- if data could not be transfered or flushedpublic void queue(M message) throws IOException
message
- message to be transferedIOException
- if data could not be transfered or flushedpublic void flush() throws IOException
IOException
- if flush failedpublic boolean flushFailed()
getFlushError()
method.public Exception getFlushError()
public IOLoop<M,?> loop()
public SelectionKey key()
protected ByteBuffer growBuffer(ByteBuffer buffer)
growthFactor()
and returns
a new buffer instance with any remaining data copied into it.buffer
- the buffer to growprotected double growthFactor()
protected int maxAge()
public ThroughputTracker inMessages()
public ThroughputTracker inBytes()
public ThroughputTracker outMessages()
public ThroughputTracker outBytes()
Copyright © 2016. All Rights Reserved.