I wrote a small JAX-WS web service that I run outside of the container with Endpoint.publish():
Endpoint endpoint = Endpoint.create(new MyServiceImpl());
endpoint.publish("http://localhost:4425/myService");
If any of my web service methods throws an exception, the endpoint will not be gracefully closed and the address will remain valid until Windows eventually releases it. This causes a classic error:
com.sun.xml.internal.ws.server.ServerRtException: Runtime server runtime error: java.net.BindException: address already in use: bind
I could wrap all my web service methods with try / catch, but this seems a bit repetitive. I also tried to set the cleanup class using Thread.setDefaultUncaughtExceptionHandler(), but it was not called when my web service method made an exception.
Is there a more elegant way to solve this issue than resorting to countless try / catch blocks?
Based on Waldheinz's answer, I tried using Jetty classes in favor of default JDK settings. The code compiles, but when it is executed, it ends immediately after publish. When using JDK classes, the main thread will remain alive until I finish the process manually. Any ideas on what's going on? I wonder if some kind of exception happens somewhere, but is swallowed, so I don't see it.
Endpoint endpoint = Endpoint.create(new MyServiceImpl());
Server s = new Server(new InetSocketAddress(HOST, PORT));
ServerConnector connector = new ServerConnector(s);
connector.setReuseAddress(true);
s.setConnectors(new Connector[] { connector });
s.setHandler(new ContextHandlerCollection());
JettyHttpServer server = new JettyHttpServer(s, false);
JettyHttpContext context = (JettyHttpContext) server.createContext(PATH);
endpoint.publish(context);
source
share