Publish and stop endpoint web service in Java

B3st0fth3w0rst picture B3st0fth3w0rst · Mar 18, 2013 · Viewed 8.2k times · Source

I am trying to start/stop an Endpoint web service in a separate thread from my main program with the click of a button. Starting works fine, i am able to access all my WebMethods without issue. The problem is, when i click the stop button to try and stop the web service endpoint, i get an exception and i don't know what it is. I am new to Java as well.

Thanks in advance.

Excetion thrown at '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)

Server class:

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

How i am attempting to stop the service/thread from my main program:

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

Answer

B3st0fth3w0rst picture B3st0fth3w0rst · Mar 20, 2013

Problem solved!

Don't use '0.0.0.0'. For some reason, creating the service on '0.0.0.0' works (uses the machine's actual ip), but Endpoint.stop() doesn't not play well with it.

Instead I just used 'System.getEvn('COMPUTERNAME')' and create the service with the machine name.