2013-03-18 65 views
5

我試圖通過點擊一個按鈕在我的主程序的單獨線程中啓動/停止一個端點Web服務。開始工作正常,我能夠訪問所有我的WebMethods沒有問題。問題是,當我點擊停止按鈕嘗試停止Web服務端點時,出現異常,我不知道它是什麼。我也是Java新手。發佈和停止Java中的端點Web服務

在此先感謝。

EXCETION在 '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"); 

回答

8

問題解決了! 不要使用'0.0.0.0'。出於某種原因,在'0.0.0.0'創建服務工作(使用機器的實際IP),但Endpoint.stop()不能很好地發揮它。相反,我只是使用'System.getEvn('COMPUTERNAME')'並用機器名創建服務。

+1

這的運行應該是答案,我用的是0.0.0.0,錯誤把我帶到這裏。 – 2016-05-04 23:25:13

0

您可以使用「輸入」阻止程序

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!"); 

    } 
}