Search code examples
javaportcxfjax-wsendpoint

How to release port of CXF-Endpoint gracefully?


I am using CXF to simply publish an endpoint, lets say:

final String uri = http://myServiceEndpoint:12345
final Endpoint endpoint = Endpoint.publish(uri, MyService());

After some processing I want to shutdown the endpoint and release the port with:

endpoint.stop();

Immediately after the "stop" instruction another service should start and bind the above mentioned port, but I am getting the following error:

Bind to port 12345 failed: java.net.BindException: Address already in use: bind
    at java.base/sun.nio.ch.Net.bind0(Native Method)
    at java.base/sun.nio.ch.Net.bind(Net.java:455)
    at java.base/sun.nio.ch.Net.bind(Net.java:447)
    at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
    at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
    at org.apache.http.impl.nio.reactor.DefaultListeningIOReactor.processSessionRequests(DefaultListeningIOReactor.java:243)
    at org.apache.http.impl.nio.reactor.DefaultListeningIOReactor.processEvents(DefaultListeningIOReactor.java:144)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:348)
    at de.hacon.dds.actor.net.http.hc.HttpApacheManager.lambda$startTheReactorQuaid$2(HttpApacheManager.java:135)
    at java.base/java.lang.Thread.run(Thread.java:834)

What can I do to release the port immediately?


Solution

  • In the meanwhile I could fix the problem releasing the port gracefully:

    endpoint.stop();
    JettyHTTPServerEngineFactory.destroyForPort(myPort);
    

    The additional calling of the destroy method the port is immediately released and the other service can bind the same port.