@Test public void testNoTransferEncoding() throws Exception { String url = "http://localhost:" + port + "/test"; AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder() .setFollowRedirect(false) .setConnectTimeout(15000) .setRequestTimeout(15000) .setAllowPoolingConnections(false) .setDisableUrlEncodingForBoundedRequests(true) .setIOThreadMultiplier(2) // 2 is default .build(); try (AsyncHttpClient client = new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config)) { Future<com.ning.http.client.Response> f = client.prepareGet(url).execute(); com.ning.http.client.Response r = f.get(10, TimeUnit.SECONDS); Assert.assertEquals(TEST_MESSAGE, r.getResponseBody()); } } }
private void initFromOptions(ClientOptions options) { if(options != ClientOptions.IMPLEMENTATION_DEFAULT) { AsyncHttpClientConfig.Builder config = new AsyncHttpClientConfig.Builder(); config.setUserAgent(options.userAgent); if(options.proxyHost != null) { config.setProxyServer(new ProxyServer(options.proxyHost, options.proxyPort)); } config.setConnectionTimeoutInMs(options.connectionTimeoutMillis); config.setRequestTimeoutInMs(options.requestTimeoutMillis); config.setFollowRedirects(options.followRedirects); config.setMaximumConnectionsPerHost(options.maxConnectionsPerDestination); config.setMaximumConnectionsTotal(options.maxConnectionsTotal); config.setFollowRedirects(RequestOptions.DEFAULT_FOLLOW_REDIRECTS); config.setAllowPoolingConnection(options.getBooleanProperty("allowPoolingConnection", true)); config.setIOThreadMultiplier(options.getIntProperty("ioThreadMultiplier", 2)); config.setIdleConnectionInPoolTimeoutInMs(options.getTimeProperty("idleConnectionInPoolTimeout", 60 * 1000)); config.setIdleConnectionTimeoutInMs(options.getTimeProperty("idleConnectionTimeout", 60 * 1000)); config.setMaxConnectionLifeTimeInMs(options.getTimeProperty("maxConnectionLife", -1)); config.setCompressionEnabled(options.getBooleanProperty("compressionEnabled", false)); config.setRequestCompressionLevel(options.getIntProperty("requestCompressionLevel", 1)); this.httpClient = new AsyncHttpClient(config.build()); } else { this.httpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().build()); } }
/** * @param apiUrl the URL of the PredictionIO API * @param threadLimit maximum number of simultaneous threads (connections) to the API * @param queueSize size of the queue * @param timeout timeout in seconds for the connections */ public BaseClient(String apiUrl, int threadLimit, int queueSize, int timeout) { this.apiUrl = apiUrl; // Async HTTP client config AsyncHttpClientConfig config = (new AsyncHttpClientConfig.Builder()) .setAllowPoolingConnections(true) .setAllowPoolingSslConnections(true) .addRequestFilter(new ThrottleRequestFilter(threadLimit)) .setMaxConnectionsPerHost(threadLimit) .setRequestTimeout(timeout * 1000) .setIOThreadMultiplier(threadLimit) .build(); this.client = new AsyncHttpClient(new NettyAsyncHttpProvider(config), config); }
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); }
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); }
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()); }