【问题标题】:Publish and stop endpoint web service in Java在 Java 中发布和停止端点 Web 服务
【发布时间】:2013-03-18 21:59:57
【问题描述】:

我正在尝试通过单击按钮在与主程序不同的线程中启动/停止 Endpoint Web 服务。开始工作正常,我可以毫无问题地访问我的所有 WebMethods。问题是,当我单击停止按钮尝试停止 Web 服务端点时,我得到一个异常并且我不知道它是什么。我也是 Java 新手。

提前致谢。

在 'ep.stop()' 处抛出异常:

WebService Running On: http://0.0.0.0:9898/

        Exception in thread "Thread-0" java.lang.NullPointerException
            at com.sun.xml.internal.ws.transport.http.server.ServerMgr.removeContext(Unknown Source)
            at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.stop(Unknown Source)
            at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.stop(Unknown Source)
            at com.simba.Server.startWebSrv(Server.java:27)
            at com.simba.Server.run(Server.java:13)

服务器类:

import javax.xml.ws.Endpoint;

public class Server extends Thread {

      public volatile boolean active = true;
      private Endpoint ep; 
      private String ip = "0.0.0.0";

      public void run(){
            ep = Endpoint.publish("http://" + ip + ":9898/",  new SWS());
            startWebSrv();
      }

      private void startWebSrv(){
          synchronized(this){
              try {
                  while (active) {
                      System.out.println("WebService Running On: http://" + ip + ":9898/");
                      wait();
                  }
              }catch (InterruptedException e) {
                  e.printStackTrace();
              }finally{
                if(!active){
                    ep.stop();
                    System.out.println("WebService Stopped!");
                }
              }
          }
      }
}

我如何尝试从我的主程序中停止服务/线程:

MenuItem mntmToggleWebservice = new MenuItem(menu_4, SWT.NONE);
        mntmToggleWebservice.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                synchronized(srv){
                    srv.active = false;
                    srv.notifyAll();            
                }
            }
        });
        mntmToggleWebservice.setText("Toggle WebService");

【问题讨论】:

    标签: java eclipse webserver endpoint


    【解决方案1】:

    问题解决了!

    不要使用'0.0.0.0'。出于某种原因,在 '0.0.0.0' 上创建服务是可行的(使用机器的实际 ip),但 Endpoint.stop() 不能很好地使用它。

    我只是使用'System.getEvn('COMPUTERNAME')' 并使用机器名称创建服务。

    【讨论】:

    • 这应该是答案,我使用的是 0.0.0.0,错误将我带到这里。
    【解决方案2】:

    您可以使用“输入”来阻止程序的运行

    public class ServiceMain {
    
        private static void DoNothing() {
    
        }
    
        public static void main(String[] args) {  
            String address = "http://127.0.0.1:7775/hb";  
            Endpoint ep = Endpoint.publish(address, new ServiceImp());
            Scanner scanIn=new Scanner(System.in);
            System.out.println("Web Service Release Successfully!"); 
            while(!scanIn.next().equals("stop")){
                DoNothing();
            }
            scanIn.close();
            ep.stop();
            System.out.println("Web Service Close Successfully!"); 
    
        }  
    } 
    

    【讨论】:

      猜你喜欢
      • 2011-01-06
      • 2014-01-15
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多