`
hanqunfeng
  • 浏览: 1527634 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring http invoker 高级篇

阅读更多

默认情况下,客户端的HttpInvokerProxy使用J2SE的HTTP Client来建立连接,即org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor,可以通过设置httpInvokerRequestExecutor属性来改变默认配置,spring提供了另外一种HttpClient,org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor。

修改配置如下:

<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://vm.netqin.com:4080/ucs/service/httpService</value>
</property>
<property name="serviceInterface" value="com.netqin.baike.service.UcService">
</property>
<property name="httpInvokerRequestExecutor">
<bean
class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor" />
</property>
</bean>

 

 

 

<bean  id="userService"  
        class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
    <!-- 服务端地址 -->
    <property  name="serviceUrl"  value="${HttpInvokerServer}/WYQ/user.service" />  
    <property   name="serviceInterface"  
        value="com.wyq.service.TestHttpInvokerInterface" />  
    <!-- 若用默认可不定义此属性 -->
    <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor"></property>
    </bean>  
    
<!--  单线程
    <bean  id="httpInvokerRequestExecutor" 
class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">  
        <property  name="httpClient">  
            <bean  class="org.apache.commons.httpclient.HttpClient">  
                <property   name="connectionTimeout"  value="2000" />  
                <property  name="timeout"  value="5000" />  
            </bean>  
        </property>  
    </bean>-->

 

 

<bean  id="httpInvokerRequestExecutor" 
class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">  
        <property name="httpClient" ref="httpclient" /> 
    </bean>
    <!--  HttpClient 启用Apache HttpClient 通信 
        默认实现,服务器平均10s左右才能响应一个请求。 
        多线程实现,服务器平均20ms左右响应一个请求。 -->  
     <bean id="httpclient" class="org.apache.commons.httpclient.HttpClient">    
        <constructor-arg>    
            <ref bean="connectionManager"/>    
        </constructor-arg>     
    </bean>  
    
     <!-- http管理参数配置 -->  
    <bean id="connectionManager" 
class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">    
        <property name="params" ref="connectionManagerParams"/>    
    </bean>   
      
    <!-- httpclient线程池 -->    
    <bean id="connectionManagerParams" 
class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">  
        <!-- 设置 连接超时时间(毫秒),默认为0不使用 -->    
        <property name="connectionTimeout" value="5000"/>   
        <!-- 设置 读取数据超时(毫秒),默认为0不使用 -->   
        <property name="soTimeout" value="10000"/>    
        <!-- 设置默认的连接数量最大允许对一个给定的主机配置 -->  
        <property name="maxTotalConnections" value="30"/>  
        <!-- 允许的最大连接数 -->    
        <property name="defaultMaxConnectionsPerHost" value="20"/>    
    </bean>   
</beans>

需要在项目中引入两个jar包:

commons-codec-x.x.jar

commons-httpclient-x.x.x.jar

 

 

参考:

http://www.cnblogs.com/johnason/archive/2012/07/18/2597064.html

http://jer-gavin.iteye.com/blog/1490505

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics