当我尝试从我的响应中读取响应时,我收到许多Socket Read Timout 发布请求。我正在使用具有以下配置的OKHttpClient版本3.11.0:
@Bean
OkHttpClient okHttpClient() {
def loggingInterceptor = new HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
Dispatcher dispatcher = new Dispatcher()
dispatcher.setMaxRequests(200)
dispatcher.setMaxRequestsPerHost(200)
return new OkHttpClient()
.newBuilder()
.eventListenerFactory(PrintingEventListener.FACTORY)
.retryOnConnectionFailure(true)
.connectTimeout(30000, TimeUnit.MILLISECONDS)
.readTimeout(30000, TimeUnit.MILLISECONDS)
.dispatcher(dispatcher)
.connectionPool(new ConnectionPool(200, 30, TimeUnit.SECONDS))
.addNetworkInterceptor(loggingInterceptor)
.addInterceptor(loggingInterceptor).build()
}
下面给出了处理请求和响应的代码:
Response r = client.newCall(request).execute()
r.withCloseable { response ->
def returnable
if (response.header('Content-Type')?.contains('application/json')) {
def body = response.body().string()
if (body.trim().isEmpty()) {
returnable = [:]
} else {
try {
def result = new JsonSlurper().parseText(body)
return result
} catch (any) {
log.error('Failed to parse json response: ' + body, any.message)
throw any
}
}
} else if (response.header('Content-Type')?.contains('image/jpeg')) {
returnable = response.body().bytes()
} else {
returnable = response.body().string()
}
return returnable
}
我添加了事件侦听器,我看到httpclient调用responseBodyStart事件并挂在那里,当达到超时秒数时,调用失败并抛出Socket Read超时异常。我的配置中缺少任何内容吗?
事件侦听器显示 responseHeaderStart或responseBodyStart,后跟连接是在达到指定的超时时间(30s)后释放?
请在下面找到事件跟踪和异常跟踪:
INFO 11097 : 2.1701E-5 -- callStart
INFO 11097 : 1.44572E-4 -- connectionAcquired
INFO 11097 : 0.001036047 -- requestHeadersStart
INFO 11097 : 0.001064492 -- requestHeadersEnd
INFO 11097 : 0.001084433 -- requestBodyStart
INFO 11097 : 0.001103787 -- requestBodyEnd
INFO 11097 : 0.001279736 -- responseHeadersStart
INFO 11097 : 1.007175496 -- responseHeadersEnd
INFO 11097 : 1.007247928 -- responseBodyStart
INFO 11097 : 31.082725087 -- connectionReleased
INFO 11097 : 31.083717147 -- callFailed
INFO 11097 : 31.092341876 --
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 sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at okio.Okio$2.read(Okio.java:140)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
at okio.RealBufferedSource.request(RealBufferedSource.java:68)
at okio.RealBufferedSource.require(RealBufferedSource.java:61)
at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.java:304)
at okhttp3.internal.http1.Http1Codec$ChunkedSource.readChunkSize(Http1Codec.java:469)
at okhttp3.internal.http1.Http1Codec$ChunkedSource.read(Http1Codec.java:449)
at okio.RealBufferedSource.request(RealBufferedSource.java:68)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:241)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:213)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at okhttp3.Call$execute.call(Unknown Source)