java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
stincmale.server.netty4.tcp.DispatchMonoHandler<RQ,RS>
- Type Parameters:
RQ
- A type of the inbound message thisChannelInboundHandlerAdapter
expects.RS
- A type of the outbound message thisChannelInboundHandlerAdapter
sends downstream.
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
- Direct Known Subclasses:
HttpDispatchMonoHandler
@ThreadSafe @Sharable public class DispatchMonoHandler<RQ,RS> extends ChannelInboundHandlerAdapter
Processes business logic.
Uses
RequestDispatcher
to process requests.
This handler must be placed in the ChannelPipeline
above (after in the inbound/upstream evaluation order) any decoders,
and above (before in the outbound/downstream evaluation order) any encoders:
ChannelPipeline p = ...;
...
p.addLast(new MyDecoder());
...
p.addLast(new MyEncoder());
...
p.addLast(new DispatchMonoHandler(...));//handles business logic
...
p.addLast(new MyExceptionHandler());
...
and must not be added to the ChannelPipeline
more than once.
This handler adds MonoHandler
in the ChannelPipeline
right below itself in order to guarantee
that at any given moment not more than one request per Channel
is being processed by
RequestDispatcher
.
Note that MonoHandler
disables auto read.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Constructor Summary
Constructors Constructor Description DispatchMonoHandler(RequestDispatcher<RQ,RS> dispatcher, long connectionIdleTimeoutMillis)
-
Method Summary
Modifier and Type Method Description void
channelRead(ChannelHandlerContext ctx, Object msg)
UsesRequestDispatcher
to process the request (msg
) and sends response downstream theChannelPipeline
.void
channelRegistered(ChannelHandlerContext ctx)
AddsIdleStateHandler
(ifgetConnectionIdleTimeoutMillis()
is positive),MonoHandler
to theChannelPipeline
right below this handler and callsChannelInboundHandler.channelRegistered(io.netty.channel.ChannelHandlerContext)
method on these handlers with thectx
.protected boolean
closeChannelAfterResponse(RQ request, RS response, Throwable failure)
Determines ifChannel
will be closed after sending theresponse
, or after processing therequest
if there is noresponse
, or if some uncaughtThrowable
was thrown below in theChannelPipeline
.void
exceptionCaught(ChannelHandlerContext ctx, Throwable failure)
protected RS
failureResponse(RQ request, Throwable failure)
Provides a response to send downstream if processing of therequest
has failed or some uncaughtThrowable
was thrown below in theChannelPipeline
.protected boolean
fireExceptionOnResponseWriteFailure(RQ request, RS response, Throwable sendFailure)
protected long
getConnectionIdleTimeoutMillis()
void
handlerRemoved(ChannelHandlerContext ctx)
void
userEventTriggered(ChannelHandlerContext ctx, Object evt)
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelUnregistered, channelWritabilityChanged
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharable
-
Constructor Details
-
DispatchMonoHandler
- Parameters:
connectionIdleTimeoutMillis
- An interval of inactivity of a TCP connection (no writes and no reads) in milliseconds after which the connection will be closed. Specify negative value to disable such a behavior; specify 0 to close connections immediately after sending a response (this particular behavior may be altered via overriding methodcloseChannelAfterResponse(Object, Object, Throwable)
).
-
-
Method Details
-
channelRegistered
AddsIdleStateHandler
(ifgetConnectionIdleTimeoutMillis()
is positive),MonoHandler
to theChannelPipeline
right below this handler and callsChannelInboundHandler.channelRegistered(io.netty.channel.ChannelHandlerContext)
method on these handlers with thectx
.- Specified by:
channelRegistered
in interfaceChannelInboundHandler
- Overrides:
channelRegistered
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelRead
UsesRequestDispatcher
to process the request (msg
) and sends response downstream theChannelPipeline
. If response isnull
, then writesMonoHandler.VOID_OUTBOUND_MESSAGE
, which results in no response being actually sent. Does not propagatemsg
upstream.Calls ReferenceCounted.release() on the
msg
after completion of processing and sending a response ifmsg
isReferenceCounted
.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
userEventTriggered
- Specified by:
userEventTriggered
in interfaceChannelInboundHandler
- Overrides:
userEventTriggered
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
handlerRemoved
- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
exceptionCaught
- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
failureResponse
Provides a response to send downstream if processing of therequest
has failed or some uncaughtThrowable
was thrown below in theChannelPipeline
. This method is called fromchannelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
,exceptionCaught(io.netty.channel.ChannelHandlerContext, java.lang.Throwable)
if currentChannel
is active.- Parameters:
request
- Provided if available.failure
- AThrowable
that was thrown during processing therequest
,or during sending a response, or during some other operations related to therequest
or the currentChannel
.- Returns:
null
.
-
closeChannelAfterResponse
protected boolean closeChannelAfterResponse(@Nullable RQ request, @Nullable RS response, @Nullable Throwable failure)Determines ifChannel
will be closed after sending theresponse
, or after processing therequest
if there is noresponse
, or if some uncaughtThrowable
was thrown below in theChannelPipeline
.This method is called from
channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
,exceptionCaught(io.netty.channel.ChannelHandlerContext, java.lang.Throwable)
if currentChannel
is active.- Parameters:
request
- Provided if available.response
- Provided if available.failure
- AThrowable
that was thrown during processing therequest
,or during sending theresponse
, or during some other operations related to therequest
or the currentChannel
.- Returns:
true
if connection idle timeout is0
andfailure
isn'tnull
.
-
fireExceptionOnResponseWriteFailure
protected boolean fireExceptionOnResponseWriteFailure(@Nullable RQ request, @Nullable RS response, Throwable sendFailure) -
getConnectionIdleTimeoutMillis
protected final long getConnectionIdleTimeoutMillis()
-