2011-02-12 59 views
2

我有一种情况,其中我称这反过来触发异步HTTP REST调用(后发送状态到另一个端点)它进一步前进之前的方法。我想的方法,等到我得到响应返回给端点,检查我得到了状态,并进一步进行。我正在寻找一种可行的Java解决方案。任何伪代码或实施将有助于等待异步调用完成第一,然后继续在Java中

看到了类似的情况@差不多Lightweight way of waiting for a group of asynchronous Java calls但没有太多的想法是否很容易实现。

实现细节

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

和一些代码也欢迎您 – Bozho 2011-02-12 18:24:55

回答

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看起来几乎一样的我的伪代码

+0

@Bozho,加入了我计划实施的代码。 @jerluc,我还没有在回调中的Java工作,为实施以上矿井,我们怎么能有这样或从processConfigStatus()REST处理程序,我可能需要调用一个方法的过程数据类给状态。 – ravi 2011-02-12 19:21:09

4

如何使用CountDownLatch

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