[undertow-dev] Using Async Api inside HttpHandler

Stuart Douglas sdouglas at redhat.com
Tue Oct 17 01:55:08 EDT 2017


You need to use the Runnable variant of dispatch() not the HttpHandler
version. The HttpHandler version will dispatch your exchange to a new
executor, but will still end the exchange when the handler returns
(unless you have dispatched again). The Runnable version will not, and
it is entirely up to you to make sure that the exchange is eventually
completed.

Stuart

On Mon, Oct 16, 2017 at 5:59 PM, Ruslan Ibragimov <ruslan at ibragimov.by> wrote:
> 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/
>
> _______________________________________________
> 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