Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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 spring异步rest客户机协调少量调用_Java_Spring_Spring Web - Fatal编程技术网

Java spring异步rest客户机协调少量调用

Java spring异步rest客户机协调少量调用,java,spring,spring-web,Java,Spring,Spring Web,我在我的服务中遇到了以下问题:我正在构建对象X,但是为了构建它,我需要进行一些http调用,以获取填充它所需的所有数据(每个剩余部分填充对象的特定部分)为了保持高性能,我认为最好使调用异步,并在所有调用完成后将对象返回给调用方。看起来像这样 ListenableFuture<ResponseEntity<String>> future1 = asycTemp.exchange(url, method, requestEntity, responseType); futur

我在我的服务中遇到了以下问题:我正在构建对象X,但是为了构建它,我需要进行一些http调用,以获取填充它所需的所有数据(每个剩余部分填充对象的特定部分)为了保持高性能,我认为最好使调用异步,并在所有调用完成后将对象返回给调用方。看起来像这样

ListenableFuture<ResponseEntity<String>> future1 = asycTemp.exchange(url, method, requestEntity, responseType);
future1.addCallback({
    //process response and set fields
    complexObject.field1 = "PARSERD RESPONSE"
},{
    //in case of fail fill default or take some ather actions
})
listenablefuture1=asycTemp.exchange(url、方法、请求实体、响应类型);
future1.addCallback({
//进程响应和设置字段
complexObject.field1=“解析响应”
},{
//如果失败,请填写默认值或采取其他措施
})
我不知道如何等待所有功能完成。我想它们是解决这类问题的一些标准spring方法。提前感谢您的建议。弹簧版本-4.2.4.1版本 致以最诚挚的问候

改编自

本例仅请求Google和Microsoft主页。当在回调中收到响应时,并且我已经完成了处理,我减少了一个。我等待倒计时闩锁,“阻塞”当前线程,直到倒计时闩锁达到0

重要的是,如果调用失败或成功,则必须递减,因为必须按0才能继续使用该方法

改编自

本例仅请求Google和Microsoft主页。当在回调中收到响应时,并且我已经完成了处理,我减少了一个。我等待倒计时闩锁,“阻塞”当前线程,直到倒计时闩锁达到0

重要的是,如果调用失败或成功,则必须递减,因为必须按0才能继续使用该方法

public static void main(String[] args) throws Exception {
    String googleUrl = "http://www.google.com";
    String microsoftUrl = "http://www.microsoft.com";
    AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
    ListenableFuture<ResponseEntity<String>> googleFuture = asyncRestTemplate.exchange(googleUrl, HttpMethod.GET, null, String.class);
    ListenableFuture<ResponseEntity<String>> microsoftFuture = asyncRestTemplate.exchange(microsoftUrl, HttpMethod.GET, null, String.class);
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    ListenableFutureCallback<ResponseEntity<java.lang.String>> listenableFutureCallback = new ListenableFutureCallback<ResponseEntity<String>>() {

        public void onSuccess(ResponseEntity<String> stringResponseEntity) {
            System.out.println(String.format("[Thread %d] Status Code: %d. Body size: %d",
                    Thread.currentThread().getId(),
                    stringResponseEntity.getStatusCode().value(),
                    stringResponseEntity.getBody().length()
            ));
            countDownLatch.countDown();
        }

        public void onFailure(Throwable throwable) {
            System.err.println(throwable.getMessage());
            countDownLatch.countDown();
        }
    };
    googleFuture.addCallback(listenableFutureCallback);
    microsoftFuture.addCallback(listenableFutureCallback);
    System.out.println(String.format("[Thread %d] This line executed immediately.", Thread.currentThread().getId()));
    countDownLatch.await();
    System.out.println(String.format("[Thread %d] All responses received.", Thread.currentThread().getId()));

}
[Thread 1] This line executed immediately.
[Thread 14] Status Code: 200. Body size: 112654
[Thread 13] Status Code: 200. Body size: 19087
[Thread 1] All responses received.