Pr0pagate
Pr0pagate

Reputation: 199

How To Know All Asynchronous HTTP Calls are Completed

I am trying to figure out how to determine if all async HTTP GET requests I've made have completed, so that I can execute another method. For context, I have something similar to the code below:

public void init() throws IOException {
    Map<String, CustomObject> mapOfObjects = new HashMap<String, CustomObject>();
    ObjectMapper mapper = new ObjectMapper();

    // some code to populate the map

    mapOfObjects.forEach((k,v) -> {
        HttpClient.asyncGet("https://fakeurl1.com/item/" + k, createCustomCallbackOne(k, mapper)); 
        // HttpClient is just a wrapper class for your standard OkHTTP3 calls, 
        // e.g. client.newcall(request).enqueue(callback);
        HttpClient.asyncGet("https://fakeurl2.com/item/" + k, createCustomCallbackTwo(k, mapper));
    });
}


private createCustomCallbackOne(String id, ObjectMapper mapper) {
    return new Callback() {
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                try (ResponseBody body = response.body()) {
                    CustomObject co = mapOfObjects.get(id);
                    if (co != null) {
                        co.setFieldOne(mapper.readValue(body.byteStream(), FieldOne.class))); 
                    }                       
                } // implicitly closes the response body                
            }    
        }

        @Override
        public void onFailure(Call call, IOException e) {
            // log error                
        }
    }
}

// createCustomCallbackTwo does more or less the same thing, 
// just sets a different field and then performs another 
// async GET in order to set an additional field

So what would be the best/correct way to monitor all these asynchronous calls to ensure they have completed and I can go about performing another method on the Objects stored inside the map?

Upvotes: 1

Views: 804

Answers (2)

JimmyB
JimmyB

Reputation: 12610

The most simple way would be to keep a count of how many requests are 'in flight'. Increment it for each request enqueued, decrement it at the end of the callback. When/if the count is 0, any/all requests are done. Using a semaphore or counting lock you can wait for it to become 0 without polling.

Note that the callbacks run on separate threads, so you must provide some kind of synchronization.

If you want to create a new callback for every request, you could use something like this:

public class WaitableCallback implements Callback {

  private boolean done;
  private IOException exception;

  private final Object[] signal = new Object[0];

  @Override
  public void onResponse(Call call, Response response) throws IOException {
      ...
      synchronized (this.signal) {
        done = true;
        signal.notifyAll();
      }
  }

  @Override
  public void onFailure(Call call, IOException e) {
    synchronized (signal) {
      done = true;
      exception = e;
      signal.notifyAll();
    }
  }

  public void waitUntilDone() throws InterruptedException {
    synchronized (this.signal) {
      while (!this.done) {
        this.signal.wait();
      }
    }
  }

  public boolean isDone() {
    synchronized (this.signal) {
      return this.done;
    }
  }

  public IOException getException() {
    synchronized (this.signal) {
      return exception;
    }
  }

}

Create an instance for every request and put it into e.g. a List<WaitableCallback> pendingRequests.

Then you can just wait for all requests to be done:

for ( WaitableCallback cb : pendingRequests ) {
  cb.waitUntilDone();
}
// At this point, all requests have been processed.

However, you probably should not create a new identical callback object for every request. Callback's methods get the Call passed as parameter so that the code can examine it to figure out which request it is processing; and in your case, it seems you don't even need that. So use a single Callback instance for the requests that should be handled identically.

Upvotes: 2

tgkprog
tgkprog

Reputation: 4598

If the function asyncGet calls your function createCustomCallbackOne then its easy.

For each key you are calling two pages. "https://fakeurl1.com/item/" and "https://fakeurl2.com/item/" (left out + k)

So you need a map to trach that and just one call back function is enough.

Use a map with key indicating each call:

  static final  Map<String, Integer> trackerOfAsyncCalls = new HashMap<>();
  public void init() throws IOException {
    Map<String, CustomObject> mapOfObjects = new HashMap<String, CustomObject>();
//need to keep a track of the keys in some object
ObjectMapper mapper = new ObjectMapper();
 trackerOfAsyncCalls.clear();
// some code to populate the map

mapOfObjects.forEach((k,v) -> {
    HttpClient.asyncGet("https://fakeurl1.com/item/" + k, createCustomCallback(k,1 , mapper)); 

    // HttpClient is just a wrapper class for your standard OkHTTP3 calls, 
    // e.g. client.newcall(request).enqueue(callback);
    HttpClient.asyncGet("https://fakeurl2.com/item/" + k, createCustomCallback(k, 2, mapper));
           trackerOfAsyncCalls.put(k + "-2", null);
        });
}

//final important private createCustomCallbackOne(final String idOuter, int which, ObjectMapper mapper) { return new Callback() { final String myId = idOuter + "-" + which;

    trackerOfAsyncCalls.put(myId, null);
    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if (response.isSuccessful()) {
             trackerOfAsyncCalls.put(myId, 1);
            ///or put outside of if if u dont care if success or fail or partial...

Now set up a thread or best a schduler that is caclled every 5 seconds, check all eys in mapOfObjects and trackerOfAsyncCalls to see if all keys have been started and some final success or timeout or error status has been got for all.

Upvotes: 1

Related Questions