<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">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 class="Apple-tab-span" style="white-space:pre"> </span>private Response result;</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>private AsyncHttpClient.BoundRequestBuilder httpRequestBuilder;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span></div><div><span class="Apple-tab-span" style="white-space:pre">     </span>public HttpTask(AsyncHttpClient.BoundRequestBuilder builder) {</div><div><span class="Apple-tab-span" style="white-space:pre">               </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 class="Apple-tab-span" style="white-space:pre">    </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">    </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>protected boolean exec() {</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>try {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>//this is non blocking event-style http call</div><div><span class="Apple-tab-span" style="white-space:pre">                 </span>httpRequestBuilder.execute(new AsyncCompletionHandler<Response>() {</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                             </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">                            </span>public Response onCompleted(Response resp) throws Exception {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                      </span>//when the response is received complete the ForkJoinTask</div><div><span class="Apple-tab-span" style="white-space:pre">                                    </span>HttpTask.this.complete(resp);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                      </span>return resp;</div><div><span class="Apple-tab-span" style="white-space:pre">                         </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                            </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                              </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">                            </span>public void onThrowable(Throwable t) {</div><div><span class="Apple-tab-span" style="white-space:pre">                                       </span>//wehn exception complete Exceptionally the ForkJoinTask</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                      </span>HttpTask.this.completeExceptionally(t);</div><div><span class="Apple-tab-span" style="white-space:pre">                              </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                            </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>});</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>} catch (IOException e) {</div><div><span class="Apple-tab-span" style="white-space:pre">                    </span>throw new RuntimeException(e);</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>return false;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">     </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>public Response getRawResult() {</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>return result;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">   </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>protected void setRawResult(Response arg0) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>result = arg0;</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">    </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 class="Apple-tab-span" style="white-space:pre">        </span>private ForkJoinPool forJoinPool = new ForkJoinPool();</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>private AsyncHttpClient httpClient = new AsyncHttpClient();</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>throws ServletException, IOException {</div><div><span class="Apple-tab-span" style="white-space:pre">               </span>final AsyncContext asyncCtxt = req.getAsyncContext();</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>RecursiveAction action = new RecursiveAction() {</div><div><span class="Apple-tab-span" style="white-space:pre">                     </span></div><div><span class="Apple-tab-span" style="white-space:pre">                     </span>@Override</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>protected void compute() {</div><div><span class="Apple-tab-span" style="white-space:pre">                           </span>//do the http request</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>HttpTask t = new HttpTask(httpClient.prepareGet("http//url.to.service/"));</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>    Response res = t.invoke();</div><div><span class="Apple-tab-span" style="white-space:pre">                               </span></div><div><span class="Apple-tab-span" style="white-space:pre">                     </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>};</div><div><span class="Apple-tab-span" style="white-space:pre">   </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>