Intro
Ok, quick stuff, just reading about Undertow, I didn’t know it was so flexible actually.
Undertow two way to do it:
Way 1
There is basically two way to use undertow, and in fact, I already made a post about the first way here, the idea is simple: use the Builder API after we create the handler we want as in here. The listener on the example is created for the port 8080, i.e. bind to localhost.
The builder will default values for performance-related stuff, as a number of threads and buffers.
public class HelloWorldServer {
public static void main(final String[] args) {
Undertow server = Undertow.builder() //Undertow builder
.addHttpListener(8080, "localhost") //Listener binding
.setHandler(new HttpHandler() { //Default Handler
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); //Response Headers
exchange.getResponseSender().send("Hello World"); //Response Sender
}
}).build();
server.start();
}
}
Way 2
Otherwise, the second way to do stuff is by manually assembling stuff to create a server.
The main steps are described here:
- Create an XNIO Worker. This worker manages both the IO and Worker threads for the server.
- Create an XNIO SSL instance (optional, only required if HTTPS is in use)
- Create an instance of the relevant Undertow listener class
- Open a server socket using XNIO and set its accept listener
REFs