Another prime usecase for composable futures:<div><br></div><div>//Warning pseudocode, not compiled</div><div><br></div><div>import akka.dispatch.{ Promise, Future }</div><div>def bridge(httpRequestBuilder: AsyncHttpClient.BoundRequestBuilder): Future[Response] = {</div>
<div><div style>  val p = Promise[Response]()</div><div style>  httpRequestBuilder.execute(new AsyncCompletionHandler[Response] {</div><div style><span style="white-space:pre-wrap">    override def</span> onCompleted(resp: Response): Response = p success resp</div>
<div style><span style="white-space:pre-wrap">    override def </span>onThrowable(t: Throwable): Unit = p failure t<span style="white-space:pre-wrap">      </span></div><div style>  })</div><div style>  p.future</div><div style>
}</div><div style><br></div><div style>in servlet...</div><div style><br></div><div style>bridge(yourRequestBuilder) map asyncCtx.write foreach { _ => asyncCtx.close }</div><div style><br></div><div style>Cheers,</div>
<div style>√</div><br><div class="gmail_quote">On Sun, May 6, 2012 at 11:58 PM, Christian Essl <span dir="ltr"><<a href="mailto:christianessl@googlemail.com" target="_blank">christianessl@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Sorry if this is a stupid question regarding fork-join pools, but I am not sure wheter the "lightweigth future" idea would also work async http calls. (And please also excuse my bad english)</div>
<div><br></div>
<div>Currently I use the ning asnyc-http-client (<a href="https://github.com/sonatype/async-http-client" target="_blank">https://github.com/sonatype/async-http-client</a>) in an event-driven callback style to do nonblocking http-calls from a servlet. </div>

<div><br></div><div>However the callback style gets a bit complicated when the program-logic gets complex so I wonder wheter I could use ForkJoinTask to transfer the callback style to non-thread-blocking futures. </div><div>

<br></div><div>The idea is to initialize the nonblocking HttpCall in ForkJoinTaks.exec() and when the callback receives the http-response it calls the ForkJoinTaks.complete() method.</div><div><br></div><div>Maybe someone can take a look at the following ForkJoinTask which wraps an HttpCall and say wheter it is a reasonable use of ForkJoinPool.</div>

<div><br></div><div>import com.ning.http.client.AsyncHttpClient;</div><div>import com.ning.http.client.AsyncCompletionHandler;</div><div>import com.ning.http.client.Response;</div><div><br></div><div>//The forkjoin task which wraps an async-http-call</div>

<div>class HttpTask extends ForkJoinTask<Response> {</div><div><br></div><div><span style="white-space:pre-wrap">     </span>private Response result;</div><div><span style="white-space:pre-wrap"> </span>private AsyncHttpClient.BoundRequestBuilder httpRequestBuilder;</div>

<div><span style="white-space:pre-wrap">  </span></div><div><span style="white-space:pre-wrap"> </span>public HttpTask(AsyncHttpClient.BoundRequestBuilder builder) {</div><div><span style="white-space:pre-wrap">           </span>//the httpRequestBuilder is setup up by the caller (setting the url etc)</div>

<div>        //and used by HttpTask to execute the http request</div><div>        this.httpRequestBuilder = builder;</div><div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span></div>

<div><span style="white-space:pre-wrap">  </span>@Override</div><div><span style="white-space:pre-wrap">        </span>protected boolean exec() {</div><div><span style="white-space:pre-wrap">               </span>try {</div>
<div><span style="white-space:pre-wrap">                  </span>//this is non blocking event-style http call</div><div><span style="white-space:pre-wrap">                     </span>httpRequestBuilder.execute(new AsyncCompletionHandler<Response>() {</div>

<div><br></div><div><span style="white-space:pre-wrap">                         </span>@Override</div><div><span style="white-space:pre-wrap">                                </span>public Response onCompleted(Response resp) throws Exception {</div>
<div><span style="white-space:pre-wrap">                                  </span>//when the response is received complete the ForkJoinTask</div><div><span style="white-space:pre-wrap">                                        </span>HttpTask.this.complete(resp);</div>
<div><span style="white-space:pre-wrap">                                  </span>return resp;</div><div><span style="white-space:pre-wrap">                             </span>}</div><div><span style="white-space:pre-wrap">                                </span></div>
<div><span style="white-space:pre-wrap">                          </span>@Override</div><div><span style="white-space:pre-wrap">                                </span>public void onThrowable(Throwable t) {</div><div><span style="white-space:pre-wrap">                                   </span>//wehn exception complete Exceptionally the ForkJoinTask</div>

<div><span style="white-space:pre-wrap">                                  </span>HttpTask.this.completeExceptionally(t);</div><div><span style="white-space:pre-wrap">                          </span>}</div><div><span style="white-space:pre-wrap">                                </span></div>
<div><span style="white-space:pre-wrap">                  </span>});</div><div><span style="white-space:pre-wrap">              </span>} catch (IOException e) {</div><div><span style="white-space:pre-wrap">                        </span>throw new RuntimeException(e);</div>

<div><span style="white-space:pre-wrap">          </span>}</div><div><span style="white-space:pre-wrap">                </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div>
<div><br></div><div><span style="white-space:pre-wrap"> </span>@Override</div><div><span style="white-space:pre-wrap">        </span>public Response getRawResult() {</div><div><span style="white-space:pre-wrap">         </span>return result;</div>

<div><span style="white-space:pre-wrap">  </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>@Override</div><div><span style="white-space:pre-wrap">        </span>protected void setRawResult(Response arg0) {</div>

<div><span style="white-space:pre-wrap">          </span>result = arg0;</div><div><span style="white-space:pre-wrap">   </span>}</div><div><span style="white-space:pre-wrap">        </span></div>
<div>}</div><div><br></div><div>I than use the HttpTask in Servelt like that:</div><div><br></div><div>public class FooServlet extends javax.servlet.http.HttpServlet {</div><div><br></div><div><span style="white-space:pre-wrap">    </span>private ForkJoinPool forJoinPool = new ForkJoinPool();</div>

<div><span style="white-space:pre-wrap">  </span>private AsyncHttpClient httpClient = new AsyncHttpClient();</div><div><span style="white-space:pre-wrap">      </span>@Override</div><div><span style="white-space:pre-wrap">        </span>protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)</div>

<div><span style="white-space:pre-wrap">                  </span>throws ServletException, IOException {</div><div><span style="white-space:pre-wrap">           </span>final AsyncContext asyncCtxt = req.getAsyncContext();</div>
<div><span style="white-space:pre-wrap">          </span>RecursiveAction action = new RecursiveAction() {</div><div><span style="white-space:pre-wrap">                 </span></div><div><span style="white-space:pre-wrap">                 </span>@Override</div>

<div><span style="white-space:pre-wrap">                  </span>protected void compute() {</div><div><span style="white-space:pre-wrap">                               </span>//do the http request</div><div><span style="white-space:pre-wrap">                            </span>HttpTask t = new HttpTask(httpClient.prepareGet("http//url.to.service/"));</div>

<div><span style="white-space:pre-wrap">                  </span>    Response res = t.invoke();</div><div><span style="white-space:pre-wrap">                         </span></div><div><span style="white-space:pre-wrap">                 </span>}</div>
<div><span style="white-space:pre-wrap">          </span>};</div><div><span style="white-space:pre-wrap">       </span>}</div><div>}</div><div><br></div><div>My assumption here is that when the HttpTask is invoked that the ForkJoinPool does not block the thread until the Response is received but rather takes another RecursiveAtion to process another request until the httpresponse arrives.</div>

<div><br></div><div>My question is wheter my assumptions are right and wheter this is a reasonable use of ForkJoinPool.</div><div><br></div><div>Thanks for any responses.</div><div><br></div>
<br>_______________________________________________<br>
Concurrency-interest mailing list<br>
<a href="mailto:Concurrency-interest@cs.oswego.edu">Concurrency-interest@cs.oswego.edu</a><br>
<a href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><span style="border-collapse:separate;color:rgb(0,0,0);font-family:Times;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;font-size:medium"><span style="font-family:arial;font-size:small"><span style="border-collapse:collapse;font-family:arial,sans-serif;font-size:13px">Viktor Klang<br>
<br>Akka Tech Lead</span><div><font face="arial, sans-serif"><span style="border-collapse:collapse"><a href="http://www.typesafe.com/" target="_blank">Typesafe</a><span> </span>- </span></font><span>The software stack for applications that scale</span><br>
<font face="arial, sans-serif"><span style="border-collapse:collapse"><br></span></font><font face="arial, sans-serif"><span style="border-collapse:collapse">Twitter: @viktorklang</span></font></div></span></span><br>
</div>