Package io.netty.handler.flow
Class FlowControlHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.flow.FlowControlHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
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 ChannelHandler
s 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:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ChannelConfig
private static final InternalLogger
private final boolean
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelRead
(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.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 theQueue
.void
Do nothing by default, sub-classes may override this method.void
Do nothing by default, sub-classes may override this method.(package private) boolean
Determine if the underlyingQueue
is empty.void
CallsChannelHandlerContext.read()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, write
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Field Details
-
logger
-
releaseMessages
private final boolean releaseMessages -
queue
-
config
-
shouldConsume
private boolean shouldConsume
-
-
Constructor Details
-
FlowControlHandler
public FlowControlHandler() -
FlowControlHandler
public FlowControlHandler(boolean releaseMessages)
-
-
Method Details
-
isQueueEmpty
boolean isQueueEmpty()Determine if the underlyingQueue
is empty. This method exists for testing, debugging and inspection purposes and it is not Thread safe! -
destroy
private void destroy()Releases all messages and destroys theQueue
. -
handlerAdded
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
read
Description copied from class:ChannelDuplexHandler
CallsChannelHandlerContext.read()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
read
in interfaceChannelOutboundHandler
- Overrides:
read
in classChannelDuplexHandler
- Throws:
Exception
-
channelRead
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelReadComplete
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelReadComplete
in interfaceChannelInboundHandler
- Overrides:
channelReadComplete
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
dequeue
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. TheminConsume
argument is used to forcedequeue()
into consuming that number of messages regardless of the channel's auto reading configuration.- See Also:
-