修复mina2客户端IoSession.close()在jdk1.5下关闭不彻底问题

阅读更多
原帖: http://hi.baidu.com/zeorliu/blog/item/4a5fd3bf5fe6470019d81f1d.html
问题:客户端使用Mina2的NioConnector时,发现IoSesssion.close()在jdk1.5下有时不能彻底关闭tcp连接(用tcpdump或者netstat看连接未关闭),而在jdk1.6下则正常。
发现:需要在mina里边调用SocketChannel.close之后,再调用selectNow。 但是jdk6.0不用selectNow就可以。
解决:为了避免修改mina2源代码,需要在IoHandler的sessionClosed()中,强制调用session.getSelectionKey.selector().selectNow()方法来彻底关闭。但是getSelectionKey是package可见,不能直接调用,还需要通过反射技术来间接调用。
try {//注意,getSelectionKey()和仅getChannel()package可见,需要通过反射来强制调用。
	SelectionKey sk = (SelectionKey)MyUtil.invoke(session, "getSelectionKey");
	sk.selector().selectNow();//无效果。
}catch(Exception e) {
	_log.warn("# sessionClosed() : session.getSelectionKey().selector().selectNow() %s", e);
	//_log.error(e, "# sessionClosed()");
}
try {//注意,getSelectionKey()和仅getChannel()package可见,需要通过反射来强制调用。
	SocketChannel sc = (SocketChannel)MyUtil.invoke(session, "getChannel");//sc.close();
	if (sc.isConnected()) {
		sc.socket().shutdownOutput();//有效果。
		sc.socket().close();//无效果??
	}
}catch(Exception e) {
	_log.warn("# sessionClosed() : session.getChannel().socket().shutdownOutput() %s", e);
	//_log.error(e, "# sessionClosed()");
}

参考: http://www.velocityreviews.com/forums/t681805-socketchannel-doesnt-disconnect.html
If I close a SocketChannel like: ((SocketChannel) selectionkey.channel()).close();
How come this returns true when called after the close? Shouldn't it be now disconnected? And how do you force it to disconnect?
((SocketChannel) selectionkey.channel()).isConnected()
Frank Cisco
---
“Peter Duniho wrote: Assuming Java sockets are like most other socket APIs (and I would guess they are), the "isConnected()" method can only tell you the state of the socket as of the most recent i/o operation.”
Exactly so. Ditto isClosed(), isBound(), etc. They tell you which methods *you* have called.
It may or may not be worth noting here that channels that are registered with a Selector don't really close until the next select()/selectNow() operation. This is obscurely described womewhere in the java.nio.channels Javadoc which I can never find when I look for it. The implication for clients that use NIO is that they may need to call Selector.selectNow() to accelerate the actual closing of a channel.
EJP
---
【虎.无名】补充(1)jdk1.5.10以上版本不存在session.close问题。和shenjianlin沟通,目前生产上jdk1.5的都是 1.5.0_12,应该不存在这个问题。
【相关帖子】[url]http://www.blogjava.net/killme2008/archive/2009/09/28/296826.html [/url] java nio的一个严重BUG

你可能感兴趣的:(Socket,Cisco,Mina,HTML,.net)