Class BufferedWriteFilter
java.lang.Object
org.apache.mina.core.filterchain.IoFilterAdapter
org.apache.mina.filter.buffer.BufferedWriteFilter
- All Implemented Interfaces:
IoFilter
An
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.- Since:
- MINA 2.0.0-M2
-
Nested Class Summary
Nested classes/interfaces inherited from interface IoFilter
IoFilter.NextFilter -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intThe buffer size allocated for each new session's buffer.private final LazyInitializedCacheMap<IoSession, IoBuffer> static final intDefault buffer size value in bytes.private static final org.slf4j.Logger -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.BufferedWriteFilter(int bufferSize) Constructor which sets buffer size tobufferSize.Uses a default instance ofConcurrentHashMap.BufferedWriteFilter(int bufferSize, LazyInitializedCacheMap<IoSession, IoBuffer> buffersMap) Constructor which sets buffer size tobufferSize. -
Method Summary
Modifier and TypeMethodDescriptionvoidexceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, Throwable cause) FiltersIoHandler.exceptionCaught(IoSession,Throwable)event.voidfilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) FiltersIoSession.write(Object)method invocation.voidFlushes the buffered data.private voidInternal method that actually frees theIoBufferthat contains the buffered data that has not been flushed.intprivate voidinternalFlush(IoFilter.NextFilter nextFilter, IoSession session, IoBuffer buf) Internal method that actually flushes the buffered data.voidsessionClosed(IoFilter.NextFilter nextFilter, IoSession session) FiltersIoHandler.sessionClosed(IoSession)event.voidsetBufferSize(int bufferSize) Sets the buffer size but only for the newly created buffers.private voidWrites anIoBufferto the session's buffer.private voidMethods inherited from class IoFilterAdapter
destroy, event, filterClose, init, inputClosed, messageReceived, messageSent, onPostAdd, onPostRemove, onPreAdd, onPreRemove, sessionCreated, sessionIdle, sessionOpened, toString
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEDefault buffer size value in bytes.- See Also:
-
bufferSize
private int bufferSizeThe buffer size allocated for each new session's buffer. -
buffersMap
-
-
Constructor Details
-
BufferedWriteFilter
public BufferedWriteFilter()Default constructor. Sets buffer size toDEFAULT_BUFFER_SIZEbytes. Uses a default instance ofConcurrentHashMap. -
BufferedWriteFilter
public BufferedWriteFilter(int bufferSize) Constructor which sets buffer size tobufferSize.Uses a default instance ofConcurrentHashMap.- Parameters:
bufferSize- the new buffer size
-
BufferedWriteFilter
Constructor which sets buffer size tobufferSize. IfbuffersMapis null then a default instance ofConcurrentHashMapis created else the provided instance is used.- Parameters:
bufferSize- the new buffer sizebuffersMap- the map to use for storing each session buffer
-
-
Method Details
-
getBufferSize
public int getBufferSize()- Returns:
- The buffer size.
-
setBufferSize
public void setBufferSize(int bufferSize) Sets the buffer size but only for the newly created buffers.- Parameters:
bufferSize- the new buffer size
-
filterWrite
public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception FiltersIoSession.write(Object)method invocation.- Specified by:
filterWritein interfaceIoFilter- Overrides:
filterWritein classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has to process this invocationwriteRequest- TheWriteRequestto process- Throws:
Exception- ifwriteRequest.messageisn't anIoBufferinstance.
-
write
-
write
-
internalFlush
private void internalFlush(IoFilter.NextFilter nextFilter, IoSession session, IoBuffer buf) throws Exception Internal method that actually flushes the buffered data.- Parameters:
nextFilter- theIoFilter.NextFilterof this filtersession- the session where buffer will be writtenbuf- the data to write- Throws:
Exception- if a write operation fails
-
flush
Flushes the buffered data.- Parameters:
session- the session where buffer will be written
-
free
-
exceptionCaught
public void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, Throwable cause) throws Exception FiltersIoHandler.exceptionCaught(IoSession,Throwable)event.- Specified by:
exceptionCaughtin interfaceIoFilter- Overrides:
exceptionCaughtin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventcause- The exception that cause this event to be received- Throws:
Exception- If an error occurred while processing the event
-
sessionClosed
FiltersIoHandler.sessionClosed(IoSession)event.- Specified by:
sessionClosedin interfaceIoFilter- Overrides:
sessionClosedin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this event- Throws:
Exception- If an error occurred while processing the event
-