Total Pageviews

2019/02/05

[Spring] org.springframework.ws.client.WebServiceIOException: I/O error: Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out

Problem
When I try to call web service, I get this SocketTimeoutException exception:
org.springframework.ws.client.WebServiceIOException: I/O error: Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:561)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:383)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:373)
    at gov.npa.antifraud.agent.web.service.BankSchedulerService.sendBankDataRequest(BankSchedulerService.java:211)
    at gov.npa.antifraud.agent.web.WebAgentApplication.lambda$0(WebAgentApplication.java:22)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
    at gov.npa.antifraud.agent.web.WebAgentApplication.main(WebAgentApplication.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at org.springframework.ws.transport.http.HttpUrlConnection.getResponseCode(HttpUrlConnection.java:143)
    at org.springframework.ws.transport.http.AbstractHttpSenderConnection.hasError(AbstractHttpSenderConnection.java:53)
    at org.springframework.ws.client.core.WebServiceTemplate.hasError(WebServiceTemplate.java:673)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:607)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)
    ... 16 more


How to extend timed out value?


The Web Service client configuration is:
package demo.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.HttpComponentsMessageSender;

@Configuration
public class WsClientConfig {
    
    private String defaultURI = "http://localhost:8080/services";
    
    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        // ignored implementation details        
        return jaxb2Marshaller;
    }
    
    @Bean
    public WebServiceTemplate webServiceTemplate() {
        WebServiceTemplate wsTemplate = new WebServiceTemplate();
        wsTemplate.setMarshaller(jaxb2Marshaller());
        wsTemplate.setUnmarshaller(jaxb2Marshaller());
        wsTemplate.setDefaultUri(defaultURI);
        
        return wsTemplate;
    }
}



How-To
The default values to connection timeout and read timeout in spring framework is 1 minute.



If I would like to extend to 2 minutes, the WsClientConfig should be modified as bellows:
package demo.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.HttpComponentsMessageSender;

@Configuration
public class WsClientConfig {
    
    private String defaultURI = "http://localhost:8080/services";
    
    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        // ignored implementation details        
        return jaxb2Marshaller;
    }
    
    @Bean
    public WebServiceTemplate webServiceTemplate() {
        WebServiceTemplate wsTemplate = new WebServiceTemplate();
        wsTemplate.setMarshaller(jaxb2Marshaller());
        wsTemplate.setUnmarshaller(jaxb2Marshaller());
        wsTemplate.setDefaultUri(defaultURI);
        wsTemplate.setMessageSender(webServiceMessageSender());
        
        return wsTemplate;
    }
    
    @Bean
    public WebServiceMessageSender webServiceMessageSender() {
      HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
      // timeout for creating a connection
      httpComponentsMessageSender.setConnectionTimeout(120 * 1000);
      // when you have a connection, timeout the read blocks for
      httpComponentsMessageSender.setReadTimeout(120 * 1000);

      return httpComponentsMessageSender;
    }
}



No comments: