使用spring-ws客户端建立持久连接

时间:2011-08-10 22:17:33

标签: https spring-ws connection

我使用spring-ws客户端以双向ssl模式调用Web服务。我需要确保每次都不会创建新连接 - 而是重新使用连接。 我做了一些重新搜索,发现默认情况下HTTP 1.1总是建立持久的http(s)连接。真的吗?

我的客户端是否需要任何代码才能确保连接是持久的?如何检查连接是否持久或是否正在创建新连接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 个答案:

答案 0 :(得分:1)

HTTP / 1.1具有连接保持活动但服务器可以决定在多次请求后关闭它或不支持keep-alive

最简单的检查方法是使用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;
    }
}