[undertow-dev] how to use futures with undertow?

Stuart Douglas sdouglas at redhat.com
Sun Dec 13 20:32:09 EST 2015



----- Original Message -----
> From: "Sascha Sadat-Guscheh" <kid at bitkid.com>
> To: undertow-dev at 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 at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev



More information about the undertow-dev mailing list