newbie
newbie

Reputation: 24625

How to combine asynchronous HTTP requests responses to one?

I'm trying to make x amount of HTTP requests asynchronously. I looked questions Asynchronous IO in Java? and How do you create an asynchronous HTTP request in JAVA?. I found good library Asynchronous Http and WebSocket Client library for Java, but I don't understand how I can safely combine multiple results into one result. For example if I have following code:

AsyncHttpClient c = new AsyncHttpClient();
List<String> urls = getUrls();
List<MyResultObject> results = new ArrayList<>();
for(String url : urls)
{
    // Create asynchronous request
    Future<MyResultObject> f = c.prepareGet(url).execute(handler);

    // How can I add completed responses to my results list ???
}

How can I safely combine those results into List and continue when all requests have finished.

Upvotes: 1

Views: 2837

Answers (3)

JackWang
JackWang

Reputation: 311

if you want to combile several http request and get all the result. you can look at the code blow.

package ParallelTasks;

import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ParallelHttpRequest {
    //thread pool to execute http request task.
    static final ExecutorService businessRequestExecutor = Executors.newCachedThreadPool();


    public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<String> urlList = new ArrayList<String>();

        final CountDownLatch latch = new CountDownLatch(urlList.size());

        List<Future<Pair<String, String>>> list = new ArrayList<Future<Pair<String, String>>>();
        for (final String url : urlList) {
            Future<Pair<String, String>> future = businessRequestExecutor.submit(new Callable<Pair<String, String>>() {
                public Pair<String, String> call() throws Exception {
                    try {
                        //do post or get http request here.
                        //SoaHttpUtil.post(config.getUrl(), buReqJson);

                        String result = "";

                        return new MutablePair<String, String>(url, result);
                    } catch (Exception ex) {
                        System.out.println(ex);
                        return new MutablePair<String, String>(url, null);
                    } finally {
                        latch.countDown();
                    }
                }
            });
            list.add(future);
        }

        //wait no more than 5 seconds.
        latch.await(5000, TimeUnit.MILLISECONDS);

        //print finished request's result.
        for (Future<Pair<String, String>> future : list) {
            if (future.isDone()) {
                System.out.println(future.get().getValue());
            }
        }
    }
}

Upvotes: 0

Vincent van der Weele
Vincent van der Weele

Reputation: 13177

I found this tutorial for using futures. You could just do the following:

AsyncHttpClient c = new AsyncHttpClient();
List<String> urls = getUrls();
List<Future<MyResultObject>> futures = new ArrayList<>();  // keep track of your futures
List<MyResultObject> results = new ArrayList<>();
for(String url : urls)
{
    // Create asynchronous request
    Future<MyResultObject> f = c.prepareGet(url).execute(handler);
    futures.add(f);
}

// Now retrieve the result
for (Future<MyResultObject> future : futures) {
  try {
    results.add(future.get());
  } catch (InterruptedException e) {
    e.printStackTrace();
  } catch (ExecutionException e) {
    e.printStackTrace();
  }
}
// continue with your result list

Upvotes: 2

gerrytan
gerrytan

Reputation: 41123

You can call the get() method of Future class to obtain the result. Note that call to the method may block until result is available

Upvotes: 0

Related Questions