开发者

Making persistent connections using spring-ws client

开发者 https://www.devze.com 2023-03-26 22:45 出处:网络
I am using a spring-ws client to invoke a web service in two way ssl mode. I need to make sure that I don\'t end up creating a new connection everytime - instead I re-use connections.

I am using a spring-ws client to invoke a web service in two way ssl mode. I need to make sure that I don't end up creating a new connection everytime - instead I re-use connections. I did some re-search and found that by default HTTP 1.1 always makes persistent http(s) connections. Is that true?

Do I need any piece of code in my client to ensure connections are persistent? How can I check if the connections are persistent or if a new connection is being created vereytime I send a new request ?

<b开发者_开发知识库eans 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>


HTTP/1.1 has connection keep-alive but the server can decide to close it after a number of requests or does not support keep-alive

The easiest way to check is to use tcpdump or wireshark and check for SYN [S] flags. Thats new connections.

I am using the following and on J2SE 1.6 jdk it did not create any new sockets across multiple requests. The server did not send the Connection: Keep-Alive header but connections were kept alive anyway.

Bean configuration:

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


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

Java Code:

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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消