2011-08-10 44 views
1

我正在使用spring-ws客戶端以雙向ssl模式調用Web服務。我需要確保每次都不會創建新的連接 - 而是重新使用連接。 我做了一些重新搜索,發現默認情況下,HTTP 1.1始終會持久化http(s)連接。真的嗎?使用spring-ws客戶端進行持久連接

我需要在我的客戶端的任何代碼段,以確保連接是持久的嗎?我如何檢查連接是否持久或是否正在創建新連接vereytime我發送新請求?

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> 

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/> 
    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"> 
     <constructor-arg ref="messageFactory"/> 
     <property name="marshaller" ref="jaxb2Marshaller" /> 
     <property name="unmarshaller" ref="jaxb2Marshaller" /> 
     <property name="messageSender" ref="httpSender" /> 
    </bean> 

    <bean id="httpSender" class="org.springframework.ws.transport.http.CommonsHttpMessageSender"> 
    </bean> 

    <oxm:jaxb2-marshaller id="jaxb2Marshaller" contextPath="com.nordstrom.direct.oms.webservices.addressval" /> 

    <bean id="Client" class="test.ClientStub"> 
      <property name="webServiceTemplate" ref="webServiceTemplate" />  
    </bean> 
</beans> 

回答

1

HTTP/1.1具有連接保持,但服務器可以決定請求的數目後,將其關閉或不支持保持活動

最簡單的檢查方法是使用tcpdump的或者Wireshark的並檢查SYN [S]標誌。那是新的連接。

我正在使用以下和J2SE 1.6 jdk它沒有創建任何跨多個請求的新套接字。服務器沒有發送Connection:Keep-Alive標頭,但連接仍然保持活動狀態。

bean配置

<bean id="wsHttpClient" factory-bean="customHttpClient" 
    factory-method="getHttpClient" /> 


<bean id="messageSender" 
     class="org.springframework.ws.transport.http.CommonsHttpMessageSender" 
     p:httpClient-ref="wsHttpClient" /> 

Java代碼的

import org.apache.commons.httpclient.*; 

import org.springframework.stereotype.*; 

@Component public class CustomHttpClient { 

    private final HttpClient httpClient; 

    public CustomHttpClient() { 
     httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); 
     httpClient.getParams().setParameter("http.protocol.content-charset", 
      "UTF-8"); 
     httpClient.getHttpConnectionManager().getParams() 
      .setConnectionTimeout(60000); 
     httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000); 
     httpClient.setHostConfiguration(new HostConfiguration()); 
    } 

    public HttpClient getHttpClient() { 
     return httpClient; 
    } 
}