[undertow-dev] Using Async Api inside HttpHandler

Ruslan Ibragimov ruslan at ibragimov.by
Mon Oct 16 11:59:57 EDT 2017


Hello Team,

I there way to execute some code asynchronously in handler and fill
response in callback?

I started with straightforward code like:

HttpHandler asyncHandler = (HttpServerExchange exchange) -> {
    client.execute(new HttpGet("https://newton.now.sh/"), new
FutureCallback<HttpResponse>() {
        @Override
        public void completed(HttpResponse result) {
            System.out.println("Received.");
            exchange.getResponseSender().send("Received.");
        }

        @Override
        public void failed(Exception ex) {
            System.out.println("Failed.");
            exchange.getResponseSender().send("Failed.");
        }

        @Override
        public void cancelled() {
            System.out.println("Cancelled.");
            exchange.getResponseSender().send("Cancelled.");
        }
    });
};

When I run this code *completed* method called, but nothing written in
response (since exchange already end)

So I googled my question and found SO answer, and I tried to dispatch
exchange as described here: https://stackoverflow.com/a/25223070/1538877

With no luck too.

Then I go to Request Lifecycle section of the docs (minor typo fix here:
https://github.com/undertow-io/undertow-docs/pull/9)
http://undertow.io/undertow-docs/undertow-docs-1.4.0/index.html#undertow-request-lifecyle
and tried approach with dispatching current handler with no luck too. (But
found that HttpHandlers class renamed too Connectors, fix:
https://github.com/undertow-io/undertow-docs/pull/8)

So my question is how to do async tasks in handlers.

My attempts as single file:
https://gist.github.com/IRus/de8a23396ccd6b59df9267d4124393db
(requires
http://mvnrepository.com/artifact/org.apache.httpcomponents/httpasyncclient/4.1.3
)

-- 
Best regards,
Ibragimov Ruslan
http://ibragimov.by/
http://heap.by/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20171016/86f12ac5/attachment-0001.html 


More information about the undertow-dev mailing list