web server Undertow

undertow 官网地址:http://undertow.io/index.html

以下内容摘自官网。

Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.

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 4.0 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

HTTP/2 Support

Undertow supports HTTP/2 out of the box, no overriding the boot class path required.

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 4.0

Undertow provides support for Servlet 4.0, 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

Ok then. Here is a simple Hello World server using Async IO:

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();
    }
}

Downloads

The easiest way to get started using Undertow is to download the Wildfly Application Server.

Undertow is the default web server in Wildfly, so usage is as simple as deploying a war.

Maven

If you want to embed Undertow then you will most likely want to use Maven. There are currently three version branches for Undertow:

2.0

The current stable Servlet 4.0 branch, requires JDK8 or above

1.4

The current stable Servlet 3.1 branch, supports JDK7

In order to use Undertow you need to add the following artifacts to your pom:

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

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

<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-websockets-jsr</artifactId>
    <version>2.0.1.Final</version>
</dependency>

You will only need the Undertow Servlet artifact if you wish to use Servlet in your application.

If you just want to download the jars directly you can download them from Maven Central.

 

Get Involved

Undertow is open source and developed on Github. Contributions are always welcome.

 

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自huangqiqing123.iteye.com/blog/2421852