Undertow how to do Non-blocking IO?

后端 未结 3 2027
清歌不尽
清歌不尽 2021-02-20 10:16

I am using Undertow to create a simple application.

public class App {
    public static void main(String[] args) {
        Undertow server = Undertow.builder().         


        
3条回答
  •  感动是毒
    2021-02-20 10:42

    The easiest thing to do would be to wrap your handler in a BlockingHandler.

    import io.undertow.Undertow;
    import io.undertow.server.*;
    import io.undertow.server.handlers.BlockingHandler;
    import io.undertow.util.Headers;
    
    public class Under {
        public static void main(String[] args) {
            Undertow server = Undertow.builder()
                    .addHttpListener(8080, "localhost")
                    .setHandler(new BlockingHandler(new HttpHandler() {
                        public void handleRequest(HttpServerExchange exchange)
                                throws Exception {
                            exchange.getResponseHeaders()
                                    .put(Headers.CONTENT_TYPE, "text/plain");
                            exchange.getResponseSender()
                                    .send("Hello World");
                        }
                    })).build();
            server.start();
        }
    }
    

提交回复
热议问题