Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 879
Dean Schulze Avatar asked May 29 '09 21:05

Dean Schulze


People also ask

How to stop HttpServer Java?

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

What is HttpServer in Java?

A HttpServer is bound to an IP address and port number and listens for incoming TCP connections from clients on this address. The sub-class HttpsServer implements a server which handles HTTPS requests. One or more HttpHandler objects must be associated with a server in order to process requests.

What is HttpHandler Java?

A handler which is invoked to process HTTP exchanges. Each HTTP exchange is handled by one of these handlers. Since: 1.6.

How to stop httpserver from running?

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

What is httpserver https?

This class is an extension of HttpServer which provides support for HTTPS. Provides a simple high-level Http server API, which can be used to build embedded HTTP servers. Both "http" and "https" are supported. The API provides a partial implementation of RFC 2616 (HTTP 1.1) and RFC 2818 (HTTP over TLS).

What is package com net httpserver?

Package com.sun.net.httpserver Description Provides a simple high-level Http server API, which can be used to build embedded HTTP servers. Both "http" and "https" are supported. The API provides a partial implementation of RFC 2616 (HTTP 1.1) and RFC 2818 (HTTP over TLS).


1 Answers

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();
like image 132
Kalpesh Patel Avatar answered Oct 14 '22 09:10

Kalpesh Patel