Works like a charm, thanks again.<br><br><div class="gmail_quote">On Mon Dec 15 2014 at 7:56:49 AM Laurent Bedubourg &lt;<a href="mailto:laurent@labe.me">laurent@labe.me</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thank you Stuart, I will try this ASAP.<br><br>Best regards<br>Laurent<br><br><div class="gmail_quote">On Sun, 14 Dec 2014 23:28 Stuart Douglas &lt;<a href="mailto:sdouglas@redhat.com" target="_blank">sdouglas@redhat.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-family:times new roman,new york,times,serif;font-size:12pt;color:#000000"><div>You need to call the HttpServerExchange.dispatch() method, which will prevent the exchange from being ended once the call stack returns. </div><div><br></div><div>There is however a thread safety issue here, as you can have your callback being called at the same time as the call stack is still returning, which means it is possible to have two threads using the HttpServerExchange, which is not thread safe (in practice this will *probably* work fine, depending on how your code is written). </div><div><br></div><div>The correct way to deal with this is:</div><div><br></div><div>HttpServerExchange.dispatch(<u></u>SameThreadExecutor.INSTANCE, new Runnable() {</div><div>   public void run() {</div><div>      //setup callback</div><div>   }</div><div>}</div><div><br></div><div>Basically this does two things, calling dispatch means that the exchange will not be ended when the call stack returns, and dispatching using SameThreadExecutor means that the runnable will be run immediately after the call stack returns in the IO thread (or whatever thread is currently active). </div><div><br></div><div>If you don&#39;t want to use a callback and just want to use await:</div><div><br></div><div><div>HttpServerExchange.dispatch(<u></u>new Runnable() {</div><div>   public void run() {</div><div>      //call await()</div><div>   }</div><div>}</div><div><br></div><div>This will dispatch to the worker thread pool, and await() will be called in a worker thread, although if you use this approach you might as well just use plain JDBC, as you loose the benefits of non-blocking IO. </div></div><div><br></div><div>Stuart</div><div><br></div><hr><blockquote style="border-left:2px solid #1010ff;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><b>From: </b>&quot;Laurent Bedubourg&quot; &lt;<a href="mailto:laurent@labe.me" target="_blank">laurent@labe.me</a>&gt;<br><b>To: </b>&quot;Jason Greene&quot; &lt;<a href="mailto:jason.greene@redhat.com" target="_blank">jason.greene@redhat.com</a>&gt;<br><b>Cc: </b><a href="mailto:undertow-dev@lists.jboss.org" target="_blank">undertow-dev@lists.jboss.org</a><br><b>Sent: </b>Saturday, 13 December, 2014 6:58:51 PM<br><b>Subject: </b>Re: [undertow-dev] scala, activate, nio, async jdbc, future, threads</blockquote></div></div><div><div style="font-family:times new roman,new york,times,serif;font-size:12pt;color:#000000"><blockquote style="border-left:2px solid #1010ff;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><br><div><br></div>Thank you very much Jason, it helps me a lot.<div><br></div><div>One last remark regarding &quot;<span style="font-size:13.1999998092651px;line-height:19.7999992370605px">you can save yourself a worker thread and reuse the thread they generate the callback from. At that callback point you can write your response out to the exchange.&quot; </span></div><div><span style="font-size:13.1999998092651px;line-height:19.7999992370605px"><br></span></div><div><span style="font-size:13.1999998092651px;line-height:19.7999992370605px">When I try to do this, calling </span><span style="font-size:13.1999998092651px;line-height:19.7999992370605px">x.getResponseSender().<u></u>send(str) un the onSuccess callback of the database future, </span><span style="line-height:19.7999992370605px;font-size:13.1999998092651px">I got an exception </span><span style="font-size:13.1999998092651px;line-height:1.5">java.lang.<u></u>IllegalStateException: UT000004: getResponseChannel() has already been called. </span></div><div><br></div><div><span style="font-size:13.1999998092651px;line-height:1.5">I imagine that the response has already been somehow cleaned up / modified / terminated by Undertow when the handler exited (I use SessionAttachHandler, date() and routing() in my test).</span></div><div><span style="font-size:13.1999998092651px;line-height:1.5"><br></span></div><div><span style="font-size:13.1999998092651px;line-height:1.5">I could not find a way to tell Undertow that I would handle the exchange and end it manually. </span></div><div><span style="line-height:1.5;font-size:13.1999998092651px"><br></span></div><div><span style="line-height:1.5;font-size:13.1999998092651px">I am using undertow 1.1.1-final maybe this has been fixed since this version?</span></div><div><span style="line-height:1.5;font-size:13.1999998092651px"><br></span></div><div><span style="line-height:1.5;font-size:13.1999998092651px">Thanks again</span></div><div><span style="line-height:1.5;font-size:13.1999998092651px"><br></span></div><div><span style="line-height:1.5;font-size:13.1999998092651px">Laurent</span></div><div><br><div><br></div></div><br><div class="gmail_quote">On Sat Dec 13 2014 at 4:57:33 AM Jason Greene &lt;<a href="mailto:jason.greene@redhat.com" target="_blank">jason.greene@redhat.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Laurent,<br>
<br>
Your hunch is correct. You never want to block the I/O thread as each thread is shared between many connections, and a pause prevents them from being processed. It’s totally reasonable to call dispatch(), which transfers the exchange to the worker pool where you can wait on a database reply, and in fact this is a normal pattern. The worker pool is sized to be pretty large as its expected to host lots of waiters. You can of course tweak this, or dispatch to your own special pool if you like. The big advantage to an async database call, even with requiring a worker thread, is that you can do other work while its running if your pattern fits (e.g. parallel db calls, iterative computation, etc).<br>
<br>
Alternatively, if the database provides a callback on complete option, you can save yourself a worker thread and reuse the thread they generate the callback from. At that callback point you can write your response out to the exchange.  Just note, that the thread policy of an exchange is that only one thread at a time can interact with it. If you have a use case where the a request bounces back and forth (e.g. a very long stream containing multiple records that must be written to the database as they com in), then you will need to coordinate locking around that. Such use-cases though should be weighed against the simplicity of just blocking in a worker thread.<br>
<br>
To answer a second question you have in a comment from your example, startBlocking() just enables the use of a plain inputstream/outputstream, which can only be done once you have dispatched and blocking i/o is now ok.<br>
<br>
Hope this helps,<br>
<br>
-Jason<br>
<br>
&gt; On Dec 12, 2014, at 3:44 PM, Laurent Bedubourg &lt;<a href="mailto:laurent@labe.me" target="_blank">laurent@labe.me</a>&gt; wrote:<br>
&gt;<br>
&gt; Ok, I think my mail could be reduced to :<br>
&gt;<br>
&gt; Is it possible possible to transfer the HttpServerExchange responsibility to another thread ?<br>
&gt;<br>
&gt; Regards<br>
&gt; Laurent<br>
&gt;<br>
&gt;<br>
&gt; On Fri Dec 12 2014 at 7:11:01 PM Laurent Bedubourg &lt;<a href="mailto:laurent@labe.me" target="_blank">laurent@labe.me</a>&gt; wrote:<br>
&gt; Hello,<br>
&gt;<br>
&gt; I am evaluating Undertow and trying to fit an async database (with scala, Activate and <a href="https://github.com/mauricio/postgresql-async" target="_blank">https://github.com/mauricio/<span style="text-decoration:underline"></span>po<u></u>stgresql-async</a> which use netty and nio).<br>
&gt;<br>
&gt; The good news is that it more or less works.<br>
&gt;<br>
&gt; My main concern is that the database system create threads using scala futures and that I am forced to &quot;Await&quot; for the result of my Futures to send the result to the HttpServerExchange.<br>
&gt;<br>
&gt; I am forced to dispath() the request because Await locks the current thread and I shouldn&#39;t lock the IO thread from my Handler, should I?<br>
&gt;<br>
&gt; It made me wondering : is is really a good idea to use futures + Await with undertow and more generaly nio?<br>
&gt;<br>
&gt; If the thread pool is limited and I lock threads, even if the database driver is async and uses nio, am I bitting my own leg?<br>
&gt;<br>
&gt; Thanks for any input you can give me and sorry if it&#39;s too scala related :)<br>
&gt;<br>
&gt; Regards<br>
&gt; Laurent<br>
&gt;<br>
&gt; PS: Here&#39;s the handleRequest I am using to test this, any comment or help welcome too since I am discovering the API.<br>
&gt;<br>
&gt; def handleRequest(x:io.undertow.<span style="text-decoration:underline"></span>se<u></u>rver.HttpServerExchange){<br>
&gt; if (x.isInIoThread()){<br>
&gt;   x.dispatch(this)<br>
&gt;   return<br>
&gt; }<br>
&gt; // useful or not? no change<br>
&gt; // x.startBlocking()<br>
&gt;<br>
&gt; // Ok, we are in worker thread we can work a little with database<br>
&gt; // val f = asyncTransactionalChain { implicit context =&gt;<br>
&gt; //   for (<br>
&gt; //     a &lt;- asyncTransactional { new AMember(&quot;aaa&quot;) };<br>
&gt; //     b &lt;- asyncById[AMember](<a href="http://a.id" target="_blank">a.id</a>)<br>
&gt; //   ) yield b<br>
&gt; // }<br>
&gt;<br>
&gt; // Simulate database work with just a future<br>
&gt; val f = future {<br>
&gt;   Some(1)<br>
&gt; }(scala.concurrent.<span style="text-decoration:underline"></span>ExecutionCo<u></u>ntext.Implicits.<span style="text-decoration:underline"></span>global)<br>
&gt;<br>
&gt; // send string to xchange<br>
&gt; def sendReply(str:String){<br>
&gt;   x.getResponseHeaders().put(<u></u>io.<span style="text-decoration:underline"></span>undertow.util.Headers.<u></u>CONTENT_<span style="text-decoration:underline"></span>TYPE, &quot;text/plain&quot;)<br>
&gt;   x.getResponseSender().send(<span style="text-decoration:underline"></span>st<u></u>r)<br>
&gt;   // x.endExchange()<br>
&gt; }<br>
&gt;<br>
&gt; // // this fails<br>
&gt; // f.onSuccess {<br>
&gt; //   case Some(result) =&gt; sendReply(result.toString)<br>
&gt; // }(scala.concurrent.<span style="text-decoration:underline"></span>ExecutionCo<u></u>ntext.Implicits.<span style="text-decoration:underline"></span>global)<br>
&gt;<br>
&gt; // // this works<br>
&gt; val result = Await.result(f, Duration(1000, MILLISECONDS))<br>
&gt; sendReply(result.toString)<br>
&gt; }<br>
&gt;<br>
&gt; ______________________________<span style="text-decoration:underline"></span><u></u>_________________<br>
&gt; undertow-dev mailing list<br>
&gt; <a href="mailto:undertow-dev@lists.jboss.org" target="_blank">undertow-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" target="_blank">https://lists.jboss.org/<span style="text-decoration:underline"></span>mailma<u></u>n/listinfo/undertow-dev</a><br>
<br>
--<br>
Jason T. Greene<br>
WildFly Lead / JBoss EAP Platform Architect<br>
JBoss, a division of Red Hat<br>
<br>
<br>
______________________________<span style="text-decoration:underline"></span><u></u>_________________<br>
undertow-dev mailing list<br>
<a href="mailto:undertow-dev@lists.jboss.org" target="_blank">undertow-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" target="_blank">https://lists.jboss.org/<span style="text-decoration:underline"></span>mailma<u></u>n/listinfo/undertow-dev</a></blockquote></div>
<br>______________________________<u></u>_________________<br>undertow-dev mailing list<br><a href="mailto:undertow-dev@lists.jboss.org" target="_blank">undertow-dev@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" target="_blank">https://lists.jboss.org/<u></u>mailman/listinfo/undertow-dev</a></blockquote></div></div>______________________________<u></u><u></u>_________________<br>
undertow-dev mailing list<br>
<a href="mailto:undertow-dev@lists.jboss.org" target="_blank">undertow-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" target="_blank">https://lists.jboss.org/<u></u>mailma<u></u>n/listinfo/undertow-dev</a></blockquote></div></blockquote></div>