如何解决Apache HTTP客户端中的ExponentialBackOffSchedulingStrategy无法正常工作
它应根据配置重试,但重试时似乎ExponentialBackOffSchedulingStrategy没有任何作用。 任何想法将不胜感激。
代码示例:
import java.io.IOException
import org.apache.http
import org.apache.http.HttpResponseInterceptor
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler
import org.apache.http.impl.client.cache.CacheConfig
import org.apache.http.impl.client.cache.CachingHttpClientBuilder
import org.apache.http.impl.client.cache.ExponentialBackOffSchedulingStrategy
import org.apache.http.protocol.HttpContext
object BackOffApp extends App {
def createClient() = {
val hcb = CachingHttpClientBuilder.create()
val cc = CacheConfig.DEFAULT
val ebo = new ExponentialBackOffSchedulingStrategy(cc,2,1000,15000)
hcb.setRetryHandler(new DefaultHttpRequestRetryHandler(5,true))
hcb.addInterceptorLast(new HttpResponseInterceptor() {
override def process(response: http.HttpResponse,context: HttpContext): Unit = {
if (response.getStatusLine.getStatusCode != 200) {
throw new IOException("Retry")
}
}
})
hcb.setSchedulingStrategy(ebo)
hcb.build
}
val request = new HttpGet("https://www.example.com/test")
val client: CloseableHttpClient = createClient()
client.execute(request)
}
输出:
20/10/06 12:10:09 INFO RetryExec: I/O exception (java.io.IOException) caught when processing request to {s}->https://www.example.com:443: Retry
20/10/06 12:10:09 INFO RetryExec: retrying request to {s}->https://www.example.com:443
20/10/06 12:10:09 INFO RetryExec: I/O exception (java.io.IOException) caught when processing request to {s}->https://www.example.com:443: Retry
20/10/06 12:10:09 INFO RetryExec: retrying request to {s}->https://www.example.com:443
20/10/06 12:10:09 INFO RetryExec: I/O exception (java.io.IOException) caught when processing request to {s}->https://www.example.com:443: Retry
20/10/06 12:10:09 INFO RetryExec: retrying request to {s}->https://www.example.com:443
20/10/06 12:10:09 INFO RetryExec: I/O exception (java.io.IOException) caught when processing request to {s}->https://www.example.com:443: Retry
20/10/06 12:10:09 INFO RetryExec: retrying request to {s}->https://www.example.com:443
20/10/06 12:10:09 INFO RetryExec: I/O exception (java.io.IOException) caught when processing request to {s}->https://www.example.com:443: Retry
20/10/06 12:10:09 INFO RetryExec: retrying request to {s}->https://www.example.com:443
图书馆
compile group: 'org.apache.httpcomponents',name: 'httpclient',version: '4.5.12'
compile group: 'org.apache.httpcomponents',name: 'httpcore',version: '4.4.13'
compile group: 'org.apache.httpcomponents',name: 'httpclient-cache',version: '4.5.12'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。