3

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

看到类似的案例@轻量级的等待一组异步Java调用的方式,但是对于是否容易实现没有太多想法。

实施细节

我有 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();
4

2 回答 2

4

使用CountDownLatch怎么样?

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

于 2012-09-17T23:56:12.793 回答
0

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


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 看起来与我的伪代码几乎相同

于 2011-02-12T18:27:46.987 回答