Non-blocking (async) DNS resolving in Java

前端 未结 6 595
一生所求
一生所求 2021-02-05 02:30

Is there a clean way to resolve a DNS query (get IP by hostname) in Java asynchronously, in non-blocking way (i.e. state machine, not 1 query = 1 thread - I\'d like to run tens

相关标签:
6条回答
  • 2021-02-05 03:05

    I don't have an answer to your question (I don't know if there is a DNS library that will operate in the async mode that you want) and this is too long for a comment.

    But, you should be able to quickly produce an async one without having to write the full DNS handler yourself. Warning, I haven't done this so I could be all wrong.

    Starting with the dnsjava code you ought to be able to implement your own resolver that will provide you both a sender and receiver method. Check out SimpleResolver and look at the send method. You ought to be able to break up this method into two methods, one to send your request that runs up to the call to either the TCPClient or the UDPClient (you would handle the actual on the wire sending at this point, as you described, with your first thread), and, one to receive, which would be called by your second thread as a response to a socket read, and handle parsing the response. You may have to either copy all of the code from the SimpleResolver (lots of private methods that you'll need and licensing allows for it), or, you could create your own version and simply load it ahead of the jared one in your classpath, or, your could reflect your way to the methods in question and set them accessible.

    You can quickly build the network client side with either netty or mina. I prefer netty for the docs.

    If you do go down this path and can/want to open source it, I can set aside some time to help if you get into trouble.

    0 讨论(0)
  • 2021-02-05 03:05

    Linux has an asynchronous DNS lookup function: http://www.imperialviolet.org/2005/06/01/asynchronous-dns-lookups-with-glibc.html

    If you are on Linux you just need to wrap that up in some JNI.

    0 讨论(0)
  • 2021-02-05 03:15

    You have multiple options

    Option 1: Java 5 Executors

    1. A Fixed thread pool: Executors.newFixedThreadPool(int)
    2. Future: A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.

    Option 2: JMS with MessageListener

    1. Requires dependency on JMS Provider etc.

    Option 2: Actor based framework

    You can scale this well with this.Look at Akka.

    0 讨论(0)
  • 2021-02-05 03:16

    You will, I think, have to implement the DNS client protocol yourself on top of raw UDP using base sockets support, or on top of TCP using NIO channels.

    0 讨论(0)
  • 2021-02-05 03:27

    There is some work on non blocking DNS in netty, but it's still work in progress in will be probably released only in 5.0

    0 讨论(0)
  • 2021-02-05 03:28

    It may be that the Apache Directory Services implementation of DNS on top of MINA is what you're looking for. The JavaDocs and other useful guides are on that page, in the left-hand side-bar.

    0 讨论(0)
提交回复
热议问题