How to shutdown com.sun.net.httpserver.HttpServer?
Asked Answered
C

4

6

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)?

Chronogram answered 29/5, 2009 at 21:21 Comment(0)
R
13

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();
Refer answered 26/5, 2010 at 22:29 Comment(1)
For a graceful shutdown, do: httpServer.stop(0); httpThreadPool.shutdown(); try { httpThreadPool.awaitTerminator(2, TimeUnit.HOURS); } catch (Exception e) {...shrieeeeek!...}Malefaction
P
0

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

Pabulum answered 30/5, 2009 at 1:26 Comment(3)
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.Chronogram
Err, it's public abstract void stop(...) on the class directly, so where is the problem? See: java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/…Nutritious
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.Chronogram
N
0

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.

Nutritious answered 16/6, 2009 at 9:13 Comment(1)
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.Chronogram
F
-1
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.

Fabria answered 30/5, 2009 at 4:54 Comment(1)
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?Nutritious

© 2022 - 2024 — McMap. All rights reserved.