Hi Stuart!

Thanks for your response. I already figured that the idea to wait for the future is probably not the best one ;-)

exchange.getIoThread()

that was what i was looking for.

Merci, Sascha

On 14 Dec 2015, at 02:32, Stuart Douglas <sdouglas@redhat.com> wrote:



----- Original Message -----
From: "Sascha Sadat-Guscheh" <kid@bitkid.com>
To: undertow-dev@lists.jboss.org
Sent: Thursday, 10 December, 2015 9:22:38 PM
Subject: [undertow-dev] how to use futures with undertow?

Hi!

We want to use undertow as the http server for our application. Our current
API is based on futures (java8 completable futures). My question is how do i
combine futures with the concept of handlers. My rather naive implementation
would be something like that.

class MyHandler implements HttpHandler {
       @Override
       public void handleRequest(HttpServerExchange exchange) throws
       Exception {
           exchange.getRequestReceiver().receiveFullString((exchange1,
           message) -> {
               CompletableFuture<Response> responseFuture =
               doTheWork(message);
               exchange1.dispatch();
               responseFuture.whenCompleteAsync((response, throwable) -> {
                    exchange1.getResponseSender().send(response);
               });
           });
       }
}

This looks reasonable. If you want to use an Executor you can use either:

exchange.getIoThread() (to execute in the IO thread, this should be fine for you example)
exchange.getConnection.getWorker() (to execute blocking tasks)

Stuart


does this look reasonable? responseFuture.whenCompleteAsync() can be passed
an Executor. Should i use an Executor from the HttpServerExchange for that?
actually i tried to, but it’s always null. Another way would be:

class MyHandler implements HttpHandler {
       @Override
       public void handleRequest(HttpServerExchange exchange) throws
       Exception {
           exchange.getRequestReceiver().receiveFullString((exchange1,
           message) -> {
               CompletableFuture<Response> responseFuture =
               doTheWork(message);
               exchange1.dispatch(() -> {
                       exchange1.getResponseSender().send(responseFuture.get());
               });
           });
       }
}

This will block the thread until the future is ready, the 


What’s the correct way to do it?

Thanks, Sascha
_______________________________________________
undertow-dev mailing list
undertow-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/undertow-dev