I’m playing around with a really basic client<->server project for fun, and thought it would be neat to implement a fallback system if the main server wasn’t reachable. I was planning to do this with an SRV record lookup.
For what it’s worth, this is how I’m getting the SRV records..
String query = "_test._tcp.my.domain.com";
Hashtable<String, String> environment = new Hashtable<String, String>();
environment.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
environment.put("java.naming.provider.url", "dns:");
InitialDirContext dirContext = new InitialDirContext(environment);
Attributes records = dirContext.getAttributes(query, new String[] {"SRV"});
So I have a few SRV records set up and all of that, the code retrieves them fine. What I don’t know is, after building a list properly ordering the records, how can I find which record to use?
I know I could just connect to lowest priority record’s host/port, if that fails, move on to the next one, etc. But then what if there was a decent amount of records and only one of the records with highest priority worked? Whoever was using the client would have to wait for each one of the lower priority records connections to time out before finally finding a working one.
What I’m looking to do is find the record with the working host/port seamlessly, in that the user wouldn’t have to wait for possibly age.
You can start connections in separate threads, and just wait until one will complete.
Please consider using http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CompletionService.html
So in short: