java SocketException:连接重置

时间:2016-10-04 21:12:29

标签: java spring rest sockets network-programming

从第三方api请求数据时,我收到以下异常。问题很少发生,但仍然是一个严重的损失。有关如何预防它的任何建议都会有所帮助。

这是堆栈跟踪。

transaction-load: <11>147500407977748914 transaction-load com.mycompany.nmi.client.NmiApiRestTemplateImpl[]: getTransactions() failed: http request failed: Connection reset
com.mycompany.nmi.client.rest.RestException: http request failed: Connection reset

Caused by: java.net.SocketException: Connection reset
    java.net.SocketInputStream.read(SocketInputStream.java:196)
    java.net.SocketInputStream.read(SocketInputStream.java:122)
    sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
    sun.security.ssl.InputRecord.readV3Record(InputRecord.java:554)
    sun.security.ssl.InputRecord.read(InputRecord.java:509)
    sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:944)
    sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1342)
    sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1369)
    sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1353)
    sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)
    org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
    org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
    com.mycompany.nmi.client.rest.RestClientServiceSpringImpl.getRequest(RestClientServiceSpringImpl.java:96)    
    com.mycompany.nmi.client.rest.RestClientServiceSpringImpl.getRequest(RestClientServiceSpringImpl.java:156)
    com.mycompany.nmi.client.NmiApiRestTemplateImpl.getTransactionsList(NmiApiRestTemplateImpl.java:158)
    com.mycompany.nmi.client.request.GetTransactionsListRequest.execute(GetTransactionsListRequest.java:172)
    com.mycompany.transactionload.stl.reader.SystemTransactionLoadReader.loadTransactions(SystemTransactionLoadReader.java:528)
    com.mycompany.transactionload.stl.reader.SystemTransactionLoadReader.init(SystemTransactionLoadReader.java:263)
    com.mycompany.transactionload.stl.reader.SystemTransactionLoadReader.read(SystemTransactionLoadReader.java:468)
    com.mycompany.transactionload.stl.reader.SystemTransactionLoadReader.read(SystemTransactionLoadReader.java:61)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)
    org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    com.sun.proxy.$Proxy16.read(Unknown Source)
    org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
    org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:155)
    org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:114)
    org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
    org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
    org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:108)
    org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
    org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:395)
    org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
    org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:267)
    org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
    org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
    org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
    org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:253)
    org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
    org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:139)
    org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:136)
    java.util.concurrent.FutureTask.run(FutureTask.java:262)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

当连接的一端决定(或被迫)突然结束连接时,会出现这种情况。这可能来自完全不受您控制的事情,例如服务器崩溃。

你无法100%阻止这种情况,所以你需要构建逻辑来处理它(例如,重新连接并重新发送)。