Skip to content

Multiple requests in parallel with AsyncHttpClient #98

@jprante

Description

@jprante

I need to connect to many servers in parallel with AsyncHttpClient.

What I think of is something like a collection of AsyncHttpClient.BoundRequestBuilder invoking in parallel. Imagine a HttpResult class as response.

Collection<AsyncHttpClient.BoundRequestBuilder> builders =  new LinkedList(...)
CompletionService<HttpResult> service  = new ExecutorCompletionService<HttpResult>();
List<Future<HttpResult>> futures  = new ArrayList<Future<HttpResult>>(builders.size());
try {
         for (Callable<HttpResult> b : builders)
             futures.add(service.submit(b));
         for (int i = 0; i < futures.size(); i++) {
             try {
                 HttpResult r = service.take().get();
                 if (r != null) {
                      ...
                 }
             } catch (ExecutionException ignore) {}
         }
     }
     finally {
         for (Future<HttpResult> f : futures)
             f.cancel(true);
     }

Right now, calling AsyncHttpClient.BoundRequestBuilder execute() now gives a single Future object. But I need to collect many Futures in the order as they return, so a take() method like the one in ExecutorCompletionService could process them.

How can I implement something like this with AsyncHttpClient? Any help is appreciated.

Thank for any hints!

Jörg

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions