How to set timeout in Spring WebServiceTemplate

妖精的绣舞 提交于 2019-12-28 05:35:19

问题


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


回答1:


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>



回答2:


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

@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);

     }
 }



回答3:


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)




回答4:


If you want that kind of control, you can

  • either switch to CommonsHttpMessageSender, which uses the Jakarta Commons HttpClient
  • or subclass HttpUrlConnectionMessageSender and in the prepareConnection(HttpURLConnection) method call UrlConnection.setReadTimeOut(int)



回答5:


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



回答6:


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



回答7:


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

}



回答8:


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>



回答9:


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


来源:https://stackoverflow.com/questions/6733744/how-to-set-timeout-in-spring-webservicetemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!