如何在 Spring WebServiceTemplate 中设置超时

发布于 2024-11-25 08:54:46 字数 260 浏览 2 评论 0原文

我正在使用 org.springframework.ws.client.core.WebServiceTemplate 用于进行 Web 服务调用。我如何配置呼叫超时。

I am using org.springframework.ws.client.core.WebServiceTemplate for making Web Service calls. How can i configure timeout for the call.

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

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

发布评论

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

评论(9

傲世九天 2024-12-02 08:54:46

如果您使用的是 Spring Webservices 2.1.0 版本,您可以使用 HttpComponentsMessageSender

CommonsHttpMessageSender 已弃用并且 Spring 不再推荐。

我的实现方式是将 WebServiceTemplate 定义为使用 HttpComponentsMessageSender。

值以毫秒为单位

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="defaultUri" value="${endpoint.url}" />
    <property name="marshaller" ref="marshaller" />
    <property name="unmarshaller" ref="unmarshaller" />
    <property name="messageSender">
        <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
            <property name="connectionTimeout" value="1200000" />
            <property name="readTimeout" value="1200000" />
        </bean>
    </property>
</bean>

只​​要确保您的 pom 文件中添加了以下内容

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.1</version>
        <scope>compile</scope>
    </dependency>

If you are using Spring Webservices 2.1.0 version, You can set timeout using HttpComponentsMessageSender.

CommonsHttpMessageSender are deprecated and not recommended by Spring anymore.

The way I have it implemented, I define my WebServiceTemplate to use HttpComponentsMessageSender.

Values are in Milliseconds

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="defaultUri" value="${endpoint.url}" />
    <property name="marshaller" ref="marshaller" />
    <property name="unmarshaller" ref="unmarshaller" />
    <property name="messageSender">
        <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
            <property name="connectionTimeout" value="1200000" />
            <property name="readTimeout" value="1200000" />
        </bean>
    </property>
</bean>

Just Make sure you have in your pom file, you added the following

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.1</version>
        <scope>compile</scope>
    </dependency>
抹茶夏天i‖ 2024-12-02 08:54:46

与 Sathish 答案相同,但以编程方式(Spring 4+):

@Component
public class MyWebServiceGatewaySupport extends WebServiceGatewaySupport
{
    @Value("${my.ws.readtimeout}")
    private String readTimeout;

    @Value("${my.ws.connectiontimeout}")
    private String connectionTimeout;

    Object marshalSendAndReceive(String endpoint, Object requestPayload)
    {

        WebServiceTemplate wsTemplate = this.getWebServiceTemplate();
        WebServiceMessageSender[] senders = wsTemplate.getMessageSenders();
        for (WebServiceMessageSender sender: senders)
        {
            try
            {
                int readTimeoutMsec = Integer.parseInt(readTimeout);
                int connTimeoutMsec = Integer.parseInt(connectionTimeout);
                HttpComponentsMessageSender httpSender = (HttpComponentsMessageSender) sender;
                httpSender.setReadTimeout(readTimeoutMsec);
                httpSender.setConnectionTimeout(connTimeoutMsec);
            }
            catch (ClassCastException|NumberFormatException cex)
            {
                 logger.warn("Cannot set WS timeout: " + cex.getMessage());
            }
        }

        return wsTemplate.marshalSendAndReceive(endpoint, requestPayload);

     }
 }

Same as Sathish answer, but programmatically (Spring 4+):

@Component
public class MyWebServiceGatewaySupport extends WebServiceGatewaySupport
{
    @Value("${my.ws.readtimeout}")
    private String readTimeout;

    @Value("${my.ws.connectiontimeout}")
    private String connectionTimeout;

    Object marshalSendAndReceive(String endpoint, Object requestPayload)
    {

        WebServiceTemplate wsTemplate = this.getWebServiceTemplate();
        WebServiceMessageSender[] senders = wsTemplate.getMessageSenders();
        for (WebServiceMessageSender sender: senders)
        {
            try
            {
                int readTimeoutMsec = Integer.parseInt(readTimeout);
                int connTimeoutMsec = Integer.parseInt(connectionTimeout);
                HttpComponentsMessageSender httpSender = (HttpComponentsMessageSender) sender;
                httpSender.setReadTimeout(readTimeoutMsec);
                httpSender.setConnectionTimeout(connTimeoutMsec);
            }
            catch (ClassCastException|NumberFormatException cex)
            {
                 logger.warn("Cannot set WS timeout: " + cex.getMessage());
            }
        }

        return wsTemplate.marshalSendAndReceive(endpoint, requestPayload);

     }
 }
云淡风轻 2024-12-02 08:54:46

从 Spring Webservices 2.2 开始,您还可以使用 Spring 的 ClientHttpRequestMessageSender:(

@Service
public class CustomWebServiceImpl extends WebServiceGatewaySupport implements CustomWebService {
    private static final int CONNECTION_TIMEOUT = (10 * 1000);
    private static final int READ_TIMEOUT = (10 * 1000);

    public CustomWebServiceImpl() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(CONNECTION_TIMEOUT);
        requestFactory.setReadTimeout(READ_TIMEOUT);
        setMessageSender(new ClientHttpRequestMessageSender(requestFactory));
    }

    (...)
}

不需要依赖 Apache HTTP 组件)

Since Spring Webservices 2.2, you can also use Spring's ClientHttpRequestMessageSender:

@Service
public class CustomWebServiceImpl extends WebServiceGatewaySupport implements CustomWebService {
    private static final int CONNECTION_TIMEOUT = (10 * 1000);
    private static final int READ_TIMEOUT = (10 * 1000);

    public CustomWebServiceImpl() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(CONNECTION_TIMEOUT);
        requestFactory.setReadTimeout(READ_TIMEOUT);
        setMessageSender(new ClientHttpRequestMessageSender(requestFactory));
    }

    (...)
}

(no dependency to Apache HTTP Components required)

天冷不及心凉 2024-12-02 08:54:46

下面的代码对我有用。

@Bean
public YourClassImpl yourClassImpl(Jaxb2Marshaller marshaller, HttpComponentsMessageSender  httpComponentsMessageSender) {
    YourClassImpl client = new YourClassImpl();
    client.setDefaultUri(PiiProperties.SOAP_ACTION.getValue());
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    client.setMessageSender(httpComponentsMessageSender);
    return client;
}

@Bean
public HttpComponentsMessageSender httpComponentsMessageSender() {     
    HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
    sender.setReadTimeout(1000);
    sender.setConnectionTimeout(1000);
    return sender;
}

The below code worked for me.

@Bean
public YourClassImpl yourClassImpl(Jaxb2Marshaller marshaller, HttpComponentsMessageSender  httpComponentsMessageSender) {
    YourClassImpl client = new YourClassImpl();
    client.setDefaultUri(PiiProperties.SOAP_ACTION.getValue());
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    client.setMessageSender(httpComponentsMessageSender);
    return client;
}

@Bean
public HttpComponentsMessageSender httpComponentsMessageSender() {     
    HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
    sender.setReadTimeout(1000);
    sender.setConnectionTimeout(1000);
    return sender;
}
花开半夏魅人心 2024-12-02 08:54:46

我就是这样做的:

@Configuration
public class MunisServiceConfig {

    @Value("${service.uri}")
    private String soapUri;

    @Bean
    Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath(CheckStatePayment.class.getPackage().getName());

        return jaxb2Marshaller;
    }

    @Bean
    public WebServiceTemplate munisService() {
        WebServiceTemplate template = new WebServiceTemplate();

        template.setMarshaller(jaxb2Marshaller());
        template.setUnmarshaller(jaxb2Marshaller());

        template.setDefaultUri(soapUri);

        HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
        httpComponentsMessageSender.setReadTimeout(3000);
        httpComponentsMessageSender.setConnectionTimeout(5000);
        template.setMessageSender(httpComponentsMessageSender);

        return template;
    }

}

That's how I did:

@Configuration
public class MunisServiceConfig {

    @Value("${service.uri}")
    private String soapUri;

    @Bean
    Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath(CheckStatePayment.class.getPackage().getName());

        return jaxb2Marshaller;
    }

    @Bean
    public WebServiceTemplate munisService() {
        WebServiceTemplate template = new WebServiceTemplate();

        template.setMarshaller(jaxb2Marshaller());
        template.setUnmarshaller(jaxb2Marshaller());

        template.setDefaultUri(soapUri);

        HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
        httpComponentsMessageSender.setReadTimeout(3000);
        httpComponentsMessageSender.setConnectionTimeout(5000);
        template.setMessageSender(httpComponentsMessageSender);

        return template;
    }

}
许一世地老天荒 2024-12-02 08:54:46

这篇文章可能会让你明白:
http://onebyteatatime。 wordpress.com/2009/03/19/how-to-set-socket-timeout-using-spring-webservicetemplate/

我实现它的方式,我定义了我的WebServiceTemplate 使用 CommonsHttpMessageSender:

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="messageFactory" />
        <property name="messageSender">
            <bean
                class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
            </bean>
        </property>
</bean>

然后,在代码中,我获取 messageSender 并为其设置超时。您同样可以在 xml 中执行此操作。

