本文整理汇总了Java中org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager类的典型用法代码示例。如果您正苦于以下问题:Java PoolingNHttpClientConnectionManager类的具体用法?Java PoolingNHttpClientConnectionManager怎么用?Java PoolingNHttpClientConnectionManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PoolingNHttpClientConnectionManager类属于org.apache.http.impl.nio.conn包,在下文中一共展示了PoolingNHttpClientConnectionManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createHttpAsyncClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
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;
}
示例2: FiberApacheHttpClientRequestExecutor
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
public FiberApacheHttpClientRequestExecutor(final Validator<CloseableHttpResponse> resValidator, final int maxConnections, final int timeout, final int parallelism) throws IOReactorException {
final DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(IOReactorConfig.custom().
setConnectTimeout(timeout).
setIoThreadCount(parallelism).
setSoTimeout(timeout).
build());
final PoolingNHttpClientConnectionManager mngr = new PoolingNHttpClientConnectionManager(ioreactor);
mngr.setDefaultMaxPerRoute(maxConnections);
mngr.setMaxTotal(maxConnections);
final CloseableHttpAsyncClient ahc = HttpAsyncClientBuilder.create().
setConnectionManager(mngr).
setDefaultRequestConfig(RequestConfig.custom().setLocalAddress(null).build()).build();
client = new FiberHttpClient(ahc);
validator = resValidator;
}
示例3: createHttpClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
public static HttpClient createHttpClient(HiTSDBConfig config) throws HttpClientInitException {
Objects.requireNonNull(config);
// 创建 ConnectingIOReactor
ConnectingIOReactor ioReactor = initIOReactorConfig(config);
// 创建链接管理器
final PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
// 创建令牌管理器
semaphoreManager = createSemaphoreManager(config);
// 创建HttpAsyncClient
CloseableHttpAsyncClient httpAsyncClient = createPoolingHttpClient(config,cm,semaphoreManager);
// 组合生产HttpClientImpl
HttpClient httpClientImpl = new HttpClient(config,httpAsyncClient,semaphoreManager);
return httpClientImpl;
}
示例4: initFixedCycleCloseConnection
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
private static void initFixedCycleCloseConnection(final PoolingNHttpClientConnectionManager cm) {
// 定时关闭所有空闲链接
Executors.newSingleThreadScheduledExecutor(
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "Fixed-Cycle-Close-Connection" );
t.setDaemon(true);
return t;
}
}
).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
LOGGER.info("Close idle connections, fixed cycle operation");
cm.closeIdleConnections(3, TimeUnit.MINUTES);
} catch(Exception ex) {
LOGGER.error("",ex);
}
}
}, 30, 30, TimeUnit.SECONDS);
}
示例5: RestClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
RestClient(String baseUrl,
ObjectMapper objectMapper,
Map<String, Object> defaultHeaders,
Function<String, String> urlTransformer,
PoolingNHttpClientConnectionManager asyncConnectionManager,
PoolingHttpClientConnectionManager syncConnectionManager,
CloseableHttpAsyncClient asyncClient,
CloseableHttpClient syncClient) {
this.objectMapper = objectMapper;
this.baseUrl = baseUrl;
this.urlTransformer = urlTransformer;
this.asyncConnectionManager = asyncConnectionManager;
this.syncConnectionManager = syncConnectionManager;
this.asyncClient = asyncClient;
this.syncClient = syncClient;
this.defaultHeaders.putAll(defaultHeaders);
this.id = UUID.randomUUID().toString().substring(0, 8);
}
示例6: initDashboard
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
private DashboardSetupStatus initDashboard(final String hostAddress, final int port) {
final String dashboardURL = String.format("http://%s:%d/", hostAddress, port);
try {
// Create a pool of http client connection, which allow up to Integer.MAX_VALUE connections.
final PoolingNHttpClientConnectionManager connectionManager
= new PoolingNHttpClientConnectionManager(new DefaultConnectingIOReactor());
connectionManager.setMaxTotal(Integer.MAX_VALUE);
final CloseableHttpAsyncClient reusableHttpClient =
HttpAsyncClients.custom().setConnectionManager(connectionManager).build();
reusableHttpClient.start();
// run another thread to send metrics.
runMetricsSenderThread();
return DashboardSetupStatus.getSuccessful(dashboardURL, reusableHttpClient);
} catch (IOReactorException e) {
LOG.log(Level.WARNING, "Dashboard: Fail on initializing connection to the dashboard server.", e);
return DashboardSetupStatus.getFailed();
}
}
示例7: getAsyncProxyHttpClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
private CloseableHttpAsyncClient getAsyncProxyHttpClient(URI proxyUri) {
LOG.info("Creating async proxy http client");
PoolingNHttpClientConnectionManager cm = getAsyncConnectionManager();
HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort());
HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
if (cm != null) {
clientBuilder = clientBuilder.setConnectionManager(cm);
}
if (proxy != null) {
clientBuilder = clientBuilder.setProxy(proxy);
}
clientBuilder = setRedirects(clientBuilder);
return clientBuilder.build();
}
示例8: defaultClientBuilder
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
/**
* Used internally to initialize the internal HTTP client used by all
* instances of a client.
* <p>
* This method can be overriden to provide a client with different options.
* The client built gets an extra interceptor to add the credentials headers.
*
* @return HTTP default async client builder
*/
protected static HttpAsyncClientBuilder defaultClientBuilder() {
try {
DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
connMgr = new PoolingNHttpClientConnectionManager(ioReactor);
connMgr.setMaxTotal(maxConnections);
connMgr.setDefaultMaxPerRoute(maxConnections);
} catch (IOReactorException e) {
}
return HttpAsyncClients
.custom()
.addInterceptorLast(new GzipInterceptors.GzipRequestInterceptor())
.setConnectionManager(connMgr)
.setDefaultRequestConfig(
RequestConfig.custom()
.setSocketTimeout(3600 * 1000) // 1 hour
.build())
.setKeepAliveStrategy(keepAliveStrategy);
}
示例9: asyncHttpClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
@Bean
public CloseableHttpAsyncClient asyncHttpClient() {
try {
PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(
new DefaultConnectingIOReactor(IOReactorConfig.DEFAULT));
connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS)
.build();
return HttpAsyncClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(config)
.build();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
示例10: SleepServerApiClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
public SleepServerApiClient() throws Exception {
connectionManager = new PoolingNHttpClientConnectionManager(
new DefaultConnectingIOReactor(IOReactorConfig.DEFAULT));
connectionManager.setMaxTotal(20000);
connectionManager.setDefaultMaxPerRoute(20000);
RequestConfig config = RequestConfig.custom().setConnectTimeout(120000)
.build();
CloseableHttpAsyncClient httpClient = HttpAsyncClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(config).build();
HttpComponentsAsyncClientHttpRequestFactory requestFactory = new HttpComponentsAsyncClientHttpRequestFactory(
httpClient);
client = new AsyncRestTemplate(requestFactory);
}
示例11: getNHttpConnManager
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
private NHttpClientConnectionManager getNHttpConnManager(Config config) throws IOException {
NHttpClientConnectionManager httpConnManager;
String connMgrStr = config.getString(HTTP_CONN_MANAGER);
switch (ApacheHttpClient.ConnManager.valueOf(connMgrStr.toUpperCase())) {
case POOLING:
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager poolingConnMgr = new PoolingNHttpClientConnectionManager(ioReactor);
poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN));
poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN));
httpConnManager = poolingConnMgr;
break;
default:
throw new IllegalArgumentException(connMgrStr + " is not supported");
}
LOG.info("Using " + httpConnManager.getClass().getSimpleName());
return httpConnManager;
}
示例12: before
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
@Before
public void before() throws Exception {
tsdb = PowerMockito.mock(TSDB.class);
config = new Config(false);
connection_manager = mock(PoolingNHttpClientConnectionManager.class);
client_builder = mock(HttpAsyncClientBuilder.class);
client = mock(CloseableHttpAsyncClient.class);
ts_meta_schema = mock(TSMetaSchema.class);
uid_meta_schema = mock(UIDMetaSchema.class);
annotation_schema = mock(AnnotationSchema.class);
config.overrideConfig("tsd.search.elasticsearch.host", "localhost:9200");
when(tsdb.getConfig()).thenReturn(config);
PowerMockito.mockStatic(HttpAsyncClients.class);
when(HttpAsyncClients.custom()).thenReturn(client_builder);
PowerMockito.whenNew(PoolingNHttpClientConnectionManager.class)
.withAnyArguments().thenReturn(connection_manager);
when(client_builder.build()).thenReturn(client);
}
示例13: AsyncServiceClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
public AsyncServiceClient(ClientConfiguration config) {
super(config);
try {
IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
.setIoThreadCount(config.getIoThreadCount()).build();
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(
ioReactorConfig);
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(
ioReactor);
cm.setMaxTotal(config.getMaxConnections());
cm.setDefaultMaxPerRoute(config.getMaxConnections());
httpClient = new HttpFactory().createHttpAsyncClient(config, cm);
/*
* socketTimeout的值限制了closeIdleConnections执行的周期。
* 如果周期相对socketTimeout的值过长,有可能一个请求分配到一个即将socketTimeout的连接,
* 在请求发送之前即抛出SocketTimeoutException。
* 现在让closeIdleConnections的执行周期为socketTimeout / 2.5。
*/
long closePeriod = 5000;
if (config.getSocketTimeoutInMillisecond() > 0) {
closePeriod = (long) (config.getSocketTimeoutInMillisecond() / 2.5);
}
closePeriod = closePeriod < 5000 ? closePeriod : 5000;
connEvictor = new IdleConnectionEvictor(cm, closePeriod);
httpClient.start();
connEvictor.start();
} catch (IOReactorException ex) {
throw new ClientException(String.format("IOReactorError: %s",
ex.getMessage()), ex);
}
}
示例14: createHttpAsyncClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
public CloseableHttpAsyncClient createHttpAsyncClient(
ClientConfiguration config, PoolingNHttpClientConnectionManager cm) {
HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
httpClientBuilder.setConnectionManager(cm);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(config.getConnectionTimeoutInMillisecond())
.setSocketTimeout(config.getSocketTimeoutInMillisecond()).build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setUserAgent(config.getUserAgent());
httpClientBuilder.disableCookieManagement();
String proxyHost = config.getProxyHost();
int proxyPort = config.getProxyPort();
if (proxyHost != null) {
if (proxyPort <= 0) {
throw new ClientException("The proxy port is invalid. Please check your configuration.");
}
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpClientBuilder.setProxy(proxy);
String proxyUsername = config.getProxyUsername();
String proxyPassword = config.getProxyPassword();
if (proxyUsername != null && proxyPassword != null) {
String proxyDomain = config.getProxyDomain();
String proxyWorkstation = config.getProxyWorkstation();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyHost, proxyPort), new NTCredentials(
proxyUsername, proxyPassword, proxyWorkstation,
proxyDomain));
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}
}
return httpClientBuilder.build();
}
示例15: asyncHttpClient
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; //导入依赖的package包/类
@Bean
public CloseableHttpAsyncClient asyncHttpClient(PoolingNHttpClientConnectionManager poolingNHttpClientConnectionManager) {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(requestTimeout)
.setSocketTimeout(requestTimeout)
.setConnectionRequestTimeout(requestTimeout)
.build();
return HttpAsyncClientBuilder
.create().setConnectionManager(poolingNHttpClientConnectionManager)
.setDefaultRequestConfig(config).build();
}