Reputation: 1001
I want to resolve some DNS tasks using the ExecutorService framework. I use java API for the DNS queries: 1/ java.net.InetSocketAddress.InetSocketAddress(String, int) - name lookup 2/ java.net.InetAddress.getByName(String) - name lookup 3/ java.net.InetAddress.getHostName() - reverse name lookup
A typical DNS task will be a Callable object defined like this :
public class IpAddressResolver extends DnsCallable<String, InetAddress> {
public IpAddressResolver(String host) {
super(host);
}
public InetAddress call() throws Exception {
return InetAddress.getByName(target);
}
}
As you can see these 3 calls are all non-interruptible.
The problem is that if Future.get(long, TimeUnit)
timeouts and then I call Future.cancel(boolean)
I do not have any chance to stop the worker thread from trying to resolve DNS tasks (I have tested with timeout:30
and attempts:5
in Linux resolv.conf
file in order to simulate a long-running DNS query).
The outcome of this is that if current running worker thread cannot be stopped, when I submit another DNS task then a second thread will have to be created, for a third task I will have a third thread and so on. If the DNS server is unresponsive, then I would have a bunch of worker threads in the pool that are doing nothing (i.e waiting for the negative DNS response that will come after timeout * attempts = 150 seconds).
I am afraid that if many threads are created and are active in the thread pool (I use a cached thread pool btw), I would have to deal with another kind of issues...
I know that a non-interruptible blocking socket read call can be stopped by closing the underlying socket but this is not the case on my side.
Does anyone know how to deal with these kind of issues?
Upvotes: 1
Views: 626
Reputation: 28951
The DNS lookup call is done by a system call. So, java cannot interrupt it. The only way is to make a thread pool and hope that bad requests will be not so often to overflow the thread pool. Or just tune your resolv.conf
with appropriate timeouts. Nothing you could do in Java.
As alternative, you may look for a pure java dns client implementation which most probably will be interruptible. E.g. fast googling shows this: http://sourceforge.net/p/dnsjava/
Upvotes: 1