我正在关注构建soap ws的教程,我无法理解为什么Netbeans不会停止端点,即java应用程序,从而让它使用端口。
我使用Netbeans(使用jdk 1.7.25)在MAC OS下运行以下代码
public class HelloPublisher {
public static void main(String[] args) {
try {
String localhost =InetAddress.getLocalHost().getHostAddress();
//InetAddress.getLoopbackAddress().getHostAddress();
System.out.println(InetAddress.getLocalHost().getHostAddress());
String port = "5901";
String url = "http://" + localhost + ":" + port + "/";
System.out.println(url);
Endpoint e = Endpoint.publish(url+ "ws/hello", new HelloWorldImpl());
} catch (UnknownHostException ex) {
System.out.println("errore");
}
}
}
但是当我停止然后再次运行它时,我得到以下错误:地址已在使用中。
Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use
at com.sun.xml.internal.ws.transport.http.server.ServerMgr.createContext(ServerMgr.java:102)
at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.publish(HttpEndpoint.java:63)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:171)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:113)
at javax.xml.ws.Endpoint.publish(Endpoint.java:240)
at it.unitn.lsde.firstsoap.endpoint.HelloPublisher.main(HelloPublisher.java:29)
Caused by: java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:444)
at sun.nio.ch.Net.bind(Net.java:436)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at sun.net.httpserver.ServerImpl.<init>(ServerImpl.java:100)
at sun.net.httpserver.HttpServerImpl.<init>(HttpServerImpl.java:50)
at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(DefaultHttpServerProvider.java:35)
at com.sun.net.httpserver.HttpServer.create(HttpServer.java:129)
at com.sun.xml.internal.ws.transport.http.server.ServerMgr.createContext(ServerMgr.java:84)
... 5 more
这是因为当我按下Netbeans下的停止按钮时java应用程序仍在运行,如果我想重用相同的地址或者我必须更改端口,我必须手动关闭它。我希望它能够关闭它,但它不会发生。
所以问题是:Netbeans做错了吗?或者我是以错误的方式使用端点? 我不想每次都改变端口..相同的代码在windows中完美运行..
答案 0 :(得分:0)
据我所知,杀死占用端口的应用程序应该释放该端口以供将来绑定。但是,您始终可以确保停止发布的服务:
Endpoint ep = Endpoint.create(new HelloWorldImpl());
ep.publish("http://" + localhost + ":" + port + "/ws/hello");
..
//DETECT STOP SIGNAL
ep.stop();
例如,停止信号可以是您可以通过servlet设置的一些标志。应定期或在循环中检查此标志。