views:

681

answers:

4

The Http Server embedded in JDK 6 is a big help developing web services, but I've got situation where I published an Endpoint and then the code crashed and left the server running.

How do you shutdown the embedded server once you've lost the reference to it (or the published Endpoint)?

A: 

I've never used this server before and I can't find any good documentation. Perhaps these less elegant solutions have occurred to you already, but I just thought I would mention them.

Seems like com.sun.net.httpserver.HttpServer has an implementation class called HttpServerImpl. It has a method called stop().

Or perhaps you can find the Thread listening on the server socket and call interrupt().

Sean

Sean McCauliff
If I had a handle to HttpServerImpl I could call stop() on it, but I have one process creating the HttpServer and want to call stop on it with another process.
Dean Schulze
Err, it's public abstract void stop(...) on the class directly, so where is the problem? See: http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html#stop(int)
ShiDoiSi
I should have provided more detail. I publish an endpoint in one JVM and if an operation on the endpoint crashes I need to shutdown the server in a different JVM. I need to call the server's admin port, but I don't see anything like an admin socket for the embeded server.
Dean Schulze
A: 
netstat -a

to find the pid of the process that has the port open (assuming you know the port), and

kill -9 $pid

to kill the process.

Dave W. Smith
I would guess that the server runs as a thread in the JVM, so you're suggesting to heavy-handedly shoot down the whole JVM.@Dean: I think you are looking for a cleaner solution which doesn't require restarting the JVM?
ShiDoiSi
A: 

How about not loosing the reference, then? When you say your code crashes, I assume you get an exception somewhere. Where exactly? So someone capable of intercepting this exception obviously needs to also have a reference to the HttpServer, which you might have to pass around yourself.

Edit: Oh. In that case if you don't want to kill the entire JVM with the HttpServer in it, then you will need to offer some form of IPC to the environment, e.g. a command channel via RMI that can be invoked from a Java program (and hence Ant).

Another solution would be to have the server listen for some "secret" cookie query, where you e.g. print/save the cookie on startup so that the Ant script can retrieve the cookie, and you can fire off a query to your "secret" URL upon which the server will exit itself gracefully.

I'd go with a quick RMI solution.

ShiDoiSi
It's the main() method in an example client that publishes the endpoint and executes the example. If it crashes I need to be able to shut the server down via Ant. The publish() and stop() would be done in a different JVM.
Dean Schulze
+1  A: 

I use the below code to start it

    this.httpServer = HttpServer.create(addr, 0);
    HttpContext context = this.httpServer.createContext("/", new DocumentProcessHandler());
    this.httpThreadPool = Executors.newFixedThreadPool(this.noOfThreads);
    this.httpServer.setExecutor(this.httpThreadPool);
    this.httpServer.start();

and below code to stop it

        this.httpServer.stop(1);
        this.httpThreadPool.shutdownNow();
Kalpesh Patel