繁体   English   中英

在Windows下工作时,端点未在Netbeans和Mac OS下停止

[英]Endpoint not stopped under netbeans and mac os, while working under windows

我正在学习构建肥皂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中完美地工作..

据我所知,杀死占用端口的应用程序应该释放该端口以供将来绑定。 但是,您始终可以确保停止发布的服务:

Endpoint ep = Endpoint.create(new HelloWorldImpl());
ep.publish("http://" + localhost + ":" + port + "/ws/hello");
..
//DETECT STOP SIGNAL
ep.stop();

例如,停止信号可以是您可以通过Servlet设置的一些标志。 应定期或在循环中检查此标志。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM