How to disable the embedded Undertow app?

In the Undertow documentation, the following example is presented as a very simple server:

public class HelloWorldServer {

    public static void main(final String[] args) {
        Undertow server = Undertow.builder()
                .addHttpListener(8080, "localhost")
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServerExchange exchange) throws Exception {
                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                        exchange.getResponseSender().send("Hello World");
                    }
                }).build();
        server.start();
    }
}

How is server shutdown? There is a method server.stop()that supposedly shuts down the server gracefully, but it is not used in this example. Should there be a runtime click disconnect click that will invoke the method server.stop()?

+4
source share
1 answer

server.start() - blocking operation.

If you started the server in another thread, you can call server.stop()from the main thread. For example:

new Thread(() -> { server.start(); }).start();
System.out.print("Server started. Press 'Enter' to stop...");
System.in.read();
server.stop();
System.out.println("Server stopped.");
+1
source

Source: https://habr.com/ru/post/1613775/


All Articles