@Test public void testMaxRetry() { try (AsyncHttpClient ahc = asyncHttpClient(config().setMaxRequestRetry(0))) { ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build()).get(); fail(); } catch (Exception t) { assertEquals(t.getCause(), RemotelyClosedException.INSTANCE); } }
@Test(timeOut = 2000, expectedExceptions = SSLHandshakeException.class) public void failInstantlyIfNotAllowedSelfSignedCertificate() throws Throwable { logger.debug(">>> failInstantlyIfNotAllowedSelfSignedCertificate"); withClient(config().setMaxRequestRetry(0).setRequestTimeout(2000)).run(client -> withServer(server).run(server -> { try { client.prepareGet(getTargetUrl()).execute().get(TIMEOUT, SECONDS); } catch (ExecutionException e) { throw e.getCause().getCause(); } })); logger.debug("<<< failInstantlyIfNotAllowedSelfSignedCertificate"); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void deferredInputStreamTrickWithCloseConnectionAndRetry() throws Throwable { try (AsyncHttpClient client = asyncHttpClient(config().setMaxRequestRetry(1).setRequestTimeout(10000).build())) { BoundRequestBuilder r = client.prepareGet(getTargetUrl()).addHeader("X-CLOSE-CONNECTION", Boolean.TRUE.toString()); PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos); Future<Response> f = r.execute(bdah); BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis); Response resp = is.getAsapResponse(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE)); // "consume" the body, but our code needs input stream CountingOutputStream cos = new CountingOutputStream(); try { try { copy(is, cos); } finally { is.close(); cos.close(); } } catch (IOException e) { throw e.getCause(); } } }
.setConnectTimeout(connectTimeout).setReadTimeout(readTimeout) .setPooledConnectionIdleTimeout(pooledConnectionIdleTimeout).setRequestTimeout(requestTimeout) .setFollowRedirect(followRedirect).setMaxRedirects(maxRedirects).setMaxRequestRetry(maxRequestRetry) .setConnectionTtl(connectionTtl).build(); asyncHttpClient = new DefaultAsyncHttpClient(config);
.setConnectTimeout(connectTimeout).setReadTimeout(readTimeout) .setPooledConnectionIdleTimeout(pooledConnectionIdleTimeout).setRequestTimeout(requestTimeout) .setFollowRedirect(followRedirect).setMaxRedirects(maxRedirects).setMaxRequestRetry(maxRequestRetry) .setConnectionTtl(connectionTtl).build(); asyncHttpClient = new DefaultAsyncHttpClient(config);
.setMaxRequestRetry(0).setShutdownQuietPeriod(0).setShutdownTimeout(0).build();
/** * Create an instance from a given {@link BulletConfig}. Requires {@link DRPCConfig#DRPC_HTTP_CONNECT_TIMEOUT}, * {@link DRPCConfig#DRPC_HTTP_CONNECT_RETRY_LIMIT}, {@link DRPCConfig#DRPC_SERVERS}, {@link DRPCConfig#DRPC_HTTP_PORT}, * and {@link DRPCConfig#DRPC_HTTP_PATH} to be set. To be used in PubSub.Context#QUERY_SUBMISSION mode. * * @param config A non-null config that contains the required settings. */ public DRPCQueryResultPubscriber(BulletConfig config) { Objects.requireNonNull(config); Number connectTimeout = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_CONNECT_TIMEOUT, Number.class); Number retryLimit = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_CONNECT_RETRY_LIMIT, Number.class); List<String> servers = (List<String>) config.getRequiredConfigAs(DRPCConfig.DRPC_SERVERS, List.class); String protocol = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PROTOCOL, String.class); String port = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PORT, String.class); String path = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PATH, String.class); String function = config.getRequiredConfigAs(DRPCConfig.DRPC_FUNCTION, String.class); List<String> endpoints = servers.stream() .map(url -> String.format(URL_TEMPLATE, protocol, url, port, path, function)) .collect(Collectors.toList()); this.urls = new RandomPool<>(endpoints); AsyncHttpClientConfig clientConfig = new DefaultAsyncHttpClientConfig.Builder() .setConnectTimeout(connectTimeout.intValue()) .setMaxRequestRetry(retryLimit.intValue()) .setReadTimeout(NO_TIMEOUT) .setRequestTimeout(NO_TIMEOUT) .build(); // This is thread safe client = new DefaultAsyncHttpClient(clientConfig); responses = new ConcurrentLinkedQueue<>(); }
/** * Create an instance from a given {@link BulletConfig}. Requires {@link DRPCConfig#DRPC_HTTP_CONNECT_TIMEOUT}, * {@link DRPCConfig#DRPC_HTTP_CONNECT_RETRY_LIMIT}, {@link DRPCConfig#DRPC_SERVERS}, {@link DRPCConfig#DRPC_HTTP_PORT}, * and {@link DRPCConfig#DRPC_HTTP_PATH} to be set. To be used in PubSub.Context#QUERY_SUBMISSION mode. * * @param config A non-null config that contains the required settings. */ public DRPCQueryResultPubscriber(BulletConfig config) { Objects.requireNonNull(config); Number connectTimeout = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_CONNECT_TIMEOUT, Number.class); Number retryLimit = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_CONNECT_RETRY_LIMIT, Number.class); List<String> servers = (List<String>) config.getRequiredConfigAs(DRPCConfig.DRPC_SERVERS, List.class); String protocol = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PROTOCOL, String.class); String port = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PORT, String.class); String path = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PATH, String.class); String function = config.getRequiredConfigAs(DRPCConfig.DRPC_FUNCTION, String.class); List<String> endpoints = servers.stream() .map(url -> String.format(URL_TEMPLATE, protocol, url, port, path, function)) .collect(Collectors.toList()); this.urls = new RandomPool<>(endpoints); AsyncHttpClientConfig clientConfig = new DefaultAsyncHttpClientConfig.Builder() .setConnectTimeout(connectTimeout.intValue()) .setMaxRequestRetry(retryLimit.intValue()) .setReadTimeout(NO_TIMEOUT) .setRequestTimeout(NO_TIMEOUT) .build(); // This is thread safe client = new DefaultAsyncHttpClient(clientConfig); responses = new ConcurrentLinkedQueue<>(); }
@Test public void reconnectAfterFailedCertificationPath() throws Throwable { logger.debug(">>> reconnectAfterFailedCertificationPath"); AtomicBoolean trust = new AtomicBoolean(); withClient(config().setMaxRequestRetry(0).setSslEngineFactory(createSslEngineFactory(trust))).run(client -> withServer(server).run(server -> { server.enqueueEcho(); server.enqueueEcho(); String body = "hello there"; // first request fails because server certificate is rejected Throwable cause = null; try { client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS); } catch (final ExecutionException e) { cause = e.getCause(); } assertNotNull(cause); // second request should succeed trust.set(true); Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get(TIMEOUT, SECONDS); assertEquals(response.getResponseBody(), body); })); logger.debug("<<< reconnectAfterFailedCertificationPath"); }
private AsyncHttpClientConfig getAsyncHttpClientConfig() { // for this test brevity's sake, we are limiting to 1 retries return config().setMaxRequestRetry(0).setRequestTimeout(10000).build(); }
@Test public void testEolTerminatedResponse() throws Exception { try (AsyncHttpClient ahc = asyncHttpClient(config().setMaxRequestRetry(0))) { ahc.executeRequest(ahc.prepareGet(getTargetUrl()).setHeader(ACCEPT_ENCODING, HttpHeaderValues.GZIP_DEFLATE).setHeader(CONNECTION, HttpHeaderValues.CLOSE).build()) .get(); } }
/** * Set the number of time a request will be retried when an {@link java.io.IOException} occurs because of a Network exception. * * @param maxRequestRetry the number of time a request will be retried * @return this */ public Builder setMaxRequestRetry(int maxRequestRetry) { configBuilder.setMaxRequestRetry(maxRequestRetry); return this; }