zoukankan      html  css  js  c++  java
  • spring整合httpclient

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 定义连接管理器 -->
    <bean id="connectionManager"
      class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
      <!-- 最大连接数 -->
      <property name="maxTotal" value="${http.maxTotal}" />
      <property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
    </bean>

    <!-- 定义Httpclient构造器 -->
    <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
      <property name="connectionManager" ref="connectionManager" />
    </bean>

    <!-- 定义Httpclient对象 -->
    <bean class="org.apache.http.impl.client.CloseableHttpClient" factory-bean="httpClientBuilder" factory-method="build" scope="prototype" />

    <!-- 请求参数的构造器 -->
    <bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
      <!-- 创建连接的最长时间 -->
      <property name="connectTimeout" value="${http.connectTimeout}" />
      <!-- 从连接池中获取到连接的最长时间 -->
      <property name="connectionRequestTimeout" value="${http.connectionRequestTimeout}" />
      <!-- 数据传输的最长时间 -->
      <property name="socketTimeout" value="${http.socketTimeout}" />
      <!-- 提交请求前测试连接是否可用 -->
    <property name="staleConnectionCheckEnabled" value="${http.staleConnectionCheckEnabled}" />
    </bean>

    <!-- 定义请求参数 -->
    <bean class="org.apache.http.client.config.RequestConfig" factory-bean="requestConfigBuilder" factory-method="build" />

    <!-- 定期关闭无效连接 -->
    <bean class="com.taotao.web.httpclient.IdleConnectionEvictor">
      <constructor-arg index="0" ref="connectionManager" />
    </bean>

    </beans>

    spring整合的原则:以httpclient与spring整合为例

    httpclient与spring的整合步骤
    1,首先在相关项目或者模块的资源文件夹下创建一个xml文科件
    applicationContext-httpclient.xml,通常这个文件放在spring文件夹中(applicationContext这个拼错,不要问我为什么这样说!!!)
    2,整合原则:把什么样的那个托管给spring,让spring去管理
    3,写一个bean,看这个对象是怎么来的
    如果该对象是经过build()出来的,那么一般就表示是工厂出来的,需要配置工厂,比如下面这样
    <!-- 定义Httpclient对象 -->
    <bean class="org.apache.http.impl.client.CloseableHttpClient"
    factory-bean="httpClientBuilder" factory-method="build" scope="prototype" />
    4,如果spring中没有该对象.那么就需要定义一个该工厂对象,如果有参数,还要设置参数
    <!-- 定义Httpclient构造器 -->
    <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
    <property name="connectionManager" ref="connectionManager" />
    </bean>
    5,如果参数引入的对象,spring没有,就需要定义一个,PoolingHttpClientConnectionManager是new出来,因此就可以了
    <!-- 定义连接管理器 -->
    <bean id="connectionManager"
    class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
    <!-- 最大连接数 -->
    <property name="maxTotal" value="${http.maxTotal}" />
    <property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
    </bean>

  • 相关阅读:
    uestc1307 windy数 ——数位DP入门题
    2013年4月3日 小雨,阴
    hdu1202 The calculation of GPA ——水题
    zoj 3693 Happy Great BG
    hdu 2035 人见人爱A^B ——同余的简单性质
    zoj2913 Bus Pass ——BFS入门题
    一个bug,持续更新……
    zoj 3406 Another Very Easy Task
    poj 1995 Raising Modulo Numbers ——快速幂
    hdu 1059 Dividing ——多重背包复习
  • 原文地址:https://www.cnblogs.com/lizhaowen/p/5925576.html
Copyright © 2011-2022 走看看