CommonsHttpMessageSender messageSender = (CommonsHttpMessageSender)webServiceTemplate.getMessageSenders()[0];
messageSender.getHttpClient().getParams().setSoTimeout(timeoutMillis);

This article will probably sort you out:
http://onebyteatatime.wordpress.com/2009/03/19/how-to-set-socket-timeout-using-spring-webservicetemplate/

The way I have it implemented, I define my WebServiceTemplate to use CommonsHttpMessageSender:

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="messageFactory" />
        <property name="messageSender">
            <bean
                class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
            </bean>
        </property>
</bean>

Then, in code, I get the messageSender and set the timeout on it. You could equally do this in your xml.

CommonsHttpMessageSender messageSender = (CommonsHttpMessageSender)webServiceTemplate.getMessageSenders()[0];
messageSender.getHttpClient().getParams().setSoTimeout(timeoutMillis);
缪败 2024-12-02 08:54:46

此代码适用于 Spring Boot(已在 2.1.5.RELEASE 上验证):

@Configuration
public class ExampleServiceClientConfiguration {

    @Value("${example-service.uri}")
    private String exampleServiceUri;

    @Value("${example-service.timeout:120}")
    private int exampleServiceTimeout;

    @Bean
    public ExampleServiceClient exampleServiceClient() {
        ExampleServiceClient client = new ExampleServiceClient();
        client.setMessageSender(httpUrlConnectionMessageSender());
        client.setDefaultUri(exampleServiceUri);
        client.setMarshaller(marshaller());
        client.setUnmarshaller(marshaller());
        return client;
    }

    @Bean
    HttpUrlConnectionMessageSender httpUrlConnectionMessageSender() {
        HttpUrlConnectionMessageSender sender = new HttpUrlConnectionMessageSender();
        Duration timeout = Duration.ofSeconds(exampleServiceTimeout);
        sender.setReadTimeout(timeout);
        sender.setConnectionTimeout(timeout);
        return sender;
    }

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath(ObjectFactory.class.getPackageName());
        return marshaller;
    }
}

This code works with Spring Boot (verified on 2.1.5.RELEASE):

@Configuration
public class ExampleServiceClientConfiguration {

    @Value("${example-service.uri}")
    private String exampleServiceUri;

    @Value("${example-service.timeout:120}")
    private int exampleServiceTimeout;

    @Bean
    public ExampleServiceClient exampleServiceClient() {
        ExampleServiceClient client = new ExampleServiceClient();
        client.setMessageSender(httpUrlConnectionMessageSender());
        client.setDefaultUri(exampleServiceUri);
        client.setMarshaller(marshaller());
        client.setUnmarshaller(marshaller());
        return client;
    }

    @Bean
    HttpUrlConnectionMessageSender httpUrlConnectionMessageSender() {
        HttpUrlConnectionMessageSender sender = new HttpUrlConnectionMessageSender();
        Duration timeout = Duration.ofSeconds(exampleServiceTimeout);
        sender.setReadTimeout(timeout);
        sender.setConnectionTimeout(timeout);
        return sender;
    }

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath(ObjectFactory.class.getPackageName());
        return marshaller;
    }
}
倦话 2024-12-02 08:54:46

对于CommonsHttpMessageSender,我们可以通过以下方式设置超时:

<bean id="httpParams" class="org.apache.commons.httpclient.params.HttpClientParams">
  <!-- Timeout in milliseconds: in this case 1 minute -->
  <property name="soTimeout" value="60000" />
</bean>

<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <property name="params" ref="httpParams" />
</bean>

<!-- Define the message sender used by all web service templates -->
<bean id="webServiceMessageSender" class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
    <constructor-arg>
        <ref bean="httpClient"/>
    </constructor-arg>
</bean>

并引用webServiceMessageSender,如下所示:

<bean id="genericWebServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
       <property name="messageSender" ref="webServiceMessageSender"/>
</bean>

For the CommonsHttpMessageSender, we can set the timeout in the following way:

<bean id="httpParams" class="org.apache.commons.httpclient.params.HttpClientParams">
  <!-- Timeout in milliseconds: in this case 1 minute -->
  <property name="soTimeout" value="60000" />
</bean>

<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <property name="params" ref="httpParams" />
</bean>

<!-- Define the message sender used by all web service templates -->
<bean id="webServiceMessageSender" class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
    <constructor-arg>
        <ref bean="httpClient"/>
    </constructor-arg>
</bean>

and ref the webServiceMessageSender as below:

<bean id="genericWebServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
       <property name="messageSender" ref="webServiceMessageSender"/>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文