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 (将#修改为@)