1. ホーム
  2. java

SocketTimeoutExceptionです。読み込みがタイムアウトしました

2022-02-08 04:37:11

例外エラーは以下の通りです。

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
    at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
    at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:282)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at com.forestar.xht.util.HttpClientUtils.getHttpClientResult(HttpClientUtils.java:305)
    at com.forestar.xht.util.HttpClientUtils.doGet(HttpClientUtils.java:103)
    at com.forestar.xht.util.HttpClientUtils.doGet(HttpClientUtils.java:59)
    at com.forestar.webservice.manager.MobileHlyPositionManager.getHlyTrackListByUserId(MobileHlyPositionManager.java:169)
    at com.forestar.xht.ssjk.RealTimeMonitor.getTrackListCache(RealTimeMonitor.java:85)
    at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter .java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)

この問題は、ネットワークリードデータのタイムアウトであり、以下の方法で解決されます。

  • a.タイムアウト時間を長くする
  • b.タイムアウト設定を削除する
  • c.例外印刷が投げられない
public class HttpClientUtils {
	
	 // Encoding format. Send encoding format unified with UTF-8
    private static final String ENCODING = "UTF-8";
    
    // Set the connection timeout in milliseconds.
    private static final int CONNECT_TIMEOUT = 6000;
    
    // Timeout for requesting data (i.e. response time), in milliseconds.
    private static final int SOCKET_TIMEOUT = 10000;

 /**
         * setConnectTimeout: Sets the connection timeout in milliseconds.
         * setConnectionRequestTimeout: Sets the connection request timeout from the connection manager (connection pool).
         * timeout, in milliseconds. This property is a new addition, as the current version is able to share connection pools.
         * setSocketTimeout: the timeout (i.e. response time) for requesting data, in milliseconds. If an interface is accessed and the data cannot be returned within how much time, the call is simply dropped.
         */
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();

取得元:https://my.oschina.net/boonya/blog/3046914