资讯专栏INFORMATION COLUMN

apache的HttpClient的默认重试机制

MartinDai / 3423人阅读

摘要:异常重试默认重试次,三次都失败则抛出或其他异常

maven

</>复制代码

  1. org.apache.httpcomponents
  2. httpclient
  3. 4.5.2
异常重试log

</>复制代码

  1. 2017-01-31 19:31:39.057 INFO 3873 --- [askScheduler-13] o.apache.http.impl.execchain.RetryExec : I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://192.168.99.100:8080: The target server failed to respond
  2. 2017-01-31 19:31:39.058 INFO 3873 --- [askScheduler-13] o.apache.http.impl.execchain.RetryExec : Retrying request to {}->http://192.168.99.100:8080
RetryExec

org/apache/http/impl/execchain/RetryExec.java

</>复制代码

  1. /**
  2. * Request executor in the request execution chain that is responsible
  3. * for making a decision whether a request failed due to an I/O error
  4. * should be re-executed.
  5. *

  6. * Further responsibilities such as communication with the opposite
  7. * endpoint is delegated to the next executor in the request execution
  8. * chain.
  9. *

  10. *
  11. * @since 4.3
  12. */
  13. @Immutable
  14. public class RetryExec implements ClientExecChain {
  15. private final Log log = LogFactory.getLog(getClass());
  16. private final ClientExecChain requestExecutor;
  17. private final HttpRequestRetryHandler retryHandler;
  18. public RetryExec(
  19. final ClientExecChain requestExecutor,
  20. final HttpRequestRetryHandler retryHandler) {
  21. Args.notNull(requestExecutor, "HTTP request executor");
  22. Args.notNull(retryHandler, "HTTP request retry handler");
  23. this.requestExecutor = requestExecutor;
  24. this.retryHandler = retryHandler;
  25. }
  26. @Override
  27. public CloseableHttpResponse execute(
  28. final HttpRoute route,
  29. final HttpRequestWrapper request,
  30. final HttpClientContext context,
  31. final HttpExecutionAware execAware) throws IOException, HttpException {
  32. Args.notNull(route, "HTTP route");
  33. Args.notNull(request, "HTTP request");
  34. Args.notNull(context, "HTTP context");
  35. final Header[] origheaders = request.getAllHeaders();
  36. for (int execCount = 1;; execCount++) {
  37. try {
  38. return this.requestExecutor.execute(route, request, context, execAware);
  39. } catch (final IOException ex) {
  40. if (execAware != null && execAware.isAborted()) {
  41. this.log.debug("Request has been aborted");
  42. throw ex;
  43. }
  44. if (retryHandler.retryRequest(ex, execCount, context)) {
  45. if (this.log.isInfoEnabled()) {
  46. this.log.info("I/O exception ("+ ex.getClass().getName() +
  47. ") caught when processing request to "
  48. + route +
  49. ": "
  50. + ex.getMessage());
  51. }
  52. if (this.log.isDebugEnabled()) {
  53. this.log.debug(ex.getMessage(), ex);
  54. }
  55. if (!RequestEntityProxy.isRepeatable(request)) {
  56. this.log.debug("Cannot retry non-repeatable request");
  57. throw new NonRepeatableRequestException("Cannot retry request " +
  58. "with a non-repeatable request entity", ex);
  59. }
  60. request.setHeaders(origheaders);
  61. if (this.log.isInfoEnabled()) {
  62. this.log.info("Retrying request to " + route);
  63. }
  64. } else {
  65. if (ex instanceof NoHttpResponseException) {
  66. final NoHttpResponseException updatedex = new NoHttpResponseException(
  67. route.getTargetHost().toHostString() + " failed to respond");
  68. updatedex.setStackTrace(ex.getStackTrace());
  69. throw updatedex;
  70. } else {
  71. throw ex;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
DefaultHttpRequestRetryHandler

org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java

</>复制代码

  1. /**
  2. * The default {@link HttpRequestRetryHandler} used by request executors.
  3. *
  4. * @since 4.0
  5. */
  6. @Immutable
  7. public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
  8. public static final DefaultHttpRequestRetryHandler INSTANCE = new DefaultHttpRequestRetryHandler();
  9. /** the number of times a method will be retried */
  10. private final int retryCount;
  11. /** Whether or not methods that have successfully sent their request will be retried */
  12. private final boolean requestSentRetryEnabled;
  13. private final Set> nonRetriableClasses;
  14. /**
  15. * Create the request retry handler using the specified IOException classes
  16. *
  17. * @param retryCount how many times to retry; 0 means no retries
  18. * @param requestSentRetryEnabled true if it"s OK to retry requests that have been sent
  19. * @param clazzes the IOException types that should not be retried
  20. * @since 4.3
  21. */
  22. protected DefaultHttpRequestRetryHandler(
  23. final int retryCount,
  24. final boolean requestSentRetryEnabled,
  25. final Collection> clazzes) {
  26. super();
  27. this.retryCount = retryCount;
  28. this.requestSentRetryEnabled = requestSentRetryEnabled;
  29. this.nonRetriableClasses = new HashSet>();
  30. for (final Class clazz: clazzes) {
  31. this.nonRetriableClasses.add(clazz);
  32. }
  33. }
  34. /**
  35. * Create the request retry handler using the following list of
  36. * non-retriable IOException classes:
  37. *
    • *
    • InterruptedIOException
    • *
    • UnknownHostException
    • *
    • ConnectException
    • *
    • SSLException
    • *
  38. * @param retryCount how many times to retry; 0 means no retries
  39. * @param requestSentRetryEnabled true if it"s OK to retry non-idempotent requests that have been sent
  40. */
  41. @SuppressWarnings("unchecked")
  42. public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) {
  43. this(retryCount, requestSentRetryEnabled, Arrays.asList(
  44. InterruptedIOException.class,
  45. UnknownHostException.class,
  46. ConnectException.class,
  47. SSLException.class));
  48. }
  49. /**
  50. * Create the request retry handler with a retry count of 3, requestSentRetryEnabled false
  51. * and using the following list of non-retriable IOException classes:
  52. *
    • *
    • InterruptedIOException
    • *
    • UnknownHostException
    • *
    • ConnectException
    • *
    • SSLException
    • *
  53. */
  54. public DefaultHttpRequestRetryHandler() {
  55. this(3, false);
  56. }
  57. /**
  58. * Used {@code retryCount} and {@code requestSentRetryEnabled} to determine
  59. * if the given method should be retried.
  60. */
  61. @Override
  62. public boolean retryRequest(
  63. final IOException exception,
  64. final int executionCount,
  65. final HttpContext context) {
  66. Args.notNull(exception, "Exception parameter");
  67. Args.notNull(context, "HTTP context");
  68. if (executionCount > this.retryCount) {
  69. // Do not retry if over max retry count
  70. return false;
  71. }
  72. if (this.nonRetriableClasses.contains(exception.getClass())) {
  73. return false;
  74. } else {
  75. for (final Class rejectException : this.nonRetriableClasses) {
  76. if (rejectException.isInstance(exception)) {
  77. return false;
  78. }
  79. }
  80. }
  81. final HttpClientContext clientContext = HttpClientContext.adapt(context);
  82. final HttpRequest request = clientContext.getRequest();
  83. if(requestIsAborted(request)){
  84. return false;
  85. }
  86. if (handleAsIdempotent(request)) {
  87. // Retry if the request is considered idempotent
  88. return true;
  89. }
  90. if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
  91. // Retry if the request has not been sent fully or
  92. // if it"s OK to retry methods that have been sent
  93. return true;
  94. }
  95. // otherwise do not retry
  96. return false;
  97. }
  98. /**
  99. * @return {@code true} if this handler will retry methods that have
  100. * successfully sent their request, {@code false} otherwise
  101. */
  102. public boolean isRequestSentRetryEnabled() {
  103. return requestSentRetryEnabled;
  104. }
  105. /**
  106. * @return the maximum number of times a method will be retried
  107. */
  108. public int getRetryCount() {
  109. return retryCount;
  110. }
  111. /**
  112. * @since 4.2
  113. */
  114. protected boolean handleAsIdempotent(final HttpRequest request) {
  115. return !(request instanceof HttpEntityEnclosingRequest);
  116. }
  117. /**
  118. * @since 4.2
  119. *
  120. * @deprecated (4.3)
  121. */
  122. @Deprecated
  123. protected boolean requestIsAborted(final HttpRequest request) {
  124. HttpRequest req = request;
  125. if (request instanceof RequestWrapper) { // does not forward request to original
  126. req = ((RequestWrapper) request).getOriginal();
  127. }
  128. return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted());
  129. }
  130. }

</>复制代码

  1. 默认重试3次,三次都失败则抛出NoHttpResponseException或其他异常

doc

Apache HttpComponents

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66577.html

相关文章

  • 记一次线程挂死排查过程(附 HttpClient 配置建议)

    摘要:此时我想到了福尔摩斯说过的一句话当你排除掉各种不可能出现的情况之后,剩下的情况无论多么难以置信,都是真相。福尔摩斯冷静下来想一想,这个线程,有可能静悄悄地退出了吗,没留下半点异常日志从理论上来说,不可能。配置建议最后,附上一份配置建议。 1、事发 我们有个视频处理程序,基于 SpringBoot,会启动几个线程来跑。要退出程序时,会发送一个信号给程序,每个线程收到信号后会平滑退出,等全...

    jollywing 评论0 收藏0
  • Apache httpclientexecute方法调试

    摘要:因为工作需要,想研究一下执行的逻辑。在这一行调用的实现我在代码里声明的只是一个接口,实现类是。首先根据传入的请求决定出目标投递到执行。 因为工作需要,想研究一下execute执行的逻辑。 在这一行调用execute: response = getHttpClient().execute(get); getHttpClient的实现: private HttpClient getHttp...

    wudengzan 评论0 收藏0
  • 爬虫框架WebMagic源码分析之Downloader

    摘要:方法,首先判断是否有这是在中配置的,如果有,直接调用的将相应内容转化成对应编码字符串,否则智能检测响应内容的字符编码。 Downloader是负责请求url获取返回值(html、json、jsonp等)的一个组件。当然会同时处理POST重定向、Https验证、ip代理、判断失败重试等。 接口:Downloader 定义了download方法返回Page,定义了setThread方法来...

    104828720 评论0 收藏0
  • spring-cloud-feign源码深度解析

    摘要:内部使用了的动态代理为目标接口生成了一个动态代理类,这里会生成一个动态代理原理统一的方法拦截器,同时为接口的每个方法生成一个拦截器,并解析方法上的元数据,生成一个请求模板。的核心源码解析到此结束了,不知道是否对您有无帮助,可留言跟我交流。 Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插拔注释支持,包...

    vibiu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<