Java – how does undertow do non blocking IO?
•
Java
I use undertow to create a simple application
public class App { public static void main(String[] args) { Undertow server = Undertow.builder().addListener(8080,"localhost") .setHandler(new HttpHandler() { public void handleRequest(HttpServerExchange exchange) throws Exception { Thread.sleep(5000); exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,"text/plain"); exchange.getResponseSender().send("Hello World"); } }).build(); server.start(); } }
This time, the first tag will wait 5 seconds and the second will wait 10 seconds
Why is that?
Solution
HttpHandler is executing in the I / O thread As stated in the document:
Request lifecycle docs discusses how to send a request to a worker thread:
import io.undertow.Undertow; import io.undertow.server.*; import io.undertow.util.Headers; public class Under { public static void main(String[] args) { Undertow server = Undertow.builder() .addListener(8080,"localhost") .setHandler(new HttpHandler() { public void handleRequest(HttpServerExchange exchange) throws Exception { if (exchange.isInIoThread()) { exchange.dispatch(this); return; } exchange.getResponseHeaders() .put(Headers.CONTENT_TYPE,"text/plain"); exchange.getResponseSender() .send("Hello World"); } }) .build(); server.start(); } }
I noticed that each request does not necessarily get a worker thread - when I set a breakpoint on the header, I have one thread per client There is a gap between undertow and underlying xnio docs, so I don't know what it means
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码