Java 异步 REST 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21035132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:06:04  来源:igfitidea点击:

Asynchronous REST client

javaspringrestasynchronousresttemplate

提问by user1335163

How to write asynchronousREST client?

如何编写异步REST客户端?

My controller(not sure if it's enough for being async):

我的控制器(不确定它是否足够async):

@RequestMapping(method = RequestMethod.GET, value = "/get/all")
@ResponseBody
public Callable < CustomersListDTO > getAllCustomers() {
    return new Callable < CustomersListDTO > () {

        @Override
        public CustomersListDTO call() throws Exception {
            Thread.sleep(2000);
            return customerService.getAllCustomers();
        }

    };
}


My synchronous REST clientmethod:


我的同步 REST客户端方法:

public Response get_all_customers() {
    ResponseEntity < CustomersListDTO > response;
    try {
        response = restTemplate.getForEntity(
            getMethodURI(ServiceExplanation.GET_ALL_CUSTOMERS),
            CustomersListDTO.class
        );
        message = "Customers obtained successfully!";
    } catch (HttpServerErrorException ex) {
        message = "ERROR: " + ex.getMessage() + " - " + ex.getResponseBodyAsString();
    } catch (HttpClientErrorException ex) {
        message = "ERROR: " + ex.getMessage() + " - " + ex.getResponseBodyAsString();
    } catch (RestClientException ex) {
        message = checkIfServerOrInternetDown();
    }

    return formResponse(message, response);
}


How do I make it asynchronous? How can the CLIENT continue doing other tasks while SERVER is obtaining data and later return found data?


我如何使它异步?CLIENT 如何在 SERVER 获取数据并随后返回找到的数据时继续执行其他任务?

回答by Nguyen Le

I would suggest adding support for groovy in your application so you can use AsyncHTTPBuilder which is what you are trying to do. It basically uses FutureTask under the cover.

我建议在您的应用程序中添加对 groovy 的支持,以便您可以使用 AsyncHTTPBuilder,这正是您想要做的。它基本上是在幕后使用 FutureTask。

回答by tonga

If you are looking for REST asynchronous client implementation, you can take a look at Jersey's asynchronous client API. It can be easily integrated with Spring.

如果您正在寻找 REST 异步客户端实现,您可以查看 Jersey 的异步客户端 API。它可以很容易地与 Spring 集成。

回答by jeffpeiyt

Check Ning' async http client: https://github.com/AsyncHttpClient/async-http-client

检查Ning的异步http客户端:https://github.com/AsyncHttpClient/async-http-client

also eBay' REST commander for parallel rest async client ready to use: http://www.restcommander.com/

还有 eBay 的 REST 指挥官,可用于并行休息异步客户端:http: //www.restcommander.com/