Best way to detect unnoticed disconnection?

"Trustin Lee (이희승)" trustin at gmail.com
Mon Mar 8 06:08:07 EST 2010


Hi Ian,

IanS wrote:
> 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.

Right.

> 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.

Yes, it's the right way.  Very well done.

Cheers,
Trustin

-- 
what we call human nature in actuality is human habit
http://gleamynode.net/


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
Url : http://lists.jboss.org/pipermail/netty-users/attachments/20100308/088d6f0d/attachment.bin 


More information about the netty-users mailing list