2

I am working on upgrading an application using Netty 3 to Netty 4. A lot of the handlers currently have code that look like this:

public class SomeHandler extends SimpleChannelUpstreamHandler {

@Override
public void channelOpen(final ChannelHandlerContext ctx, final ChannelStateEvent e) {
     // do stuff with input
     // ....
     // then call the sendUpstream method
      ctx.sendUpstream(e);
 }

}

I am looking how to convert this to Netty 4. I see that ChannelOutboundHandlerAdapter replaces SimpleChannelUpstreamHandler now, but still couple of questions:

  1. ChannelStateEvent is no longer available in Netty 4, what is the mechanism to receive channel events now?
  2. What is ctx.sendUpstream(e) doing there, and how can that be replicated in Netty 4?
Finlay Weber
  • 2,989
  • 3
  • 17
  • 37

1 Answers1

2

you would override channelActive(...) in this case and call ctx.fireChannelActive() as a replacement for sendUpstream(...)

Norman Maurer
  • 23,104
  • 2
  • 33
  • 31
  • I guess channelActive is the replacement for channelOpen. good. but in channelOpen, ctx.sendUpstream(e); is being called...what will the replacement of that call be, in the overriden channelActive? – Finlay Weber Jan 28 '21 at 10:57
  • ctx.fireChannelActive() or the override super method. – Norman Maurer Jan 28 '21 at 11:05
  • Thanks! I have a feeling this part deals with how events are propagated between handlers in the pipeline. Do you by chance know the section within the documentation that explains this? Or any other guide? Tried googling but can't find any useful information – Finlay Weber Jan 28 '21 at 11:08
  • I think https://netty.io/wiki/new-and-noteworthy-in-4.0.html and the javadocs of ChannelPipeline. – Norman Maurer Jan 28 '21 at 11:30
  • so in a method called `channelActive` I need to call `ctx.fireChannelActive()` ... the first question was, why I am firing channelActive again, in a method that handles channelActive? Won't that lead to a loop? Apparently this calls `channelActive` in the next channel handler! I think the previous API, that has ctx.sendUpstream, makes it more obvious that the next component in the pipeline is being activated. Just a feedback – Finlay Weber Jan 28 '21 at 12:44