Skip to content

Instantly share code, notes, and snippets.

@fbricon
Last active September 11, 2024 08:58
Show Gist options
  • Save fbricon/cf25b6670659216447e17712d5acd7f6 to your computer and use it in GitHub Desktop.
Save fbricon/cf25b6670659216447e17712d5acd7f6 to your computer and use it in GitHub Desktop.
Automatically register a Quarkus app with a custom hostname.local via mDNS
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.quarkus.platform:quarkus-bom:3.14.2@pom
//DEPS io.quarkus:quarkus-rest
//DEPS org.jmdns:jmdns:3.5.12
//COMPILE_OPTIONS -parameters
//RUNTIME_OPTIONS -Djava.util.logging.manager=org.jboss.logmanager.LogManager -Dmdns.host=awesome
import java.io.IOException;
import java.net.InetAddress;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/hello")
@ApplicationScoped
public class qmdns {
private static final Logger LOGGER = Logger.getLogger(qmdns.class);
@GET
public String sayHello() {
return "hello there!";
}
public static void main(String[] args) {
Quarkus.run(args);
}
@Inject
GreetingService service;
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/greeting/{name}")
public String greeting(String name) {
return service.greeting(name);
}
@ApplicationScoped
static public class GreetingService {
public String greeting(String name) {
return "Salut " + name + "!";
}
}
/**
* Automatically register this application via mDNS for easy discovery, e.g.
* ping
*/
@ApplicationScoped
static public class AutoDNS {
private JmDNS jmdns;
@ConfigProperty(name = "quarkus.http.port")
int quarkusPort;
@ConfigProperty(name = "mdns.host")
String mdnsHost;
public void onStart(@Observes StartupEvent startup) {
//TODO check active profile to restrict mDNS registration to dev mode
var hostName = mdnsHost+".local";
try {
jmdns = JmDNS.create(InetAddress.getLocalHost(), mdnsHost);
// Register a service
LOGGER.infof("Registering mDNS service %s",hostName);
ServiceInfo serviceInfo = ServiceInfo.create("_http._tcp.local.", mdnsHost, quarkusPort, 0, 0, mdnsHost);
jmdns.registerService(serviceInfo);
LOGGER.infof("The application is available from http://%s:%d/", hostName, quarkusPort);
} catch (IOException e) {
LOGGER.errorf(e, "Failed to register mDNS service %s", hostName);
}
}
public void onStop(@Observes ShutdownEvent shutdown) {
LOGGER.info("The application is stopping...");
if (jmdns != null) {
LOGGER.info("Unregistering mDNS services...");
jmdns.unregisterAllServices();
jmdns = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment