RzCoDer RzCoDer - 1 year ago 183
Java Question

Netty 4 handle channel events

In Netty 3 I can handle open/close events with ChannelUpstreamHandler, like

pipeline.addLast("channelGroupHandler", new SimpleChannelUpstreamHandler() {
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) {

public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

but in Netty 4 it's doesn't work. So, how I can handle this events now?

Answer Source

If you check New and noteworthy in 4.0 you can see that:

  • In 3.x, When a new connected Channel is created at least three ChannelStateEvents are triggered: channelOpen, channelBound, and channelConnected. When a Channel is closed, at least 3 more: channelDisconnected, channelUnbound, and channelClosed.

  • In 4.x, channelOpen, channelBound, and channelConnected have been merged to channelActive. Otherwise, channelDisconnected, channelUnbound, and channelClosed have been merged to channelInactive.

You should use the new API and that's it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download