Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jersey 使用Apache HTTP客户端的自定义重试处理程序?_Jersey_Jersey 2.0_Jersey Client - Fatal编程技术网

Jersey 使用Apache HTTP客户端的自定义重试处理程序?

Jersey 使用Apache HTTP客户端的自定义重试处理程序?,jersey,jersey-2.0,jersey-client,Jersey,Jersey 2.0,Jersey Client,简短版本: 使用时是否可以配置自定义重试处理程序 ApacheConnectorProvider+池客户端连接管理器?如果是,怎么做 长版本: 我正在使用Jersey 2.25.1,最近我从使用默认HTTP连接机制切换到Apache PoolghtTPClientConnectionManager,以提高我的自动测试框架的可伸缩性 它通常是有效的,但随着我的扩展,我偶尔会遇到“连接超时”。我相信ApacheHttpClient能够接受自定义重试处理程序,但我不知道如何使用Jersey配置自定义处

简短版本: 使用时是否可以配置自定义重试处理程序 ApacheConnectorProvider+池客户端连接管理器?如果是,怎么做

长版本: 我正在使用Jersey 2.25.1,最近我从使用默认HTTP连接机制切换到Apache PoolghtTPClientConnectionManager,以提高我的自动测试框架的可伸缩性

它通常是有效的,但随着我的扩展,我偶尔会遇到“连接超时”。我相信ApacheHttpClient能够接受自定义重试处理程序,但我不知道如何使用Jersey配置自定义处理程序。我在很多搜索尝试中都找不到任何例子

两年前有人问过类似的问题,但没有答案

以下是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(); 
但重试处理程序客户端属性设置不存在


谢谢你的指点

版本2.26中引入了对RetryHandler的支持。请参见
ApacheClientProperties.RETRY\u HANDLER

我知道这个问题很老了,但是在ApacheHttpClient的v3.x中,您可以在每个请求级别(PostMethod,现在是HttpPost)上设置重试处理程序,但是现在您似乎是在使用新的4.x构建器模式创建HttpClient时设置的。下面是一段代码片段,它使用池连接管理器和重试处理程序设置客户机,希望这能帮助其他人从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;
  }
}
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;
  }
}