如何有效地使用带有RestTemplate的HttpComponentsClientHttpRequestFactory? [英] How to use HttpComponentsClientHttpRequestFactory with RestTemplate efficiently?

查看:1918
本文介绍了如何有效地使用带有RestTemplate的HttpComponentsClientHttpRequestFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RestTemplate 及其工厂

I am using RestTemplate along with its factory HttpComponentsClientHttpRequestFactory in one of my projects. In this project, I need to make a Http url call to my server which is running a restful service which returns back the response as a JSON String.

下面是我的代码-

public class GetUserClientData {

    public String getData(KeyHolder keys) {
        return new HTTPRequestAccess(keys).makeHttpRequest();
    }
}

下面是我的类,其中包装了HttpClient部分-

Below is my class which wraps the HttpClient part -

public class HTTPRequestAccess {

    // should this be static?
    private RestTemplate restTemplate;
    private KeyHolder keys;
    private int timeout;

    public HTTPRequestAccess(KeyHolder keys){
        this(keys.getTimeoutValue()); // setting timeout to RestTemplate
        this.keys = keys;
    }

    public HTTPRequestAccess(int timeout) {
        this.timeout = timeout;
        restTemplate = new RestTemplate(clientHttpRequestFactory());
    }

    private ClientHttpRequestFactory clientHttpRequestFactory() {
        // is this not expensive that every time we are creating this new object?
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setReadTimeout(timeout);
        factory.setConnectTimeout(timeout);
        return factory;
    }

    public String makeHttpRequest() {
        String response = null;        
        try {
            // some logic here

            String url = generateURL();
            response = restTemplate.getForObject(url, String.class);

            // some logic here
        } catch (RestClientException ex) {
            // log exception and do some stuff
        } catch (Exception ex) {
            // log exception
        }

        return response;
    }
}

RestTemplateHttpComponentsClientHttpRequestFactory在我的HTTPRequestAccess类中应该是静态的,就像我正确看到的一样,我正在为RestTemplate中的每个请求重新创建整个连接池,这不是我猜的正确方法,因为每个工厂都有连接和线程池,我猜它们是很重的对象.

Should RestTemplate and HttpComponentsClientHttpRequestFactory be static here in my HTTPRequestAccess class as if I see it correctly, I am recreating the whole connection pool for each request in RestTemplate which is not the right way I guess because each factory has connection and thread pool and they are pretty heavy object I guess.

通常,在多线程环境中使用RestTemplate及其工厂HttpComponentsClientHttpRequestFactory的最佳方法是什么?我猜RestTemplate是线程安全的,但我不认为HttpComponentsClientHttpRequestFactory是线程安全的.如果我错了纠正我吗?我将在繁重的负载下运行该库.

In general what is the best way to use RestTemplate along with its factory HttpComponentsClientHttpRequestFactory in a multithreading environment? I guess RestTemplate is thread safe but I don't think HttpComponentsClientHttpRequestFactory is thread safe. Correct me if I am wrong? I will be running this library under heavy load.

我正在使用spring-web-3.2.8.RELEASE版本.

推荐答案

在我的一个项目中,我创建了HttpComponentsClientHttpRequestFactory的静态实例,并将其传递给每个RestTemplate. 不过,在此处中,建议也使用RestTemplate的全局实例

In one of my projects, I had created a static instance of HttpComponentsClientHttpRequestFactory and passed it to every RestTemplate. Though, in here, it is suggested to also have a global instance of RestTemplate.

也许无关紧要,但重要的一点是在构造它时将HttpClients.createDefault()传递给HttpComponentsClientHttpRequestFactory,因为默认情况下,该工厂使用系统属性为您的工厂创建HttpClient,这可能会给生产环境带来很多麻烦. 您也可以传递自定义的HttpClient.

Maybe irrelevant, but one important point is to pass HttpClients.createDefault() to your HttpComponentsClientHttpRequestFactory while constructing it since by default, this factory uses system properties to create HttpClient for your factory and that may cause a lot of pain in production environment. You may also pass your custom HttpClient.

这篇关于如何有效地使用带有RestTemplate的HttpComponentsClientHttpRequestFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