zoukankan      html  css  js  c++  java
  • springboot(五)设置Httpclient和Spring的整合

    设置HttpclientSpring的整合

    import org.apache.http.client.config.RequestConfig;

    import org.apache.http.impl.client.CloseableHttpClient;

    import org.apache.http.impl.client.HttpClients;

    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.beans.factory.annotation.Value;

    import org.springframework.context.annotation.Bean;

    import org.springframework.context.annotation.Configuration;

    import org.springframework.context.annotation.PropertySource;

    import org.springframework.context.annotation.Scope;

    import com.taotao.common.httpclient.IdleConnectionEvictor;

    @Configuration

    @PropertySource(value = "classpath:httpclient.properties")

    public class HttpclientSpringConfig {

        @Value("${http.maxTotal}")

        private Integer httpMaxTotal;

        @Value("${http.defaultMaxPerRoute}")

        private Integer httpDefaultMaxPerRoute;

        @Value("${http.connectTimeout}")

        private Integer httpConnectTimeout;

        @Value("${http.connectionRequestTimeout}")

        private Integer httpConnectionRequestTimeout;

        @Value("${http.socketTimeout}")

        private Integer httpSocketTimeout;

        @Value("${http.staleConnectionCheckEnabled}")

        private Boolean httpStaleConnectionCheckEnabled;

        @Autowired

        private PoolingHttpClientConnectionManager manager;

        @Bean

        public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {

            PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();

            // 最大连接数

            poolingHttpClientConnectionManager.setMaxTotal(httpMaxTotal);

            // 每个主机的最大并发数

            poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpDefaultMaxPerRoute);

            return poolingHttpClientConnectionManager;

        }

        // 定期关闭无效连接

        @Bean

        public IdleConnectionEvictor idleConnectionEvictor() {

            return new IdleConnectionEvictor(manager);

        }

        // 定义Httpclient

        @Bean

        @Scope("prototype")

        public CloseableHttpClient closeableHttpClient() {

            return HttpClients.custom().setConnectionManager(this.manager).build();

        }

        // 请求配置

        @Bean

        public RequestConfig requestConfig() {

            return RequestConfig.custom().setConnectTimeout(httpConnectTimeout) // 创建连接的最长时间

                    .setConnectionRequestTimeout(httpConnectionRequestTimeout) // 从连接池中获取到连接的最长时间

                    .setSocketTimeout(httpSocketTimeout) // 数据传输的最长时间

                    .setStaleConnectionCheckEnabled(httpStaleConnectionCheckEnabled) // 提交请求前测试连接是否可用

                    .build();

        }

    }

    本人开发小白,所写随笔有转发、有心得、随笔、所见问题、或者感觉不错的东西,希望能帮助他人,同时也相当给自己方便!(未来及标明出处,望原作者以及读者见谅海涵!一切为了能解决问题。。。。)
  • 相关阅读:
    python实现二分查找算法例子代码
    Python实现计算圆周率π的值到任意位的方法示例
    什么是TWS耳机
    [置顶] 单例模式lua实现
    推荐五星级C语言学习网站
    poj2689 Prime Distance 有难度 埃拉托斯尼斯筛法的运用
    局域网
    pager-taglib分页处理的使用
    创建ListView的基本步骤
    求最大连续子数列和(只扫描一次数列)
  • 原文地址:https://www.cnblogs.com/chengjiao/p/9633612.html
Copyright © 2011-2022 走看看