How to shutdown com.sun.net.httpserver.HttpServer?

Dean Schulze picture Dean Schulze · May 29, 2009 · Viewed 10.6k times · Source

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

Answer

Kalpesh Patel picture Kalpesh Patel · May 27, 2010

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