Package org.apache.mina.filter.buffer
Class BufferedWriteFilter
- java.lang.Object
-
- org.apache.mina.core.filterchain.IoFilterAdapter
-
- org.apache.mina.filter.buffer.BufferedWriteFilter
-
- All Implemented Interfaces:
IoFilter
public final class BufferedWriteFilter extends IoFilterAdapter
AnIoFilterimplementation used to buffer outgoingWriteRequestalmost like whatBufferedOutputStreamdoes. 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 theProtocolCodecFilteras it only handlesWriteRequest's carryingIoBufferobjects.- Since:
- MINA 2.0.0-M2
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.mina.core.filterchain.IoFilter
IoFilter.NextFilter
-
-
Field Summary
Fields Modifier and Type Field Description private intbufferSizeThe buffer size allocated for each new session's buffer.private LazyInitializedCacheMap<IoSession,IoBuffer>buffersMapstatic intDEFAULT_BUFFER_SIZEDefault buffer size value in bytes.private static org.slf4j.LoggerLOGGER
-
Constructor Summary
Constructors Constructor Description BufferedWriteFilter()Default 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Throwable cause)FiltersIoHandler.exceptionCaught(IoSession,Throwable)event.voidfilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)FiltersIoSession.write(Object)method invocation.voidflush(IoSession session)Flushes the buffered data.private voidfree(IoSession session)Internal method that actually frees theIoBufferthat contains the buffered data that has not been flushed.intgetBufferSize()private 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 voidwrite(IoSession session, IoBuffer data)Writes anIoBufferto the session's buffer.private voidwrite(IoSession session, IoBuffer data, IoBuffer buf)-
Methods inherited from class org.apache.mina.core.filterchain.IoFilterAdapter
destroy, event, filterClose, init, inputClosed, messageReceived, messageSent, onPostAdd, onPostRemove, onPreAdd, onPreRemove, sessionCreated, sessionIdle, sessionOpened, toString
-
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
Default buffer size value in bytes.- See Also:
- Constant Field Values
-
bufferSize
private int bufferSize
The buffer size allocated for each new session's buffer.
-
buffersMap
private final LazyInitializedCacheMap<IoSession,IoBuffer> buffersMap
-
-
Constructor Detail
-
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
public BufferedWriteFilter(int bufferSize, LazyInitializedCacheMap<IoSession,IoBuffer> buffersMap)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 Detail
-
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 java.lang.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:
java.lang.Exception- ifwriteRequest.messageisn't anIoBufferinstance.
-
write
private void write(IoSession session, IoBuffer data)
Writes anIoBufferto the session's buffer.- Parameters:
session- the session to which a write is requesteddata- the data to buffer
-
write
private void write(IoSession session, IoBuffer data, IoBuffer buf)
WritesdataIoBufferto thebufIoBufferwhich buffers write requests for thesessionIoSessionuntil buffer is full or manually flushed.- Parameters:
session- the session where buffer will be writtendata- the data to bufferbuf- the buffer where data will be temporarily written
-
internalFlush
private void internalFlush(IoFilter.NextFilter nextFilter, IoSession session, IoBuffer buf) throws java.lang.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:
java.lang.Exception- if a write operation fails
-
flush
public void flush(IoSession session)
Flushes the buffered data.- Parameters:
session- the session where buffer will be written
-
free
private void free(IoSession session)
Internal method that actually frees theIoBufferthat contains the buffered data that has not been flushed.- Parameters:
session- the session we operate on
-
exceptionCaught
public void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Throwable cause) throws java.lang.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:
java.lang.Exception- If an error occurred while processing the event
-
sessionClosed
public void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session) throws java.lang.Exception
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:
java.lang.Exception- If an error occurred while processing the event
-
-