六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 198|回复: 0

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

[复制链接]

升级  14.67%

14

主题

14

主题

14

主题

秀才

Rank: 2

积分
72
 楼主| 发表于 2013-2-7 20:57:58 | 显示全部楼层 |阅读模式
原帖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,应该不存在这个问题。
【相关帖子】http://www.blogjava.net/killme2008/archive/2009/09/28/296826.html java nio的一个严重BUG
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表