Java 6 JAX-WS self hosted services listening on one interface only

JAX-WS included with Java 6 provides a built in HTTP server giving the ability to host web services without running a Java EE server. I've created several sample services to get myself familiar with the functionality and followed http://java.sun.com/developer/technicalArticles/J2SE/jax_ws_2/ guide.

However, there is one issue that I can't get sorted out, and this how to publish a standalone service that listens on all interfaces of the machine, not just one. To explain the problem, when you use the sample publish code of: *Endpoint.publish("http://localhost:8080/test", new ServiceImpl());* the service implemented by ServiceImpl will be published to port 8080 on the loopback interface. This makes the service accessible using "http://localhost:8080/test" and "http://127.0.0.1:8080/test" from the local machine ONLY.

If you have other network interfaces, for example an ethernet connection with DHCP assigned address of 192.168.0.10 you need to publish the service with *Endpoint.publish("http://192.168.0.10:8080/test", new ServiceImpl());* for it to be accessible externally. When published this way, the service does not respond to requests to "http://localhost:8080/test" from the machine it was published on.

Does anyone know how to publish a JAX-WS service so that a wildcard InetSocketAddress is used in the resultant com.sun.net.httpserver.HttpServer ?

Using url of "http://:8080/test" doesn't work and binds to localhost.

Thanks in advance.

[1497 byte] By [kubakabacinskia] at [2007-11-27 9:22:54]
# 1

I've found a pseudo solution...

YourServiceImpl serviceImpl = new YourServiceImpl();

try {

Enumeration ifs = NetworkInterface.getNetworkInterfaces();

while (ifs.hasMoreElements()) {

NetworkInterface ni = (NetworkInterface)ifs.nextElement();

Enumeration ads;

if (ni != null) {

ads = ni.getInetAddresses();

while (ads.hasMoreElements()) {

InetAddress ad = (InetAddress)ads.nextElement();

if (ad != null) {

Endpoint.publish("http://"+ad.getHostAddress()+":8080/yourService",serviceImpl);

}

}

}

}

} catch (Exception e) {

System.out.println("Something went horribly wrong:"+e.getMessage());

}

With one chunk of ugly code you now have your service on all interfaces... fingers crossed for a neater solution.

kubakabacinskia at 2007-7-12 22:17:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...