本文整理匯總了Java中org.apache.http.impl.conn.PoolingHttpClientConnectionManager類的典型用法代碼示例。如果您正苦於以下問題:Java PoolingHttpClientConnectionManager類的具體用法?Java PoolingHttpClientConnectionManager怎麽用?Java PoolingHttpClientConnectionManager使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PoolingHttpClientConnectionManager類屬於org.apache.http.impl.conn包,在下文中一共展示了PoolingHttpClientConnectionManager類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: afterPropertiesSet
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
@PostConstruct
public void afterPropertiesSet() throws Exception {
RegistryBuilder<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.create();
schemeRegistry.register("http", PlainConnectionSocketFactory.getSocketFactory());
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(new KeyManager[0], new TrustManager[]{new SimpleTrustManager()}, null);
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslcontext);
schemeRegistry.register("https", sf);
pool = new PoolingHttpClientConnectionManager(schemeRegistry.build());
pool.setMaxTotal(maxConnection);
pool.setDefaultMaxPerRoute(maxConnection);
pool.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(sotimeout).build());
}
示例2: getConnctionManager
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
public static PoolingHttpClientConnectionManager getConnctionManager(){
Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
try {
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
new TrustAllHostNameVerifier());
socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create()
.register("http", new PlainConnectionSocketFactory())
.register("https", trustSelfSignedSocketFactory)
.build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
Data.logger.warn("", e);
}
PoolingHttpClientConnectionManager cm = (socketFactoryRegistry != null) ?
new PoolingHttpClientConnectionManager(socketFactoryRegistry):
new PoolingHttpClientConnectionManager();
// twitter specific options
cm.setMaxTotal(2000);
cm.setDefaultMaxPerRoute(200);
return cm;
}
示例3: testHttpRequestGet
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
@Test
public void testHttpRequestGet() throws Exception {
RequestConfig.Builder req = RequestConfig.custom();
req.setConnectTimeout(5000);
req.setConnectionRequestTimeout(5000);
req.setRedirectsEnabled(false);
req.setSocketTimeout(5000);
req.setExpectContinueEnabled(false);
HttpGet get = new HttpGet("http://127.0.0.1:54322/login");
get.setConfig(req.build());
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(5);
HttpClientBuilder builder = HttpClients.custom();
builder.disableAutomaticRetries();
builder.disableRedirectHandling();
builder.setConnectionTimeToLive(5, TimeUnit.SECONDS);
builder.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE);
builder.setConnectionManager(cm);
CloseableHttpClient client = builder.build();
String s = client.execute(get, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
assertEquals(301, response.getStatusLine().getStatusCode());
return "success";
}
});
assertEquals("success", s);
}
示例4: createConnectionManager
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
protected HttpClientConnectionManager createConnectionManager(Registry<ConnectionSocketFactory> registry, int maxTotalConnections, int connectionsPerRoute) {
// setup the connection live time
PoolingHttpClientConnectionManager answer =
new PoolingHttpClientConnectionManager(registry, null, null, null, getConnectionTimeToLive(), TimeUnit.MILLISECONDS);
int localMaxTotalConnections = maxTotalConnections;
if (localMaxTotalConnections == 0) {
localMaxTotalConnections = getMaxTotalConnections();
}
if (localMaxTotalConnections > 0) {
answer.setMaxTotal(localMaxTotalConnections);
}
int localConnectionsPerRoute = connectionsPerRoute;
if (localConnectionsPerRoute == 0) {
localConnectionsPerRoute = getConnectionsPerRoute();
}
if (localConnectionsPerRoute > 0) {
answer.setDefaultMaxPerRoute(localConnectionsPerRoute);
}
LOG.info("Created ClientConnectionManager " + answer);
return answer;
}
示例5: OAuthHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
public OAuthHttpClient(int maxConnection, int connectTimeout, int socketTimeout) {
PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build());
// set max connection
connectionManager.setMaxTotal(maxConnection);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.build();
httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
.build();
}
示例6: DefaultConnector
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
/**
*
*/
public DefaultConnector() {
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.closeIdleConnections(120, TimeUnit.SECONDS);
// would be nice to set this from outside -> keep alive
final SocketConfig sConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(Context.SOCKET_TO).build();
cm.setDefaultSocketConfig(sConfig);
cm.setMaxTotal(150);
cm.setDefaultMaxPerRoute(150);
cm.setValidateAfterInactivity(0);
final HttpRequestRetryHandler rh = new DefaultHttpRequestRetryHandler(3, true);
httpClient = HttpClients.custom().setRetryHandler(rh).setConnectionManager(cm).build();
}
示例7: init
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
public void init() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
/* 配置同時支持 HTTP 和 HTPPS */
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
/* 初始化連接管理器 */
poolConnManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
poolConnManager.setMaxTotal(maxTotal);
poolConnManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
.setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpClient = getConnection();
log.info("HttpConnectionManager初始化完成...");
} catch (Exception e) {
log.error("error", e);
}
}
示例8: test
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
public static void test(){
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
// Increase max connections for localhost:80 to 50
HttpHost localhost = new HttpHost("http://cc.0071515.com", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 2);
// CloseableHttpClient httpClient = HttpClients.custom()
// .setConnectionManager(cm)
// .build();
httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
}
示例9: createDefaultHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
protected CloseableHttpClient createDefaultHttpClient() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(maxConnections);
cm.setDefaultMaxPerRoute(maxConnections);
Builder config = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout);
// TODO: Set Credentials
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm).setDefaultRequestConfig(config.build())
.build();
return httpClient;
}
示例10: getPool
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
public HttpClient getPool() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
HttpClientBuilder builder = HttpClientBuilder.create();
connectionManager.setMaxTotal(maxConnections);
connectionManager.setDefaultMaxPerRoute(maxConnections);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeOut)
.setConnectionRequestTimeout(timeOut).setSocketTimeout(timeOut)
.build();
connectionMonitor = new IdleConnectionMonitorThread(connectionManager);
connectionMonitor.start();
builder.setConnectionManager(connectionManager);
builder.setKeepAliveStrategy(myStrategy);
CloseableHttpClient defaultHttpClient = builder
.setDefaultRequestConfig(config).build();
logger.debug("prepared client");
return defaultHttpClient;
}
示例11: httpsGet
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
public static String httpsGet(String getUrl, String defaultCharset, Map<String, String> headerParas)
throws KeyManagementException, NoSuchAlgorithmException {
// 采用繞過驗證的方式處理https請求
SSLContext sslcontext = createIgnoreVerifySSL();
// 設置協議http和https對應的處理socket鏈接工廠的對象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslcontext))
.build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
// 創建自定義的httpclient對象
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connManager).build();
return getInner(getUrl, defaultCharset, headerParas, httpclient);
}
示例12: initPoolingHttpClientManager
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
private void initPoolingHttpClientManager () {
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(MAX_CONNECTION);
connectionManager.setDefaultMaxPerRoute(MAX_CONNECTION_PER_ROUTE);
final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setConnectTimeout(CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(CONNECTION_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT);
final RequestConfig requestConfig = requestConfigBuilder.build();
HashSet<Header> defaultHeaders = new HashSet<Header>();
defaultHeaders.add(new BasicHeader(HttpHeaders.PRAGMA, "no-cache"));
defaultHeaders.add(new BasicHeader(HttpHeaders.CACHE_CONTROL, "no-cache"));
final HttpClientBuilder httpClientBuilder = HttpClients.custom().setDefaultHeaders(defaultHeaders).disableAuthCaching().disableContentCompression();
this.httpClient = httpClientBuilder.setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
}
示例13: getCustomClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
/**
* custom http client for server with SSL errors
*
* @return
*/
public final CloseableHttpClient getCustomClient() {
try {
HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
(TrustStrategy) (X509Certificate[] arg0, String arg1) -> true).build();
builder.setSSLContext(sslContext);
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
builder.setConnectionManager(connMgr);
return builder.build();
} catch (Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
}
return getSystemClient();
}
示例14: create
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
public HttpClientConnectionManager create(ApacheSdkHttpClientFactory configuration,
AttributeMap standardOptions) {
ConnectionSocketFactory sslsf = getPreferredSocketFactory(standardOptions);
final PoolingHttpClientConnectionManager cm = new
PoolingHttpClientConnectionManager(
createSocketFactoryRegistry(sslsf),
null,
DefaultSchemePortResolver.INSTANCE,
null,
configuration.connectionTimeToLive().orElse(Defaults.CONNECTION_POOL_TTL).toMillis(),
TimeUnit.MILLISECONDS);
cm.setDefaultMaxPerRoute(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
cm.setMaxTotal(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
cm.setDefaultSocketConfig(buildSocketConfig(standardOptions));
return cm;
}
示例15: InternalPredictionService
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //導入依賴的package包/類
@Autowired
public InternalPredictionService(AppProperties appProperties){
this.appProperties = appProperties;
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(150);
connectionManager.setDefaultMaxPerRoute(150);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(DEFAULT_REQ_TIMEOUT)
.setConnectTimeout(DEFAULT_CON_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT).build();
httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.setRetryHandler(new HttpRetryHandler())
.build();
}