Top Description Fields Constructors Methods
io.netty.handler.flush

public Class FlushConsolidationHandler

extends ChannelDuplexHandler
Class Inheritance
Imports
io.netty.channel.Channel, .ChannelDuplexHandler, .ChannelHandler, .ChannelHandlerContext, .ChannelOutboundHandler, .ChannelOutboundInvoker, .ChannelPipeline, .ChannelPromise, io.netty.util.internal.ObjectUtil, java.util.concurrent.Future

ChannelDuplexHandler which consolidates Channel#flush() / ChannelHandlerContext#flush() operations (which also includes Channel#writeAndFlush(Object) / Channel#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:

If 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.

Field Summary

Modifier and TypeField and Description
private final boolean
private ChannelHandlerContext
public static final 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).

private final int
private int
private final Runnable
private Future<?>
private boolean

Constructor Summary

AccessConstructor and Description
public
FlushConsolidationHandler()

Create new instance which explicit flush after 256 pending flush operations at the latest.

public
FlushConsolidationHandler(int
the number of flushes after which an explicit flush will be done.
explicitFlushAfterFlushes
)

Create new instance which doesn't consolidate flushes when no read is in progress.

public
FlushConsolidationHandler(int
the number of flushes after which an explicit flush will be done.
explicitFlushAfterFlushes
,
boolean
whether to consolidate flushes even when no read loop is currently ongoing.
consolidateWhenNoReadInProgress
)

Create new instance.

Method Summary

Modifier and TypeMethod and Description
private void
public void
public void
public void
public void
close(ChannelHandlerContext
the ChannelHandlerContext for which the close operation is made
ctx
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.close.

Implements io.netty.channel.ChannelOutboundHandler.close.

Calls ChannelHandlerContext#close(ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
public void
public void
public void
private void
private void
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
private void
private void
Inherited from io.netty.channel.ChannelDuplexHandler:
bindconnectderegisterreadwrite

Field Detail

consolidateWhenNoReadInProgressback to summary
private final boolean consolidateWhenNoReadInProgress
ctxback to summary
private ChannelHandlerContext ctx
DEFAULT_EXPLICIT_FLUSH_AFTER_FLUSHESback to summary
public static final 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).

explicitFlushAfterFlushesback to summary
private final int explicitFlushAfterFlushes
flushPendingCountback to summary
private int flushPendingCount
flushTaskback to summary
private final Runnable flushTask
nextScheduledFlushback to summary
private Future<?> nextScheduledFlush
readInProgressback to summary
private boolean readInProgress

Constructor Detail

FlushConsolidationHandlerback to summary
public FlushConsolidationHandler()

Create new instance which explicit flush after 256 pending flush operations at the latest.

FlushConsolidationHandlerback to summary
public FlushConsolidationHandler(int explicitFlushAfterFlushes)

Create new instance which doesn't consolidate flushes when no read is in progress.

Parameters
explicitFlushAfterFlushes:int

the number of flushes after which an explicit flush will be done.

FlushConsolidationHandlerback to summary
public FlushConsolidationHandler(int explicitFlushAfterFlushes, boolean consolidateWhenNoReadInProgress)

Create new instance.

Parameters
explicitFlushAfterFlushes:int

the number of flushes after which an explicit flush will be done.

consolidateWhenNoReadInProgress:boolean

whether to consolidate flushes even when no read loop is currently ongoing.

Method Detail

cancelScheduledFlushback to summary
private void cancelScheduledFlush()
channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelReadCompleteback to summary
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelReadComplete.

Implements io.netty.channel.ChannelInboundHandler.channelReadComplete.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelReadComplete.

Calls ChannelHandlerContext#fireChannelReadComplete() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelWritabilityChangedback to summary
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged.

Implements io.netty.channel.ChannelInboundHandler.channelWritabilityChanged.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged.

Calls ChannelHandlerContext#fireChannelWritabilityChanged() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
closeback to summary
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.close.

Implements io.netty.channel.ChannelOutboundHandler.close.

Doc from io.netty.channel.ChannelDuplexHandler.close.

Calls ChannelHandlerContext#close(ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.close.

the ChannelHandlerContext for which the close operation is made

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.close.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.close.

thrown if an error occurs

disconnectback to summary
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.disconnect.

Implements io.netty.channel.ChannelOutboundHandler.disconnect.

Doc from io.netty.channel.ChannelDuplexHandler.disconnect.

Calls ChannelHandlerContext#disconnect(ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.disconnect.

the ChannelHandlerContext for which the disconnect operation is made

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.disconnect.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.disconnect.

thrown if an error occurs

exceptionCaughtback to summary
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.exceptionCaught.

Implements io.netty.channel.ChannelHandler.exceptionCaught, io.netty.channel.ChannelInboundHandler.exceptionCaught.

Doc from io.netty.channel.ChannelHandler.exceptionCaught.

Gets called if a Throwable was thrown.

Annotations
@Override
flushback to summary
public void flush(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.flush.

Implements io.netty.channel.ChannelOutboundHandler.flush.

Doc from io.netty.channel.ChannelDuplexHandler.flush.

Calls ChannelHandlerContext#flush() to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.flush.

the ChannelHandlerContext for which the flush operation is made

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.flush.

thrown if an error occurs

flushIfNeededback to summary
private void flushIfNeeded(ChannelHandlerContext ctx)
flushNowback to summary
private void flushNow(ChannelHandlerContext ctx)
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
resetReadAndFlushIfNeededback to summary
private void resetReadAndFlushIfNeeded(ChannelHandlerContext ctx)
scheduleFlushback to summary
private void scheduleFlush(final ChannelHandlerContext ctx)