We have used the open source dnsjava library for several applications. For most purposes, the library works well. However, each query is run its own thread, on its own port. This means that for applications with high query volumes, an unacceptable number of threads and ports may be in use.
To solve this problem, we have implemented an extension to dnsjava-2.0.0. The extension adds a new
NonblockingResolver, with the following features :
- Java nio library used
- Queries run in one thread, on one port (where possible). If two concurrent queries have been set by the application to have the same header ID, then they will be run on separate ports.
- Original dnsjava
- A new callback interface (
ResponseQueue) is provided to allow one application thread to handle completion of all queries.
Here is some example code that uses the
ResponseQueue [It is pretty contrived - a simple example like this would do just as well to use the standard
Resolver callback] :
import org.xbill.DNS.* import uk.nominet.dnsjnio.* NonblockingResolver resolver = new NonblockingResolver("localhost"); Message query = getQuery("example.net"); Integer id = new Integer(idCount++); // Not the header ID, but can be! ResponseQueue queue = new ResponseQueue(); resolver.sendAsync(query, id, queue); Response result = queue.remove(); // This call blocks until the queue is not empty assertTrue(result.getId() == id && !result.isException());
The blocking remove call would normally be done in the event loop, and any query completion will complete the call.
This code has been tested on Windows XP and Solaris. The Java nio library contains a native binary for the platform it runs on; although all platforms should run fine, there are not yet any guarantees. I’d be interested to hear if anybody has any success/problems with any other platforms.
The code can be downloaded from here
Note that dnsjava must be on your classpath