Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for an asynchronous call to complete first and then proceed in Java

I have a situation wherein I call a method which in turn triggers a asynchronous HTTP REST call(sends the status later to another endpoint) before it proceeds further. I want the method to wait until i get the response back to the endpoint, check the status i got and proceed further. I am looking for a feasible solution in Java. Any pseudo code or implementation will be helpful

saw similar case @ Lightweight way of waiting for a group of asynchronous Java calls but not much idea about the same whether it is easy to implement.

Implementation details

I have JAX-RS endpoint to handle the async response as below

@POST
@Path("/status")
@Consumes("application/xml")
public Response processConfigStatus(@Context UriInfo uriInfo, ConfigStatus configStatus)
{
   // process Status got from the async REST call
   return ResponseHelper.createNoContentResponse();
}

Class which handles and processes

Class ProcessData{

  public void updateData()
    checktStatus(uri);
    update();// should wait untill the processConfigStatus() endpoint gives status
  }

  private  checktStatus(String uri){
     // makes a REST call to a URI using a JAX-RS webclient or a httpclient this returns HTTP 200 or 204 code immediatley. And then the Server process the request asynchronously and gives back the status to JAX-RS endpoint(/status).
     post(uri);
  }

}

Method call from another Class

ProcessData pd = new ProcessData()
pd.updateData();
like image 948
ravi Avatar asked Oct 14 '25 06:10

ravi


1 Answers

How about using a CountDownLatch?

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

like image 84
mxro Avatar answered Oct 16 '25 18:10

mxro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!