cpprototypes
cpprototypes

Reputation: 23

waiting on asynchronous http requests in java

I'm using Jetty HTTP Client to make about 50 HTTP calls asynchronously. The code looks something like this:

List<Address> addresses = getAddresses();
final List<String> done = Collections.synchronizedList(new LinkedList<String>());
List<ContentExchange> requests;
for (Address address : addresses) {
    ContentExchange ce = new ContentExchange() {
        @Override
        protected void onResponseComplete() throws IOException {
            //handle response
            done.add("done");
        }
    }
    ce.setURL(createURL(address));
    requests.add(ce);
}
for (ContentExchange ce : requests) {
    httpClient.send(ce);
}

while (done.size() != addresses.size()) {
    Thread.yield();
}

System.out.println("All addresses processed");

It's calling a rest service that returns back some data about the address. What I expect it to do is this:

  1. Make 50 asynchronous (non-blocking) http calls.
  2. The thread will wait until all 50 are finished.

However, it's not working. It works fine if I don't have the while loop, but I need to wait until all 50 are done. Is there some way to wait until all 50 are done?

Also I know about ExecutorService and multiple thread solution, but I need a single thread solution with non-blocking IO.

Upvotes: 2

Views: 2737

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

Use the java.util.concurrent.CountDownLatch to manage this.

Example from Eclipse Jetty 8.1.10.v20130312's Siege.java test class:

final CountDownLatch latch = new CountDownLatch(concurrent);   

for (int i=0;i<concurrent;i++)
{
    ConcurrentExchange ex = new ConcurrentExchange(client,latch,uris,repeats);
    if (!ex.next()) // this executes the client.send()
    {
        latch.countDown(); // count down if client.send() was in error
    }
}

latch.await(); // wait for all ConcurrentExchange's to complete (or error out)

Note: ConcurrentExchange is a private class within Siege.java.

Then in your HttpExchange object, use the CountDownLatch.countDown() call in the following methods

Note that all of the examples use a AtomicBoolean counted to make sure that they are only counted once.

if (!counted.getAndSet(true)) // get the value, then set it to true
{
    // only get here if counted returned false. (and that will only happen once)
    latch.countDown(); // count down this exchange as being done.
}

Upvotes: 2

Related Questions