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

Sascha Sadat-Guscheh kid at bitkid.com
Tue Dec 15 07:11:12 EST 2015


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 at redhat.com> wrote:
> 
> 
> 
> ----- 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 <mailto:undertow-dev at lists.jboss.org>
>> https://lists.jboss.org/mailman/listinfo/undertow-dev <https://lists.jboss.org/mailman/listinfo/undertow-dev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20151215/6580109f/attachment-0001.html 


More information about the undertow-dev mailing list