简体   繁体   English

Java:SelectionKey.attach()是否损坏?

[英]Java: Is SelectionKey.attach() broken?

In my implementation of Java NIO I have not been able to get SelectionKey.attach() to work. 在Java NIO的实现中,我无法使SelectionKey.attach()正常工作。 Basically, once clients connect (OP_ACCEPT interest ops) I add them to a map where their IP address maps to an object that maintains state for the client. 基本上,一旦客户端连接(OP_ACCEPT兴趣操作),我便将其添加到映射中,其中其IP地址映射到维护客户端状态的对象。 Then, when an OP_READ occurs, I again retrieve the client's IP address, and this time get the value from the map and get the client's state object that way. 然后,当发生OP_READ时,我再次获取客户端的IP地址,这一次从映射中获取值并以这种方式获取客户端的状态对象。

The problem here is that I have to do a map lookup EVERY TIME data is read from the network. 这里的问题是,每次从网络读取数据时,我都必须进行地图查找。 There is a lot of wasted work going on there. 那里有很多浪费的工作。 So, I was thrilled to see that you can attach an arbitrary object type to a SelectionKey, which should be easily retrieved when you call SelectionKey.attachment(), even if we are now handling a different event (retrieved during OP_READ versus put into the map during OP_ACCEPT). 因此,令我激动的是,您可以将任意对象类型附加到SelectionKey上,即使我们现在正在处理其他事件(在OP_READ期间进行检索,而放入OPTIONS中,则可以很容易地在调用SelectionKey.attachment()时检索该对象类型)。在OP_ACCEPT期间映射)。

The problem is that it doesn't work. 问题是它不起作用。 When I retrieve the attachment, it is always null. 当我检索附件时,它始终为null。 And if I set up the attachment via attach() and then immediately call attachment(), it does work. 而且,如果我通过attach()设置附件,然后立即调用attachment(),它确实可以工作。 Somehow between different events, it loses its association. 不同事件之间以某种方式失去了联系。

I'm sorry, my code is a bit long to post here, but if you look at the comments in this thread: link text ...You will see that some others have basically come to the same conclusion: that attach() and attachment() don't work, and never have. 抱歉,我的代码要发布到这里有点长,但是如果您查看此线程中的注释: 链接文本 ...您将看到其他一些基本得出相同的结论:attach()和attachment()不起作用,也永远不起作用。

Is there a trick to get it to work, or am I stuck with the evil overhead necessary of manually doing a lookup in a map EVERY TIME there is a new read event to handle? 是否有使它正常工作的技巧,还是我每次都需要在地图上手动进行查找而烦恼的额外开销?

Finally, is there a way to "wrap" SelectionKey in a new subclass that will properly handle attach() and attachment()? 最后,有没有一种方法可以在新的子类中“包装” SelectionKey,该子类将正确处理attach()和attachment()?

Thanks! 谢谢!

I have successfully attached objects to SelectionKeys without any problems, but only from the same event. 我已经成功地将对象附加到SelectionKeys上,没有任何问题,但仅来自同一事件。 For example the first time I get an OP_READ , I attach an object to the SelectionKey and on subsequent reads I retrieve it, and it works fine. 例如,第一次获取OP_READ ,我将一个对象附加到SelectionKey上,随后读取时将其检索到,并且可以正常工作。 Maybe OP_ACCEPT and OP_READ handle different keys because they're different events, but I think the SelectionKey should be the same for the same connection... however if you make different connections even from the same IP you will get different SelectionKeys. 也许OP_ACCEPTOP_READ处理不同的键是因为它们是不同的事件,但是我认为对于相同的连接来说SelectionKey应该是相同的...但是,即使您从相同的IP进行不同的连接,您也会得到不同的SelectionKeys。

UPDATE: I just checked my code and there is something very important: I didn't call attach() at all; 更新:我只是检查了我的代码,有一个非常重要的事情:我根本没有调用attach(); I used the SelectableChannel.register(Selector sel, int ops, Object att) method. 我使用了SelectableChannel.register(Selector sel, int ops, Object att)方法。 If you do that, subsequent calls to attachment() on the SelectionKey will work. 如果您这样做,则随后对SelectionKey的attachment()的调用将起作用。

There is no CR in your linked page, and I can't see anything on bugs.sun.com . 您链接的页面中没有CR,并且在bugs.sun.com上看不到任何内容 However, attach / attachment is very simple code (although slightly more complicated after my bug . My guess is that you are getting a different SelectionKey , so trying to wrap it would be pointless. 但是, attach / attachment是非常简单的代码(尽管在我的bug之后稍微复杂一些。我的猜测是您将获得一个不同的SelectionKey ,因此尝试包装它将毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM