How to set timeout in Spring WebServiceTemplate

前端 未结 9 1030
一整个雨季
一整个雨季 2020-12-05 06:45

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

相关标签:
9条回答
  • 2020-12-05 07:02

    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>
    
    0 讨论(0)
  • 2020-12-05 07:06

    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;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 07:11

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 07:15

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 07:18

    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);
    
         }
     }
    
    0 讨论(0)
  • 2020-12-05 07:20

    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)
    0 讨论(0)
提交回复
热议问题