mszhe的技术分享 人心惟危,道心惟微。惟精惟一,允执厥中。

undertow

2017-10-11

阅读:


官网

http://undertow.io/

简介

  • Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.
  • Undertow是用java语言编写的、灵活性强的web服务器,支持NIO和BIO
  • Undertow has a composition based architecture that allows you to build a web server by combining small single purpose handlers. The gives you the flexibility to choose between a full Java EE servlet 3.1 container, or a low level non-blocking handler, to anything in between.
  • Undertow is designed to be fully embeddable(嵌入式), with easy to use fluent builder APIs. Undertow’s lifecycle is completely controlled by the embedding application.
  • Undertow is sponsored by JBoss and is the default web server in the Wildfly Application Server.

Why Undertow

Lightweight

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.

HTTP Upgrade Support

Support for HTTP upgrade, to allow multiple protocols to be multiplexed over the HTTP port.

Web Socket Support

Undertow provides full support for Web Sockets, including JSR-356 support.

Servlet 3.1

Undertow provides support for Servlet 3.1, including support for embedded servlet. It is also possible to mix both Servlets and native undertow non-blocking handlers in the same deployment.

Embeddable

Undertow can be embedded in an application or run standalone with just a few lines of code.

Flexible

An Undertow server is configured by chaining handlers together. It is possible to add as much or as little functionality as you need, so you don’t pay for what you are not using.

Show me the code

方式一,普通方式

maven dependency:

<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-core</artifactId>
    <version>1.4.12.Final</version>
</dependency>

java code(Async IO):

package com.mszhe;

import io.undertow.Undertow;
import io.undertow.util.Headers;

public class Demo {
    public static void main(String[] args) {
        Undertow undertow = Undertow.builder()
                .addHttpListener(8080, "localhost")
                .setHandler(exchange -> {
                    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                    exchange.getResponseSender().send("Hello World");
                }).build();
        undertow.start();
    }
}

方式二,undertow部署servlet

maven dependency:

<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-servlet</artifactId>
    <version>1.4.12.Final</version>
</dependency>

main method:

package com.mszhe;

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

import javax.servlet.ServletException;

public class Demo2 {
    public static void main(String[] args) {
        ServletInfo servletInfo = Servlets.servlet("hi", MyServlet.class);
        servletInfo.addInitParam("message", "hello world!");
        servletInfo.addMapping("/hi");

        DeploymentInfo deploymentInfo = Servlets.deployment();
        deploymentInfo.setClassLoader(Demo2.class.getClassLoader());
        deploymentInfo.setContextPath("/");
        deploymentInfo.setDeploymentName("hi.war");
        deploymentInfo.addServlets(servletInfo);

        ServletContainer container = Servlets.defaultContainer();
        DeploymentManager manager = container.addDeployment(deploymentInfo);
        manager.deploy();

        PathHandler pathHandler = Handlers.path();
        HttpHandler myApp;
        try {
            myApp = manager.start();
        } catch (ServletException e) {
            throw new RuntimeException("failed!");
        }

        pathHandler.addPrefixPath("/", myApp);

        Undertow server = Undertow.builder().
                addHttpListener(8080, "localhost")
                .setHandler(pathHandler).build();

        server.start();
    }
}

MyServlet.java:

package com.mszhe;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.write("<p style='color:red;text-align:center;'>"+this.getInitParameter("message")+"</p>");
        writer.close();
    }
}