public class FlushConsolidationHandler extends ChannelDuplexHandler
ChannelDuplexHandler
which consolidates Channel.flush()
/ ChannelHandlerContext.flush()
operations (which also includes
ChannelOutboundInvoker.writeAndFlush(Object)
/ ChannelOutboundInvoker.writeAndFlush(Object, ChannelPromise)
and
ChannelOutboundInvoker.writeAndFlush(Object)
/
ChannelOutboundInvoker.writeAndFlush(Object, ChannelPromise)
).
Flush operations are generally speaking expensive as these may trigger a syscall on the transport level. Thus it is in most cases (where write latency can be traded with throughput) a good idea to try to minimize flush operations as much as possible.
If a read loop is currently ongoing, flush(ChannelHandlerContext)
will not be passed on to the next
ChannelOutboundHandler
in the ChannelPipeline
, as it will pick up any pending flushes when
channelReadComplete(ChannelHandlerContext)
is triggered.
If no read loop is ongoing, the behavior depends on the consolidateWhenNoReadInProgress
constructor argument:
false
, flushes are passed on to the next handler directly;true
, the invocation of the next handler is submitted as a separate task on the event loop. Under
high throughput, this gives the opportunity to process other flushes before the task gets executed, thus
batching multiple flushes into one.explicitFlushAfterFlushes
is reached the flush will be forwarded as well (whether while in a read loop, or
while batching outside of a read loop).
If the Channel
becomes non-writable it will also try to execute any pending flush operations.
The FlushConsolidationHandler
should be put as first ChannelHandler
in the
ChannelPipeline
to have the best effect.
ChannelHandler.Sharable
限定符和类型 | 字段和说明 |
---|---|
static int |
DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES
The default number of flushes after which a flush will be forwarded to downstream handlers (whether while in a
read loop, or while batching outside of a read loop).
|
构造器和说明 |
---|
FlushConsolidationHandler()
Create new instance which explicit flush after 256 pending flush
operations at the latest.
|
FlushConsolidationHandler(int explicitFlushAfterFlushes)
Create new instance which doesn't consolidate flushes when no read is in progress.
|
FlushConsolidationHandler(int explicitFlushAfterFlushes,
boolean consolidateWhenNoReadInProgress)
Create new instance.
|
bind, connect, deregister, read, write
channelActive, channelInactive, channelRegistered, channelUnregistered, userEventTriggered
ensureNotSharable, isSharable
public static final int DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHES
public FlushConsolidationHandler()
public FlushConsolidationHandler(int explicitFlushAfterFlushes)
explicitFlushAfterFlushes
- the number of flushes after which an explicit flush will be done.public FlushConsolidationHandler(int explicitFlushAfterFlushes, boolean consolidateWhenNoReadInProgress)
explicitFlushAfterFlushes
- the number of flushes after which an explicit flush will be done.consolidateWhenNoReadInProgress
- whether to consolidate flushes even when no read loop is currently
ongoing.public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapter
handlerAdded
在接口中 ChannelHandler
handlerAdded
在类中 ChannelHandlerAdapter
java.lang.Exception
public void flush(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelDuplexHandler
ChannelHandlerContext.flush()
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.flush
在接口中 ChannelOutboundHandler
flush
在类中 ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the flush operation is madejava.lang.Exception
- thrown if an error occurspublic void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelReadComplete()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelReadComplete
在接口中 ChannelInboundHandler
channelReadComplete
在类中 ChannelInboundHandlerAdapter
java.lang.Exception
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRead
在接口中 ChannelInboundHandler
channelRead
在类中 ChannelInboundHandlerAdapter
java.lang.Exception
public void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireExceptionCaught(Throwable)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.exceptionCaught
在接口中 ChannelHandler
exceptionCaught
在接口中 ChannelInboundHandler
exceptionCaught
在类中 ChannelInboundHandlerAdapter
java.lang.Exception
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandler
ChannelOutboundInvoker.disconnect(ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.disconnect
在接口中 ChannelOutboundHandler
disconnect
在类中 ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the disconnect operation is madepromise
- the ChannelPromise
to notify once the operation completesjava.lang.Exception
- thrown if an error occurspublic void close(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandler
ChannelOutboundInvoker.close(ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.close
在接口中 ChannelOutboundHandler
close
在类中 ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the close operation is madepromise
- the ChannelPromise
to notify once the operation completesjava.lang.Exception
- thrown if an error occurspublic void channelWritabilityChanged(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelWritabilityChanged()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelWritabilityChanged
在接口中 ChannelInboundHandler
channelWritabilityChanged
在类中 ChannelInboundHandlerAdapter
java.lang.Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapter
handlerRemoved
在接口中 ChannelHandler
handlerRemoved
在类中 ChannelHandlerAdapter
java.lang.Exception