<div dir="ltr"><div><div><div>Hello Team,<br><br></div>I there way to execute some code asynchronously in handler and fill response in callback?<br><br></div>I started with straightforward code like:<br><br></div><div><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:&quot;Fira Code&quot;;font-size:12pt">HttpHandler <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">asyncHandler </span>= (HttpServerExchange <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">exchange</span>) -&gt; {<br>    <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">client</span>.execute(<span style="color:rgb(0,0,128);font-weight:bold">new </span>HttpGet(<span style="color:rgb(0,128,0);font-weight:bold">&quot;<a href="https://newton.now.sh/">https://newton.now.sh/</a>&quot;</span>), <span style="color:rgb(0,0,128);font-weight:bold">new </span>FutureCallback&lt;HttpResponse&gt;() {<br>        <span style="color:rgb(128,128,0)">@Override<br></span><span style="color:rgb(128,128,0)">        </span><span style="color:rgb(0,0,128);font-weight:bold">public void </span>completed(HttpResponse <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">result</span>) {<br>            System.<span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">out</span>.println(<span style="color:rgb(0,128,0);font-weight:bold">&quot;Received.&quot;</span>);<br>            <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">exchange</span>.getResponseSender().send(<span style="color:rgb(0,128,0);font-weight:bold">&quot;Received.&quot;</span>);<br>        }<br><br>        <span style="color:rgb(128,128,0)">@Override<br></span><span style="color:rgb(128,128,0)">        </span><span style="color:rgb(0,0,128);font-weight:bold">public void </span>failed(Exception <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">ex</span>) {<br>            System.<span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">out</span>.println(<span style="color:rgb(0,128,0);font-weight:bold">&quot;Failed.&quot;</span>);<br>            <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">exchange</span>.getResponseSender().send(<span style="color:rgb(0,128,0);font-weight:bold">&quot;Failed.&quot;</span>);<br>        }<br><br>        <span style="color:rgb(128,128,0)">@Override<br></span><span style="color:rgb(128,128,0)">        </span><span style="color:rgb(0,0,128);font-weight:bold">public void </span>cancelled() {<br>            System.<span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">out</span>.println(<span style="color:rgb(0,128,0);font-weight:bold">&quot;Cancelled.&quot;</span>);<br>            <span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">exchange</span>.getResponseSender().send(<span style="color:rgb(0,128,0);font-weight:bold">&quot;Cancelled.&quot;</span>);<br>        }<br>    });<br>};</pre></div><div>When I run this code <b>completed</b> method called, but nothing written in response (since exchange already end)<br></div><div><br></div><div>So I googled my question and found SO answer, and I tried to dispatch exchange as described here: <a href="https://stackoverflow.com/a/25223070/1538877">https://stackoverflow.com/a/25223070/1538877</a><br></div><div><br></div><div>With no luck too.<br><br></div><div>Then I go to Request Lifecycle section of the docs (minor typo fix here: <a href="https://github.com/undertow-io/undertow-docs/pull/9">https://github.com/undertow-io/undertow-docs/pull/9</a>) <a href="http://undertow.io/undertow-docs/undertow-docs-1.4.0/index.html#undertow-request-lifecyle">http://undertow.io/undertow-docs/undertow-docs-1.4.0/index.html#undertow-request-lifecyle</a> and tried approach with dispatching current handler with no luck too. (But found that HttpHandlers class renamed too Connectors, fix: <a href="https://github.com/undertow-io/undertow-docs/pull/8">https://github.com/undertow-io/undertow-docs/pull/8</a>)<br></div><div><div><div><div><div></div><div><br></div><div>So my question is how to do async tasks in handlers.</div><div><br></div><div></div><div>My attempts as single file: <a href="https://gist.github.com/IRus/de8a23396ccd6b59df9267d4124393db">https://gist.github.com/IRus/de8a23396ccd6b59df9267d4124393db</a><br></div><div>(requires <a href="http://mvnrepository.com/artifact/org.apache.httpcomponents/httpasyncclient/4.1.3">http://mvnrepository.com/artifact/org.apache.httpcomponents/httpasyncclient/4.1.3</a>)<br clear="all"></div><div><br>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div style="font-size:12.8px"><div><div dir="ltr"><div dir="ltr"><div dir="ltr">Best regards,<br>Ibragimov Ruslan<br><a href="http://ibragimov.by/" style="color:rgb(17,85,204)" target="_blank">http://ibragimov.by/</a><br><a href="http://heap.by/" style="color:rgb(17,85,204)" target="_blank">http://heap.by/</a></div></div></div></div></div></div></div></div></div>
</div></div></div></div></div></div>