2

I am getting SocketException while calling a POST API However, My Client machine is under VPC (Virtual Private Cloud) however the server is in Open Cloud (Without any firewall).

When we call this API, we get the exception within a millisecond

My client configuration is as below

request.timeout.in.ms=10000
connection.timeout.in.ms=10000
socket.timeout.in.ms=600000
max.total.connection=100
max.per.route=100

Note: This exception occurs intermediately (Not all the time)

Please help me with it

Below is the stack trace


    org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://xxxxxxxx:8080/resources/internal/xxx/xxx/xxx": 
    Connection reset; nested exception is java.net.SocketException: Connection reset
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
        at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:407)
        at com.my.org.ext.client.RestClient.aggregate(RestClient.java:78)
        at com.my.org.ext.service.PExternalServiceImpl.aggregate(PExternalServiceImpl.java:164)
        at sun.reflect.GeneratedMethodAccessor270.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
        at com.sun.proxy.$Proxy145.aggregate(Unknown Source)
        at com.my.org.ext.service.ExternalServiceImpl.receiveMessage(ExternalServiceImpl.java:534)
        at sun.reflect.GeneratedMethodAccessor265.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:180)
        at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:112)
        at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48)
        at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:174)
        at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:72)
        at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:47)
        at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:764)
        at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:708)
        at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:544)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:210)
        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:138)
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
        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:165)
        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:272)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
        at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:89)
        at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
        at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652)
        ... 28 common frames omitted,

I went through What's causing my java.net.SocketException: Connection reset? But I am not sure what exactly the problem is and how can I solve it.

MishraJi
  • 304
  • 2
  • 6
  • 18
  • did you manage to find an answer to this? we're having this occur intermittently. thanks – S-- Nov 07 '18 at 10:19
  • 4
    Yes, While creating HttpClient for RestTemplate bean, I have added evictIdeleConnection property and it worked, I haven't received this exception anymore. HttpClients.custom().setDefaultRequestConfig(requestConfig) .setConnectionManager(connManager) .evictIdleConnections(IDLE_CONNECTION_WAIT_TIME, TimeUnit.SECONDS).build(); – MishraJi Nov 14 '18 at 06:35
  • thanks, i'll try it out. – S-- Nov 14 '18 at 10:17

2 Answers2

2

Connection reset can happen for a lot of reasons. But my suggestion is to try connection eviction for idle connections. When you call with stale connection connection reset can happen either from server or client.

mystery
  • 875
  • 6
  • 16
0

It seems some network issues. First check connectivity without java telnet YOUR_IP 8080 or use curl curl YOUR_IP:8080

Yuriy Zhigulskiy
  • 1,382
  • 9
  • 11
  • If network issue would be there then none of the requests will get Succeed, however, in my case some of the requests are facing this issue most of them got to succeed. – MishraJi Apr 27 '18 at 08:50