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
public class FlowControlHandler extends ChannelDuplexHandler
TheFlowControlHandler
ensures that only one message perread()
is sent downstream. Classes such asByteToMessageDecoder
orMessageToByteEncoder
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 downstreamChannelHandler
s that would like to hold subsequent events while they're processing one event. It's a common problem with theHttpObjectDecoder
that will very often fire anHttpRequest
that is immediately followed by aLastHttpContent
event.{@code ChannelPipeline pipeline = ...; pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new FlowControlHandler()); pipeline.addLast(new MyExampleHandler()); class MyExampleHandler extends ChannelInboundHandlerAdapter {
- See Also:
ChannelConfig.setAutoRead(boolean)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
FlowControlHandler.RecyclableArrayDeque
A recyclableArrayDeque
.-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private ChannelConfig
config
private static InternalLogger
logger
private FlowControlHandler.RecyclableArrayDeque
queue
private int
readRequestCount
private boolean
releaseMessages
-
Constructor Summary
Constructors Constructor Description FlowControlHandler()
FlowControlHandler(boolean releaseMessages)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelInactive(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelRead(ChannelHandlerContext ctx, java.lang.Object msg)
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelReadComplete(ChannelHandlerContext ctx)
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
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.void
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.(package private) boolean
isQueueEmpty()
Determine if the underlyingQueue
is empty.void
read(ChannelHandlerContext ctx)
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 Detail
-
logger
private static final InternalLogger logger
-
releaseMessages
private final boolean releaseMessages
-
queue
private FlowControlHandler.RecyclableArrayDeque queue
-
config
private ChannelConfig config
-
readRequestCount
private int readRequestCount
-
-
Method Detail
-
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
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
read
public void read(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
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:
java.lang.Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
dequeue
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. TheminConsume
argument is used to forcedequeue()
into consuming that number of messages regardless of the channel's auto reading configuration.
-
-