Say “Hola” to mDNS-SD in Java

Service discovery in Java is not as simple as it should be. Apple’s reference implementation, Bonjour, is ironically very difficult to setup on Mac OS X (for JavaFX at least). Alternative pure-Java implementations exist, but I’ve yet to find one that identifies services quickly, offers configurable logging, and is robust enough to tolerate the mDNS responses generated by a 3rd-generation Apple TV (seriously, this kept crashing one of the services I tried). While the system-wide service design provided by Bonjour and Avahi is ideal for reducing network traffic via caching, it also adds an additional software dependency that is outside the control of an app developer.

Enter Hola, a dead-simple, pure-Java implementation of Multicast DNS Service Discovery (mDNS-SD). Zeroconf networking promises incredible ease-of-use for end users, but to encourage adoption that simplicity should extend to developers as well. Thus, Hola has been designed with low requirements and a (hopefully!) intuitive API. Want to find every TiVo on your local network? Here’s the code to do it:

try {
    Service service = Service.fromName("_tivo-mindrpc._tcp");
    Query query = Query.createFor(service, Domain.LOCAL);
    List<Instance> instances = query.runOnce();
    instances.stream().forEach(System.out::println);
} catch (UnknownHostException e) {
    logger.error("Unknown host: ", e);
} catch (IOException e) {
    logger.error("IO error: ", e);
}

Each returned Instance will have a user-visible name, a list of IP addresses, a port number, and a set of attributes:

String userVisibleName = instance.getName();
List<InetAddress> addresses = instance.getAddresses();
int port = instance.getPort();
if (instance.hasAttribute("platform")) {
    String platform = instance.lookupAttribute("platform");
}

And that’s it. To search for different services, just change the String passed to Service.fromName(). Right now only synchronous operation is supported, but I plan to add an asynchronous listener in the next release. As for requirements, Hola depends on nothing beyond Java 8 and SLF4J.

Development is hosted on GitHub at https://github.com/fflewddur/hola and pre-compiled JARs are available. If you’re looking for Java-based service discovery, give Hola a try. As always, comments and bug reports are welcome.

3 thoughts on “Say “Hola” to mDNS-SD in Java

  1. Dear Mr. Kulesza,
    What, if any, professional projects have you completed using the mdnsjava GitHub project? There’s a debate brewing as to whether or not to include DNS-based service discovery in standards for professional media services.
    Thanks!

  2. Hello, thanks for this – it’s a very nice and objective implementation!

    I am trying to find my Python mDNS server with this Java library but I can’t get the service name right! Should I use something like:
    Service service = Service.fromName(“_http._tcp.local.”);

    I am using https://github.com/wmcbrine/pyzeroconf

    Thanks a lot 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *