'How to catch lost connection event in netty channel handler

I'm working on an old app that is using Netty to connect to a couple of remote TCP endpoints.

The app contains an implementation of IdleStateAwareChannelHandler and overrides several methods provided by it and SimpleChannelHandler (channelConnected, channelIdle, messageReceived, exceptionCaught, channelClosed).

This implementation is not able to cope with the scenario where the application server loses connection towards the remote server while my application is running.

I have hoped that introducing my own custom implementation of channelDisconnected() method would allow me to react to connection loss, but in practice I'm seeing something different:

  • I simulate connection loss by removing my application server from my network, thus cutting it off from both incoming and outgoing traffic
  • I leave it isolated for 5-10 min and observe the logs
  • Then I bring back the application server back to network
  • Only once I have restored the application server to the network I start seeing my debug logs from exceptionCaught and channelDisconnected methods
  • While the machine is isolated, I see from the logs that channelIdle method is being invoked regularly

Question: Is it possible to isolate and react on connection loss in my channel handler ?

Additional Info:

  • Netty version: 3.2.7.Final


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source