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

[b]原帖[/b]:[url]http://hi.baidu.com/zeorliu/blog/item/4a5fd3bf5fe6470019d81f1d.html[/url]
问题:客户端使用Mina2的NioConnector时,发现IoSesssion.close()在jdk1.5下有时不能彻底关闭tcp连接(用tcpdump或者netstat看连接未关闭),而在jdk1.6下则正常。
[b]发现[/b]:需要在mina里边调用SocketChannel.close之后,再调用selectNow。 但是jdk6.0不用selectNow就可以。
[b]解决[/b]:为了避免修改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()");
}

[b]参考[/b]:[url]http://www.velocityreviews.com/forums/t681805-socketchannel-doesnt-disconnect.html[/url]
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.
[color=red][i]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[/i].[/color]
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值