Top Description Inners Fields Constructors Methods
io.netty.handler.flow

public Class FlowControlHandler

extends ChannelDuplexHandler
Class Inheritance
Imports
java.util.ArrayDeque, .Queue, io.netty.channel.ChannelConfig, .ChannelDuplexHandler, .ChannelHandler, .ChannelHandlerContext, io.netty.handler.codec.ByteToMessageDecoder, .MessageToByteEncoder, io.netty.util.ReferenceCountUtil, io.netty.util.internal.ObjectPool, .ObjectPool.Handle, .ObjectPool.ObjectCreator, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory

The FlowControlHandler ensures that only one message per read() is sent downstream. Classes such as ByteToMessageDecoder or MessageToByteEncoder are free to emit as many events as they like for any given input. A channel's auto reading configuration doesn't usually apply in these scenarios. This is causing problems in downstream ChannelHandlers that would like to hold subsequent events while they're processing one event. It's a common problem with the HttpObjectDecoder that will very often fire an HttpRequest that is immediately followed by a LastHttpContent event.
ChannelPipeline pipeline = ...;

pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new FlowControlHandler());

pipeline.addLast(new MyExampleHandler());

class MyExampleHandler extends ChannelInboundHandlerAdapter {
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
      ctx.channel().config().setAutoRead(false);

      // The FlowControlHandler will hold any subsequent events that
      // were emitted by HttpObjectDecoder until auto reading is turned
      // back on or Channel#read() is being called.
    }
  }
}
See Also
ChannelConfig#setAutoRead(boolean)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private ChannelConfig
private static final InternalLogger
private FlowControlHandler.RecyclableArrayDeque
private final boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
public
FlowControlHandler(boolean releaseMessages)

Method Summary

Modifier and TypeMethod and Description
public void
public void
public void
private int
dequeue(ChannelHandlerContext ctx, int minConsume)

Dequeues one or many (or none) messages depending on the channel's auto reading state and returns the number of messages that were consumed from the internal queue.

private void
destroy()

Releases all messages and destroys the Queue.

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.
pack-priv boolean
isQueueEmpty()

Determine if the underlying Queue is empty.

public void
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflushwrite

Field Detail

configback to summary
private ChannelConfig config
loggerback to summary
private static final InternalLogger logger
queueback to summary
private FlowControlHandler.RecyclableArrayDeque queue
releaseMessagesback to summary
private final boolean releaseMessages
shouldConsumeback to summary
private boolean shouldConsume

Constructor Detail

FlowControlHandlerback to summary
public FlowControlHandler()
FlowControlHandlerback to summary
public FlowControlHandler(boolean releaseMessages)

Method Detail

channelInactiveback to summary
public void channelInactive(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Implements io.netty.channel.ChannelInboundHandler.channelInactive.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

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

Annotations
@Override
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
dequeueback to summary
private int dequeue(ChannelHandlerContext ctx, int minConsume)

Dequeues one or many (or none) messages depending on the channel's auto reading state and returns the number of messages that were consumed from the internal queue. The minConsume argument is used to force dequeue() into consuming that number of messages regardless of the channel's auto reading configuration.

See Also
read(ChannelHandlerContext), channelRead(ChannelHandlerContext, Object)
destroyback to summary
private void destroy()

Releases all messages and destroys the Queue.

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
isQueueEmptyback to summary
pack-priv boolean isQueueEmpty()

Determine if the underlying Queue is empty. This method exists for testing, debugging and inspection purposes and it is not Thread safe!

readback to summary
public void read(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.read.

Implements io.netty.channel.ChannelOutboundHandler.read.

Doc from io.netty.channel.ChannelDuplexHandler.read.

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

Annotations
@Override
io.netty.handler.flow back to summary

private final Class FlowControlHandler.RecyclableArrayDeque

extends ArrayDeque<Object>
Class Inheritance

A recyclable ArrayDeque.

Field Summary

Modifier and TypeField and Description
private static final int
DEFAULT_NUM_ELEMENTS

A value of 2 should be a good choice for most scenarios.

private final ObjectPool.Handle<FlowControlHandler.RecyclableArrayDeque>
private static final ObjectPool<FlowControlHandler.RecyclableArrayDeque>
private static final long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static FlowControlHandler.RecyclableArrayDeque
public void
Inherited from java.util.ArrayDeque:
addaddAlladdFirstaddLastclearclonecontainsdescendingIteratorelementforEachgetFirstgetLastisEmptyiteratorofferofferFirstofferLastpeekpeekFirstpeekLastpollpollFirstpollLastpoppushremoveremoveremoveAllremoveFirstremoveFirstOccurrenceremoveIfremoveLastremoveLastOccurrenceretainAllsizespliteratortoArraytoArray

Field Detail

DEFAULT_NUM_ELEMENTSback to summary
private static final int DEFAULT_NUM_ELEMENTS

A value of 2 should be a good choice for most scenarios.

handleback to summary
private final ObjectPool.Handle<FlowControlHandler.RecyclableArrayDeque> handle
RECYCLERback to summary
private static final ObjectPool<FlowControlHandler.RecyclableArrayDeque> RECYCLER
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.ArrayDeque.serialVersionUID.

Constructor Detail

RecyclableArrayDequeback to summary
private RecyclableArrayDeque(int numElements, ObjectPool.Handle<FlowControlHandler.RecyclableArrayDeque> handle)

Method Detail

newInstanceback to summary
public static FlowControlHandler.RecyclableArrayDeque newInstance()
recycleback to summary
public void recycle()