【问题标题】:Custom retry handler with Apache HTTP client?使用 Apache HTTP 客户端自定义重试处理程序?
【发布时间】:2017-02-21 21:50:44
【问题描述】:

短版: 使用时是否可以配置自定义重试处理程序 ApacheConnectorProvider + PoolingHttpClientConnectionManager?如果有,怎么做?

加长版: 我正在使用 Jersey 2.25.1,最近我从使用默认 HTTP 连接机制切换到 Apache PoolingHttpClientConnectionManager 以提高我的自动化测试框架的扩展能力。

它通常可以正常工作,但随着我的扩展,我偶尔会遇到“连接超时”。我相信 Apache HTTPClient 有能力接受自定义重试处理程序,但我不知道如何使用 Jersey 配置自定义处理程序。在我的多次搜索尝试中找不到任何示例。

2 年前Here 有人问过类似的问题,但没有答案。

这是我的 Jersey 客户端的初始化代码:

    SSLContext ctx = initSSLTrustFactory();

    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE))
            .build();

    ClientConfig cfg = new ClientConfig();

    PoolingHttpClientConnectionManager connMan = new PoolingHttpClientConnectionManager(registry);
    connMan.setMaxTotal(200);
    connMan.setDefaultMaxPerRoute(50);

    if (timeout != -1) {
        SocketConfig sc = SocketConfig.custom()
                .setSoTimeout(timeout)
                .setSoReuseAddress(true)
                .setTcpNoDelay(true)
                .setSoReuseAddress(true)
                .build();
        connMan.setDefaultSocketConfig(sc);
        cfg.property(ApacheClientProperties.REQUEST_CONFIG, RequestConfig
                .custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout)
                .build()
        );
    }
    cfg.property(ApacheClientProperties.CONNECTION_MANAGER, connMan);

    ApacheConnectorProvider acp = new ApacheConnectorProvider();
    cfg.connectorProvider(acp);

    ClientBuilder builder = JerseyClientBuilder.newBuilder();
    client = builder
            .hostnameVerifier((String hostname, SSLSession session) -> true)
            .withConfig(cfg)
            .register(JacksonFeature.class)
            .register(MultiPartWriter.class)
            .build(); 

有一个示例 HERE 显示了在实际 Apache HTTPClient 上设置它的一种方法,但我没有看到在泽西岛做等效的方法,但我仍在尝试了解如何自定义泽西岛。

我原以为它是通过在 ClientConfig 实例中执行类似于以下操作来完成的:

cfg.property(ApacheClientProperties.RETRY_HANDLER, new HttpRequestRetryHandler() {

    public boolean retryRequest(
            IOException exception,
            int executionCount,
            HttpContext context) {
         ...
    }
  }
);

但重试处理程序客户端属性设置不存在。

感谢您的指点!

【问题讨论】:

    标签: jersey jersey-2.0 jersey-client


    【解决方案1】:

    在 2.26 版本中引入了对 RetryHandlers 的支持。见ApacheClientProperties.RETRY_HANDLERhere

    【讨论】:

      【解决方案2】:

      我知道这个问题已经很老了,但是在 Apache HttpClient 的 v 3.x 中,您可以在每个请求级别(PostMethod,现在是 HttpPost)设置重试处理程序,但是现在看来您设置了这个当您使用新的 4.x 构建器模式创建 HttpClient 时。这是一个使用池连接管理器和重试处理程序设置客户端的代码 sn-p,希望这有助于其他必须从 v3 迁移到 v4 的人。

      public class HttpClientFactory {
        private static final int MAX_TOTAL_CONNECTIONS = 60;
        public static final int REMOTE_SEARCH_TIMEOUT_DEFAULT = 180000;
      
        private int connectionTimeout = REMOTE_SEARCH_TIMEOUT_DEFAULT;
        private int maxTotalConnections = MAX_TOTAL_CONNECTIONS;
        private final SomeCustomRetryHandler somecustomretryhandler = new SomeCustomRetryHandler();
      
        public HttpClient createHttpClient() {
          RequestConfig config = RequestConfig.custom()
                  .setConnectTimeout(3000)
                  .setConnectionRequestTimeout(3000)
                  .setSocketTimeout(3000)
                  .build();
      
          return HttpClients.custom()
                  .setConnectionManager(createPoolingHttpConnectionManager())
                  .setDefaultRequestConfig(config)
                  .setRetryHandler(somecustomretryhandler)
                  .build();
        }
      
        private PoolingHttpClientConnectionManager createPoolingHttpConnectionManager() {
          PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
          cm.setDefaultMaxPerRoute(getMaxTotalConnections(6);
          cm.setMaxTotal(6);
          return cm;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多