Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
等待异步调用首先完成,然后在Java中继续_Java_Asynchronous_Synchronous - Fatal编程技术网

等待异步调用首先完成,然后在Java中继续

等待异步调用首先完成,然后在Java中继续,java,asynchronous,synchronous,Java,Asynchronous,Synchronous,在这种情况下,我调用一个方法,该方法在继续之前触发一个异步HTTP REST调用(稍后将状态发送到另一个端点)。我希望该方法等待,直到我将响应返回到端点,检查我获得的状态并继续。我正在寻找一个可行的Java解决方案。任何伪代码或实现都会有所帮助 看到了类似的情况@,但对于相同的情况是否易于实现,没有多少想法 实施细节 Class ProcessData{ public void updateData() checktStatus(uri); update();// shou

在这种情况下,我调用一个方法,该方法在继续之前触发一个异步HTTP REST调用(稍后将状态发送到另一个端点)。我希望该方法等待,直到我将响应返回到端点,检查我获得的状态并继续。我正在寻找一个可行的Java解决方案。任何伪代码或实现都会有所帮助

看到了类似的情况@,但对于相同的情况是否易于实现,没有多少想法

实施细节

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);
  }

}
ProcessData pd = new ProcessData()
pd.updateData();
我有JAX-RS端点来处理异步响应,如下所示

@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 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);
  }

}
ProcessData pd = new ProcessData()
pd.updateData();
来自另一个类的方法调用

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);
  }

}
ProcessData pd = new ProcessData()
pd.updateData();

正如在您提供的链接中一样,您必须实现一种方法来跟踪有多少异步调用仍在等待响应,并等待该计数为零


count = numAsyncCalls
..calling all of the RESTful services here (each call must have some sort of callback to decrement 'count' variable above..
while (count > 0)
   wait around
在链接中使用CountDownLatch看起来与我的伪代码几乎相同

使用一个

一种同步辅助工具,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成


您这边的一些代码也是welcome@Bozho,添加了iam计划实施的代码@jerluc,我还没有研究过java中的回调,对于我的上述实现,我们如何实现它,或者从processConfigStatus()REST处理程序中,我可能需要调用ProcessData类中的方法来给出状态。