title: Undertown. Async responce with another thread
body: <p>I try to realize how to build an true-async http server with Undertow. How to send a response asynchronously if I have another thread which one is processing a request already?<br>
  I wrote code like this: </p>
  
  <pre class="lang-java prettyprint-override"><code>    Undertow server = Undertow.builder()
              .addHttpListener(8080, "localhost")
              .setHandler(exchange -&gt; {
                  CompletableFuture.runAsync(() -&gt; {
                      try {
                          Thread.sleep(100);
                      } catch (InterruptedException e) {
                          throw new RuntimeException(e);
                      }
                  }).thenAccept(string -&gt; {
                      exchange.getResponseHeaders()
                              .put(Headers.CONTENT_TYPE, "text/plain");
                      exchange.getResponseSender().send("Hello World");
                      exchange.endExchange();
                  }).exceptionally(throwable -&gt; {
                      System.out.println(throwable.toString());
                      return null;
                  });
              }).build();
      server.start();
  </code></pre>
  
  <p>but this server response 200 with no data and in logs </p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: UT000127: Response has already been sent</p>
  </blockquote>
  
  <p>When I use io.undertow.server.HttpServerExchange#dispatch(java.lang.Runnable) method like this: </p>
  
  <pre class="lang-java prettyprint-override"><code>    Undertow server = Undertow.builder()
              .addHttpListener(8080, "localhost")
              .setHandler(exchange -&gt; {
  
                  exchange.dispatch(() -&gt; {
  
                      CompletableFuture.runAsync(() -&gt; {
                          try {
                              Thread.sleep(100);
                          } catch (InterruptedException e) {
                              throw new RuntimeException(e);
                          }
                      }).thenAccept(string -&gt; {
                          exchange.getResponseHeaders()
                                  .put(Headers.CONTENT_TYPE,"text/plain");
                          exchange.getResponseSender().send("Hello World");
                          exchange.endExchange();
                      }).exceptionally(throwable -&gt; {
                          System.out.println(throwable.toString());
                          return null;
                      });
  
                  });
              }).build();
      server.start();
  </code></pre>
  
  <p>of course a response "Hello World" as expected but server creates new thread for every request! </p>
  
  <p>(jvisualvm after 10 parallel requests)
  <a href="https://i.stack.imgur.com/k2gJf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k2gJf.png" alt="jvisualvm after 10 parallel requests"></a></p>