builder.setAllowPoolingConnections(true); if (client.getSslContext() != null) { builder.setSSLContext(client.getSslContext());
@Test(timeOut = 10 * 60 * 1000, groups = "scalability") public void rc10kProblem() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setMaxConnectionsPerHost(C10K).setAllowPoolingConnections(true).build())) { List<Future<Integer>> resps = new ArrayList<>(C10K); int i = 0; while (i < C10K) { resps.add(client.prepareGet(String.format("http://127.0.0.1:%d/%d", ports[i % SRV_COUNT], i)).execute(new MyAsyncHandler(i++))); } i = 0; for (Future<Integer> fResp : resps) { Integer resp = fResp.get(); assertNotNull(resp); assertEquals(resp.intValue(), i++); } } }
@Test(groups = { "online", "default_provider" }, enabled = false) public void invalidStreamTest2() throws Throwable { AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setRequestTimeout(10000).setFollowRedirect(true).setAllowPoolingConnections(false).setMaxRedirects(6).build(); try (AsyncHttpClient client = getAsyncHttpClient(config)) { Response response = client.prepareGet("http://bit.ly/aUjTtG").execute().get(); if (response != null) { System.out.println(response); } } catch (Throwable t) { t.printStackTrace(); assertNotNull(t.getCause()); assertEquals(t.getCause().getMessage(), "invalid version format: ICY"); } }
@Test(groups = { "standalone", "default_provider" }) public void testMaxTotalConnections() { try (AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setAllowPoolingConnections(true).setMaxConnections(1).build())) { String url = getTargetUrl(); int i; Exception exception = null; for (i = 0; i < 3; i++) { try { log.info("{} requesting url [{}]...", i, url); Response response = client.prepareGet(url).execute().get(); log.info("{} response [{}].", i, response); } catch (Exception ex) { exception = ex; } } assertNull(exception); } }
public PerfClientCallable(Parameters params, ArrayList<DestToPayload> payloads) { this.params = params; this.asyncHttpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder() .setConnectTimeout(Integer.MAX_VALUE) .setRequestTimeout(Integer.MAX_VALUE) .setAllowPoolingConnections(true) .build()); this.payloads = new ArrayList<>(); for (DestToPayload payload : payloads) { this.payloads.add(asyncHttpClient.preparePost(payload.getDestination()) .addHeader("content-type", "application/json") .addHeader("Accept", "application/xml") .setBody(payload.getPayload()) .setRequestTimeout(Integer.MAX_VALUE) .build()); } executionStrategy = getExecutionStrategy(); }
@Test(groups = { "standalone", "default_provider" }) public void basicAuthFileNoKeepAliveTest() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setAllowPoolingConnections(false).build())) { ClassLoader cl = getClass().getClassLoader(); // override system properties URL url = cl.getResource("SimpleTextFile.txt"); File file = new File(url.toURI()); final String fileContent = getFileContent(file); AsyncHttpClient.BoundRequestBuilder r = client.preparePost(getTargetUrl()).setBody(file).setRealm((new Realm.RealmBuilder()).setPrincipal(user).setPassword(admin).build()); Future<Response> f = r.execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), fileContent); } }
@Test(invocationCount = 4, groups = { "online", "default_provider" }) public void multipleSslRequestsWithDelayAndKeepAlive() throws Throwable { AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder().setFollowRedirect(true).setSSLContext(getSSLContext()).setAllowPoolingConnections(true).setConnectTimeout(10000) .setPooledConnectionIdleTimeout(60000).setRequestTimeout(10000).setMaxConnectionsPerHost(-1).setMaxConnections(-1); try (AsyncHttpClient client = getAsyncHttpClient(configBuilder.build())) { final BoundRequestBuilder builder = client.prepareGet(VERISIGN_HTTPS_URL); final Response response1 = builder.execute().get(); Thread.sleep(4000); final Response response2 = builder.execute().get(); if (response2 != null) { System.out.println("Success (2nd response was not null)."); } else { System.out.println("Failed (2nd response was null)."); } Assert.assertNotNull(response1); Assert.assertNotNull(response2); } }
@Override public void start() { selectorScheduler = schedulerService.customScheduler(schedulersConfig .withDirectRunCpuLightWhenTargetBusy(true) .withMaxConcurrentTasks(DEFAULT_SELECTOR_THREAD_COUNT) .withName(name), MAX_VALUE); workerScheduler = schedulerService.ioScheduler(schedulersConfig); AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder(); builder.setAllowPoolingConnections(true); configureTransport(builder); configureTlsContext(builder); configureProxy(builder); configureConnections(builder); AsyncHttpClientConfig config = builder.build(); asyncHttpClient = new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config); }
public Builder setAllowPoolingConnections(boolean allowPoolingConnections) { configBuilder.setAllowPoolingConnections(allowPoolingConnections); return this; }
private void configureConnections(AsyncHttpClientConfig.Builder builder) throws InitialisationException { if (maxConnections > 0) { builder.addRequestFilter(new CustomTimeoutThrottleRequestFilter(maxConnections)); } builder.setMaxConnections(maxConnections); builder.setMaxConnectionsPerHost(maxConnections); builder.setAllowPoolingConnections(usePersistentConnections); builder.setAllowPoolingSslConnections(usePersistentConnections); builder.setConnectionTTL(MAX_CONNECTION_LIFETIME); builder.setPooledConnectionIdleTimeout(connectionIdleTimeout); builder.setIOThreadMultiplier(1); }
public void setAllowPoolingConnection(boolean allowPoolingConnection) { builder.setAllowPoolingConnections(allowPoolingConnection); }
public Builder setAllowPoolingConnections(boolean allowPoolingConnections) { configBuilder.setAllowPoolingConnections(allowPoolingConnections); return this; }
public Builder setAllowPoolingConnections(boolean allowPoolingConnections) { configBuilder.setAllowPoolingConnections(allowPoolingConnections); return this; }
protected AsyncHttpClientConfig.Builder httpClientBuilder() { return new AsyncHttpClientConfig.Builder()// .setAllowPoolingConnections(true)// .setMaxConnectionsPerHost(1)// .setMaxConnections(1)// .setConnectTimeout(1000)// .setRequestTimeout(1000).setFollowRedirect(true); }
AsyncHttpClientConfig createHttpClientConfig() { return new Builder() .setCompressionEnforced( true ) .setAllowPoolingConnections( true ) .setExecutorService( executor ) .setRequestTimeout( 30000 ) .build(); }
private void configureConnections(AsyncHttpClientConfig.Builder builder) { if (maxConnections > 0) { builder.addRequestFilter(new CustomTimeoutThrottleRequestFilter(maxConnections)); } builder.setMaxConnections(maxConnections); builder.setMaxConnectionsPerHost(maxConnections); builder.setAllowPoolingConnections(usePersistentConnections); builder.setAllowPoolingSslConnections(usePersistentConnections); builder.setConnectionTTL(MAX_CONNECTION_LIFETIME); builder.setPooledConnectionIdleTimeout(connectionIdleTimeout); builder.setIOThreadMultiplier(1); }
/** * Set whether to allow connection pooling or not. Default is true. * * @param allowPoolingConnections the maximum number of connections. * @return this config builder. */ public Builder allowPoolingConnections(final boolean allowPoolingConnections) { clientConfig.setAllowPoolingConnections(allowPoolingConnections); clientConfig.setAllowPoolingSslConnections(allowPoolingConnections); return this; }
@Override public void start() throws MuleException { AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder(); builder.setAllowPoolingConnections(true); configureTransport(builder); configureTlsContext(builder); configureProxy(builder); configureConnections(builder); AsyncHttpClientConfig config = builder.build(); asyncHttpClient = new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config); }
AsyncHttpClient buildDefaultClient() { builder.setMaxRequestRetry(maxRequestRetry)//尝试重试次数 .setAllowPoolingConnections(allowPollingConnections)//开启连接池模式 .setMaxConnections(maxConnections)//最大连接数 .setConnectTimeout(connectTimeout)//连接超时时间 .setPooledConnectionIdleTimeout(readTimeout)//设置连接池超时时间 .setReadTimeout(readTimeout)//读取请求超时时间 .setFollowRedirect(followRedirect)//开启重定向追踪 .setMaxRedirects(maxRedirects)//重定向次数限制 .setUserAgent(userAgent)//设置UserAgent .setMaxConnectionsPerHost(maxConnectionsPerHost)//设置初始连接池大小 .setIOThreadMultiplier(ioThreadMultiplier)//用于IO的线程数 .setCompressionEnforced(compressionEnforced)//压缩执行 ; return new AsyncHttpClient(builder.build()); }
public Builder setAllowPoolingConnections(boolean allowPoolingConnections) { configBuilder.setAllowPoolingConnections(allowPoolingConnections); return this; }