Undertow [pt2]

All

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:

  1. Create an XNIO Worker. This worker manages both the IO and Worker threads for the server.
  2. Create an XNIO SSL instance (optional, only required if HTTPS is in use)
  3. Create an instance of the relevant Undertow listener class
  4. Open a server socket using XNIO and set its accept listener

 

 

REFs

http://undertow.io/

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s