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

发布于 2024-11-29 11:30:19 字数 1570 浏览 6 评论 0原文

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

我的客户端中是否需要任何代码来确保连接持久?如何在发送新请求时检查连接是否持久或者是否正在创建新连接?

<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>

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 ?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2024-12-06 11:30:19

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文