@Override protected CloseableHttpAsyncClient newClient() { CloseableHttpAsyncClient result = HttpAsyncClients.custom().build(); result.start(); return result; }
public MetricFetcher() { int cores = Runtime.getRuntime().availableProcessors() * 2; long keepAliveTime = 0; int queueSize = 2048; RejectedExecutionHandler handler = new DiscardPolicy(); fetchService = new ThreadPoolExecutor(cores, cores, keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize), new NamedThreadFactory("sentinel-dashboard-metrics-fetchService"), handler); fetchWorker = new ThreadPoolExecutor(cores, cores, keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize), new NamedThreadFactory("sentinel-dashboard-metrics-fetchWorker"), handler); IOReactorConfig ioConfig = IOReactorConfig.custom() .setConnectTimeout(3000) .setSoTimeout(3000) .setIoThreadCount(Runtime.getRuntime().availableProcessors() * 2) .build(); httpclient = HttpAsyncClients.custom() .setRedirectStrategy(new DefaultRedirectStrategy() { @Override protected boolean isRedirectable(final String method) { return false; } }).setMaxConnTotal(4000) .setMaxConnPerRoute(1000) .setDefaultIOReactorConfig(ioConfig) .build(); httpclient.start(); start(); }
public SentinelApiClient() { IOReactorConfig ioConfig = IOReactorConfig.custom().setConnectTimeout(3000).setSoTimeout(10000) .setIoThreadCount(Runtime.getRuntime().availableProcessors() * 2).build(); httpClient = HttpAsyncClients.custom().setRedirectStrategy(new DefaultRedirectStrategy() { @Override protected boolean isRedirectable(final String method) { return false; } }).setMaxConnTotal(4000).setMaxConnPerRoute(1000).setDefaultIOReactorConfig(ioConfig).build(); httpClient.start(); }
private void setupAsyncClient() { final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom(); if (sslContext != null) { clientBuilder.setSSLContext(sslContext); clientBuilder.addInterceptorFirst(new HttpsResponseInterceptor()); } httpAsyncClient = clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider()).build(); httpAsyncClient.start(); }
private CloseableHttpAsyncClient createAsyncHttpClient(NHttpClientConnectionManager connectionManager) { return configureHttpClient( HttpAsyncClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(getRequestConfig()) .setProxyAuthenticationStrategy(httpClientConfig.getProxyAuthenticationStrategy()) .setRoutePlanner(getRoutePlanner()) .setDefaultCredentialsProvider(httpClientConfig.getCredentialsProvider()) ).build(); }
HttpAsyncClientBuilder builder = HttpAsyncClients.custom() .setThreadFactory(new RapidoidThreadFactory("http-client", true)) .disableConnectionState()
.build(); HttpAsyncClientBuilder client = HttpAsyncClients.custom() .setDefaultCookieStore(new BasicCookieStore()) .setSSLContext(sslContext)
private CloseableHttpAsyncClient createHttpClient(URL credentialsScopeUrl, String username, String password) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(credentialsScopeUrl.getHost(), credentialsScopeUrl.getPort()), new UsernamePasswordCredentials(username, password)); CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider).build(); httpClient.start(); return httpClient; }
public HttpAsyncClientBuilder httpClientBuilder() { final int connectionTimeout = this.getConnectionTimeout(); final int readTimeout = this.getReadTimeout(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout) .setConnectTimeout(connectionTimeout).build(); return HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig); }
private static CloseableHttpAsyncClient buildHttpClient() { RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT) .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT) .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT) .build(); CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom() .setDefaultRequestConfig(defaultRequestConfig) .build(); // Start httpAsyncClient.start(); return httpAsyncClient; }
private static CloseableHttpAsyncClient initializeHttpClient(URL credentialsScopeUrl, String username, String password) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(credentialsScopeUrl.getHost(), credentialsScopeUrl.getPort()), new UsernamePasswordCredentials(username, password)); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(20000) .setConnectTimeout(20000) .setConnectionRequestTimeout(20000) .build(); CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider) .setDefaultRequestConfig(requestConfig) .build(); httpClient.start(); return httpClient; }
/** * 设置代理 * * @param hostOrIP * @param port * @return */ static public HttpAsyncClientBuilder proxy(String hostOrIP, int port) { // 依次是代理地址,代理端口号,协议类型 HttpHost proxy = new HttpHost(hostOrIP, port, "http"); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); return HttpAsyncClients.custom().setRoutePlanner(routePlanner); }
static CloseableHttpAsyncClient buildDefaultHttpClient() { // TODO: Increase timeout?? RequestConfig requestConfig = RequestConfig.custom().build(); CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build(); httpClient.start(); return httpClient; }
private static CloseableHttpAsyncClient createNoSSLClient() { final TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; try { final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); return HttpAsyncClients.custom() .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .setSSLContext(sslContext).build(); } catch (Exception e) { logger.error("Could not create SSLContext",e); return null; } }
@VisibleForTesting public Sender(String apiKey, int retryCount, String url) { ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); this.url = url; this.authorizationHeader = String.format("key=%s", apiKey); this.client = HttpAsyncClients.custom() .setMaxConnTotal(100) .setMaxConnPerRoute(10) .build(); this.executor = new AsyncRetryExecutor(scheduler).retryOn(ServerFailedException.class) .retryOn(TimeoutException.class) .retryOn(IOException.class) .withExponentialBackoff(100, 2.0) .withUniformJitter() .withMaxDelay(4000) .withMaxRetries(retryCount); this.client.start(); }
/** * Create asynchronous http client based on connection manager. * * @param connectionManager Asynchronous http client connection manager. * @return Asynchronous http client based on connection manager. */ protected CloseableHttpAsyncClient createHttpAsyncClient(NHttpClientConnectionManager connectionManager) { HttpAsyncClientBuilder builder = HttpAsyncClients.custom().setConnectionManager(connectionManager); int socketBufferSizeInBytes = this.config.getSocketBufferSizeInBytes(); if (socketBufferSizeInBytes > 0) { builder.setDefaultConnectionConfig( ConnectionConfig.custom().setBufferSize(socketBufferSizeInBytes).build()); } return builder.build(); }
protected CloseableHttpAsyncClient createAsyncHttpClient(NHttpClientConnectionManager connectionManager) { return HttpAsyncClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(getRequestConfig()) .setProxyAuthenticationStrategy(httpClientConfig.getProxyAuthenticationStrategy()) .setRoutePlanner(getRoutePlanner()) .setDefaultCredentialsProvider(httpClientConfig.getCredentialsProvider()) .build(); }
private CloseableHttpAsyncClient createAsyncHttpClient(NHttpClientConnectionManager connectionManager) { return configureHttpClient( HttpAsyncClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(getRequestConfig()) .setProxyAuthenticationStrategy(httpClientConfig.getProxyAuthenticationStrategy()) .setRoutePlanner(getRoutePlanner()) .setDefaultCredentialsProvider(httpClientConfig.getCredentialsProvider()) ).build(); }
private CloseableHttpAsyncClient createHttpAsyncClient(YunpianConf conf) throws IOReactorException { IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors()) .setConnectTimeout(conf.getConfInt(YunpianConf.HTTP_CONN_TIMEOUT, "10000")) .setSoTimeout(conf.getConfInt(YunpianConf.HTTP_SO_TIMEOUT, "30000")).build(); ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig); PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor); ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .setCharset(Charset.forName(conf.getConf(YunpianConf.HTTP_CHARSET, YunpianConf.HTTP_CHARSET_DEFAULT))).build(); connManager.setDefaultConnectionConfig(connectionConfig); connManager.setMaxTotal(conf.getConfInt(YunpianConf.HTTP_CONN_MAXTOTAL, "100")); connManager.setDefaultMaxPerRoute(conf.getConfInt(YunpianConf.HTTP_CONN_MAXPERROUTE, "10")); CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setConnectionManager(connManager).build(); httpclient.start(); return httpclient; }
public static CloseableHttpAsyncClient getClient() { synchronized (HttpClientConnector.class) { if (asyncHttpClient == null ) { asyncHttpClient = HttpAsyncClients.custom() .setMaxConnPerRoute(MAX_CONN_PER_ROUTE) .setMaxConnTotal(MAX_CONN_TOTAL) .setDefaultIOReactorConfig( IOReactorConfig.custom() .setIoThreadCount(4) .setSoKeepAlive(true) .setSoReuseAddress(true) .build() ).build(); asyncHttpClient.start(); } return asyncHttpClient; } }