Class SimpleChannelDuplexHandler<I,​O>

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, io.netty.channel.ChannelOutboundHandler
    Direct Known Subclasses:
    SignatureHandler, ThreeWayHandshakeClientHandler, ThreeWayHandshakeServerHandler

    public abstract class SimpleChannelDuplexHandler<I,​O>
    extends io.netty.channel.SimpleChannelInboundHandler<I>
    implements io.netty.channel.ChannelOutboundHandler
    ChannelDuplexHandler which allows to explicit only handle a specific type of messages.

    For example here is an implementation which only handle String messages.

         public class StringHandler extends
                 SimpleChannelDuplexHandler<String, String> {
    
             @Override
             protected void channelRead0(ChannelHandlerContext ctx, String message)
                     throws Exception {
                 System.out.println(message);
             }
    
             @Override
             protected void channelWrite0(ChannelHandlerContext ctx, String message)
                       throws Exception {
                   System.out.println(message);
             }
         }
     

    Be aware that depending of the constructor parameters it will release all handled messages by passing them to ReferenceCountUtil.release(Object) and fulfills the corresponding ChannelPromise in the channelWrite0(io.netty.channel.ChannelHandlerContext, O, io.netty.channel.ChannelPromise) method. In this case you may need to use ReferenceCountUtil.retain(Object) if you pass the object to the next handler in the ChannelPipeline.

    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

        io.netty.channel.ChannelHandler.Sharable
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      boolean acceptOutboundMessage​(Object msg)
      Returns true if the given message should be handled.
      void bind​(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)  
      protected abstract void channelRead0​(io.netty.channel.ChannelHandlerContext ctx, I msg)
      Is called for each message of type SimpleChannelDuplexHandler on inbound channel.
      protected abstract void channelWrite0​(io.netty.channel.ChannelHandlerContext ctx, O msg, io.netty.channel.ChannelPromise promise)
      Is called for each message of type SimpleChannelDuplexHandler on the outbound channel.
      void close​(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)  
      void connect​(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)  
      void deregister​(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)  
      void disconnect​(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)  
      void flush​(io.netty.channel.ChannelHandlerContext ctx)  
      void read​(io.netty.channel.ChannelHandlerContext ctx)  
      void write​(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)  
      • Methods inherited from class io.netty.channel.SimpleChannelInboundHandler

        acceptInboundMessage, channelRead
      • Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

        channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
      • Methods inherited from class io.netty.channel.ChannelHandlerAdapter

        ensureNotSharable, handlerAdded, handlerRemoved, isSharable
      • Methods inherited from interface io.netty.channel.ChannelHandler

        handlerAdded, handlerRemoved
    • Constructor Detail

      • SimpleChannelDuplexHandler

        protected SimpleChannelDuplexHandler​(boolean inboundAutoRelease,
                                             boolean outboundAutoRelease,
                                             boolean autoFulfillPromise)
        Create a new instance which will try to detect the types to match out of the types parameter of the class.
        Parameters:
        inboundAutoRelease - true if inbound handled messages should be released automatically by passing them to ReferenceCountUtil.release(Object).
        outboundAutoRelease - true if outbound handled messages should be released automatically by passing them to ReferenceCountUtil.release(Object).
        autoFulfillPromise - true if outbound ChannelPromise should be fulfilled automatically
      • SimpleChannelDuplexHandler

        protected SimpleChannelDuplexHandler​(Class<? extends I> inboundMessageType,
                                             Class<? extends O> outboundMessageType,
                                             boolean inboundAutoRelease,
                                             boolean outboundAutoRelease,
                                             boolean autoFulfillPromise)
        Create a new instance
        Parameters:
        inboundMessageType - The type of messages to match
        outboundMessageType - The type of messages to match
        inboundAutoRelease - true if inbound handled messages should be released automatically by passing them to ReferenceCountUtil.release(Object).
        outboundAutoRelease - true if outbound handled messages should be released automatically by passing them to ReferenceCountUtil.release(Object).
        autoFulfillPromise - true if outbound ChannelPromise should be fulfilled automatically
    • Method Detail

      • channelRead0

        protected abstract void channelRead0​(io.netty.channel.ChannelHandlerContext ctx,
                                             I msg)
                                      throws Exception
        Is called for each message of type SimpleChannelDuplexHandler on inbound channel.
        Specified by:
        channelRead0 in class io.netty.channel.SimpleChannelInboundHandler<I>
        Parameters:
        ctx - the ChannelHandlerContext which this SimpleChannelDuplexHandler belongs to
        msg - the message to handle
        Throws:
        Exception - is thrown if an error occurred
      • bind

        public void bind​(io.netty.channel.ChannelHandlerContext ctx,
                         SocketAddress localAddress,
                         io.netty.channel.ChannelPromise promise)
        Specified by:
        bind in interface io.netty.channel.ChannelOutboundHandler
      • connect

        public void connect​(io.netty.channel.ChannelHandlerContext ctx,
                            SocketAddress remoteAddress,
                            SocketAddress localAddress,
                            io.netty.channel.ChannelPromise promise)
        Specified by:
        connect in interface io.netty.channel.ChannelOutboundHandler
      • disconnect

        public void disconnect​(io.netty.channel.ChannelHandlerContext ctx,
                               io.netty.channel.ChannelPromise promise)
        Specified by:
        disconnect in interface io.netty.channel.ChannelOutboundHandler
      • close

        public void close​(io.netty.channel.ChannelHandlerContext ctx,
                          io.netty.channel.ChannelPromise promise)
                   throws Exception
        Specified by:
        close in interface io.netty.channel.ChannelOutboundHandler
        Throws:
        Exception
      • deregister

        public void deregister​(io.netty.channel.ChannelHandlerContext ctx,
                               io.netty.channel.ChannelPromise promise)
        Specified by:
        deregister in interface io.netty.channel.ChannelOutboundHandler
      • read

        public void read​(io.netty.channel.ChannelHandlerContext ctx)
        Specified by:
        read in interface io.netty.channel.ChannelOutboundHandler
      • write

        public void write​(io.netty.channel.ChannelHandlerContext ctx,
                          Object msg,
                          io.netty.channel.ChannelPromise promise)
                   throws Exception
        Specified by:
        write in interface io.netty.channel.ChannelOutboundHandler
        Throws:
        Exception
      • acceptOutboundMessage

        public boolean acceptOutboundMessage​(Object msg)
        Returns true if the given message should be handled. If false it will be passed to the next ChannelOutboundHandler in the ChannelPipeline.
      • channelWrite0

        protected abstract void channelWrite0​(io.netty.channel.ChannelHandlerContext ctx,
                                              O msg,
                                              io.netty.channel.ChannelPromise promise)
                                       throws Exception
        Is called for each message of type SimpleChannelDuplexHandler on the outbound channel.
        Parameters:
        ctx - the ChannelHandlerContext which this SimpleChannelDuplexHandler belongs to
        msg - the message to handle
        promise - the corresponding promise
        Throws:
        Exception - is thrown if an error occurred
      • flush

        public void flush​(io.netty.channel.ChannelHandlerContext ctx)
        Specified by:
        flush in interface io.netty.channel.ChannelOutboundHandler