Specify connection timeout in java.nio

后端 未结 2 1194
不知归路
不知归路 2020-12-18 03:24

Using non-blocking I/O, the code for connecting to a remote address looks something like:

SocketChannel channel = SelectorProvider.provider().openSocketChann         


        
2条回答
  •  庸人自扰
    2020-12-18 03:55

    Interesting question. I'm not sure that nio itself provides a solution.

    In my experience, I've run a Callable to attempt the connection, and then used the Future to poll for a response, using 'interval' and 'timeout' variables to loop and Thread.sleep() for a response.

    Hopefully that points you in a useful direction ...

    Also, I suggest you take a look at Apache Mina (you could describe Mina as an nio framework). It handles a lot of this kind of stuff for you, for example in the StreamIoHandler http://mina.apache.org/report/trunk/apidocs/org/apache/mina/handler/stream/StreamIoHandler.html

提交回复
热议问题