NanoHTTP as windows service with Apache Commons Daemon

161 Views Asked by At

I made a small HTTP listener with NanoHTTP library, I would like to use it with Apache Commons Daemon (https://commons.apache.org/proper/commons-daemon/index.html) to install with procrun as Windows service. I read the docs but I cannot understand how to manage start-stop jobs in separate process. How can I stop NanoHTTPD from another separate Java process?

This is my class extending NanoHTTPD:

public class PrintServer extends NanoHTTPD {

    private static final int SERVER_PORT=11100;
    private static final String MIMEJSON = "application/json";

    public PrintServer() throws IOException {
        super(SERVER_PORT);
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
        log.info("Cinebot PrintServer avviato e in ascolto su porta {}",SERVER_PORT);

    }

    public static void main(String[] args) {
        try {
            new PrintServer();
        } catch (IOException ioe) {
            log.error("Non posso avviare PrintServer", ioe);
        }
    }

    @Override
    public Response serve(IHTTPSession session) {
        return newFixedLengthResponse(Response.Status.OK, MIMEJSON, null);
    }

}
0

There are 0 best solutions below