public final class BufferedWriteFilter extends IoFilterAdapter
IoFilter implementation used to buffer outgoing WriteRequest almost
like what BufferedOutputStream does. Using this filter allows to be less dependent
from network latency. It is also useful when a session is generating very small messages
too frequently and consequently generating unnecessary traffic overhead.
Please note that it should always be placed before the ProtocolCodecFilter
as it only handles WriteRequest's carrying IoBuffer objects.IoFilter.NextFilter| Modifier and Type | Field and Description |
|---|---|
private int |
bufferSize
The buffer size allocated for each new session's buffer.
|
private LazyInitializedCacheMap<IoSession,IoBuffer> |
buffersMap
|
static int |
DEFAULT_BUFFER_SIZE
Default buffer size value in bytes.
|
private org.slf4j.Logger |
logger |
| Constructor and Description |
|---|
BufferedWriteFilter()
Default constructor.
|
BufferedWriteFilter(int bufferSize)
Constructor which sets buffer size to
bufferSize.Uses a default
instance of ConcurrentHashMap. |
BufferedWriteFilter(int bufferSize,
LazyInitializedCacheMap<IoSession,IoBuffer> buffersMap)
Constructor which sets buffer size to
bufferSize. |
| Modifier and Type | Method and Description |
|---|---|
void |
exceptionCaught(IoFilter.NextFilter nextFilter,
IoSession session,
java.lang.Throwable cause)
Filters
IoHandler.exceptionCaught(IoSession,Throwable) event. |
void |
filterWrite(IoFilter.NextFilter nextFilter,
IoSession session,
WriteRequest writeRequest)
Filters
IoSession.write(Object) method invocation. |
void |
flush(IoSession session)
Flushes the buffered data.
|
private void |
free(IoSession session)
Internal method that actually frees the
IoBuffer that contains
the buffered data that has not been flushed. |
int |
getBufferSize()
Returns buffer size.
|
private void |
internalFlush(IoFilter.NextFilter nextFilter,
IoSession session,
IoBuffer buf)
Internal method that actually flushes the buffered data.
|
void |
sessionClosed(IoFilter.NextFilter nextFilter,
IoSession session)
Filters
IoHandler.sessionClosed(IoSession) event. |
void |
setBufferSize(int bufferSize)
Sets the buffer size but only for the newly created buffers.
|
private void |
write(IoSession session,
IoBuffer data)
Writes an
IoBuffer to the session's buffer. |
private void |
write(IoSession session,
IoBuffer data,
IoBuffer buf)
|
destroy, filterClose, init, inputClosed, messageReceived, messageSent, onPostAdd, onPostRemove, onPreAdd, onPreRemove, sessionCreated, sessionIdle, sessionOpened, toStringprivate final org.slf4j.Logger logger
public static final int DEFAULT_BUFFER_SIZE
private int bufferSize
private final LazyInitializedCacheMap<IoSession,IoBuffer> buffersMap
public BufferedWriteFilter()
DEFAULT_BUFFER_SIZE
bytes. Uses a default instance of ConcurrentHashMap.public BufferedWriteFilter(int bufferSize)
bufferSize.Uses a default
instance of ConcurrentHashMap.bufferSize - the new buffer sizepublic BufferedWriteFilter(int bufferSize,
LazyInitializedCacheMap<IoSession,IoBuffer> buffersMap)
bufferSize. If
buffersMap is null then a default instance of ConcurrentHashMap
is created else the provided instance is used.bufferSize - the new buffer sizebuffersMap - the map to use for storing each session bufferpublic int getBufferSize()
public void setBufferSize(int bufferSize)
bufferSize - the new buffer sizepublic void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws java.lang.Exception
IoSession.write(Object) method invocation.filterWrite in interface IoFilterfilterWrite in class IoFilterAdapternextFilter - the IoFilter.NextFilter for this filter. You can reuse this
object until this filter is removed from the chain.session - The IoSession which has to process this invocationwriteRequest - The WriteRequest to processjava.lang.Exception - if writeRequest.message isn't an
IoBuffer instance.private void write(IoSession session, IoBuffer data)
IoBuffer to the session's buffer.session - the session to which a write is requesteddata - the data to bufferprivate void write(IoSession session, IoBuffer data, IoBuffer buf)
data IoBuffer to the buf
IoBuffer which buffers write requests for the
session until buffer is full
or manually flushed.session - the session where buffer will be writtendata - the data to bufferbuf - the buffer where data will be temporarily writtenprivate void internalFlush(IoFilter.NextFilter nextFilter, IoSession session, IoBuffer buf) throws java.lang.Exception
nextFilter - the NextFilter of this filtersession - the session where buffer will be writtenbuf - the data to writejava.lang.Exception - if a write operation failspublic void flush(IoSession session)
session - the session where buffer will be writtenprivate void free(IoSession session)
IoBuffer that contains
the buffered data that has not been flushed.session - the session we operate onpublic void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Throwable cause) throws java.lang.Exception
IoHandler.exceptionCaught(IoSession,Throwable) event.exceptionCaught in interface IoFilterexceptionCaught in class IoFilterAdapternextFilter - the IoFilter.NextFilter for this filter. You can reuse this
object until this filter is removed from the chain.session - The IoSession which has received this eventcause - The exception that cause this event to be receivedjava.lang.Exceptionpublic void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session) throws java.lang.Exception
IoHandler.sessionClosed(IoSession) event.sessionClosed in interface IoFiltersessionClosed in class IoFilterAdapternextFilter - the IoFilter.NextFilter for this filter. You can reuse this
object until this filter is removed from the chain.session - The IoSession which has received this eventjava.lang.Exception