Best way to detect unnoticed disconnection?
IanS
iswett at yahoo.com
Sun Mar 7 14:31:28 EST 2010
I need to be able to detect disconnection, such as when a network connection
gets dropped. I'm using TCP and keepalive, which I thought would detect
this, but it does not appear to.
So I am using an IdleStateHandler along with
channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) to send a ping
message every 15 seconds, and I use the read timer to close the connection
if I haven't received any responses for 30 seconds.
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)
{
if(e.getState()==IdleState.READER_IDLE)
{
// Close the channel.
if(e.getChannel().isConnected())
e.getChannel().close();
}
else if(e.getState()==IdleState.WRITER_IDLE)
{
Message message = Message.newBuilder().setPing(
Protocol.Ping.newBuilder()
.setRequest(true)
.setTimestamp(System.currentTimeMillis())
.build()).build();
e.getChannel().write(message);
}
}
This does work properly, but I want to make sure that I'm going about this
the recommended way. If so, then probably this is a useful reminder to
others to handle unnoticed disconnection.
Thanks, Ian
--
View this message in context: http://n2.nabble.com/Best-way-to-detect-unnoticed-disconnection-tp4691635p4691635.html
Sent from the Netty User Group mailing list archive at Nabble.com.
More information about the netty-users
mailing list