Undertow is a flexible performant web server written in java, provid blocking and non-blocking api. It can also embeded in your app can ship it prod with no problem. Now it is also becoming the hot choice for mircoservice deployment.

Undertow is extremely lightweight, with the Undertow core jar coming in at under 1Mb. It is lightweight at runtime too, with a simple embedded server using less than 4Mb of heap space

Key Feature

  • Lightweight
  • Support HTTP Upgrade (multiplexed over the HTTP port)
  • Support Web Socket
  • Servlet 3.1
  • Embeddable

Undertow Server builder

Undertow use easy builder api to build Servlet Container and configure it as you would in deployment descriptor file (web.xml). As you configure your DeploymentInfo with Servlet class, request mapping, welcome files and other stuff that you do in your regular web.xml file, then use DeploymentManager to deploy it in ServletContainer, then path use PathHandler with context path to you deployed app in container and finally add our PathHandler to listener at specified port. Now start then undertow server to listen.

App.java

import static io.undertow.servlet.Servlets.servlet;

import javax.servlet.ServletException;

import org.glassfish.jersey.servlet.ServletContainer;

import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

public class App {

	private static Undertow server;

	public static void main(String[] args) 
        throws ServletException {
		startContainer(9090);
	}

	public static void stopContainer() {
		server.stop();
	}

	public static void startContainer(int port)
        throws ServletException {
        
        io.undertow.servlet.api.ServletContainer 
            servletContainer = Servlets.defaultContainer();
            
        DeploymentInfo di = Servlets.deployment()
            .setClassLoader(App.class.getClassLoader()).setContextPath("/jaxrx")
            .setDeploymentName("jaxrx-demo-1.0")
            .addServlets(servlet("jerseyServlet", ServletContainer.class)
            .setLoadOnStartup(1).setAsyncSupported(true)
            .addInitParam("javax.ws.rs.Application", JerseyApp.class.getName())
            .addMapping("/api/*")).addWelcomePage("index.html");
        
        DeploymentManager manager = servletContainer.addDeployment(di);
        
        manager.deploy();
        
        PathHandler path = Handlers.path(Handlers.redirect("/jaxrx"))
            .addPrefixPath("/jaxrx", manager.start());
            
        server = Undertow.builder()
            .addHttpListener(port, "localhost").setHandler(path).build();
            
        server.start();
    }
}

Dependency build.gradle

group 'com.dgstack.eg.jaxrs'
version '1.0'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'application'

sourceCompatibility = 1.8

mainClassName = 'com.dgstack.eg.jaxrx.App'

repositories {
    mavenCentral()
}

dependencies {
    //Jersey servlet dependencies
    compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.25'

    //Undertow dependencies
    compile group: 'io.undertow', name: 'undertow-core', version: '1.4.7.Final'
    compile group: 'io.undertow', name: 'undertow-servlet', version: '1.4.7.Final'
    
    //J2ee servlet dependencies
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'

}

task runApp(type: JavaExec) {
  classpath = sourceSets.main.runtimeClasspath
  main = 'com.dgstack.eg.jaxrx.App'
}

Get the code at this gist.

Share :

Subscribe & Social

Subscribe to this blog via RSS.

Recent Posts

Categories

Java 14

Python 2

Wordpress 1

Ops 3

Shared 3

Angular 1

Web 1

Draftblog 1

Popular Tags

Java (14) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1) Draftblog (1)

About

DGStack is web blog by digvijay bhakuni. This blog is about computer programming tips and trick. And some short tutorial and gettings started post about the framework, libriay, api and much more. The blog target mostly open source stuff like java, j2ee, nodeJs, angularJs, spring, struts, mongodb, mean, etc

Disclamer

This website claims no credit for any images posted on this site unless otherwise noted. Images on this blog are copyright to its respectful owners. If there is an image appearing on this blog that belongs to you and do not wish for it appear on this site, please E-mail with a link to said image and it will be promptly removed. And the information is best of my knowledge one can always write comment or mail regarding information is this blog.