org.apache.http.conn.HttpConnectionFactory Java Examples
The following examples show how to use
org.apache.http.conn.HttpConnectionFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtendedHttpClientBuilder.java From lavaplayer with Apache License 2.0 | 6 votes |
private static HttpClientConnectionManager createDefaultConnectionManager( HttpClientConnectionOperator operator, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory ) { PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager( operator, connectionFactory, -1, TimeUnit.MILLISECONDS ); manager.setMaxTotal(3000); manager.setDefaultMaxPerRoute(1500); return manager; }
Example #2
Source File: AutoCleanedPoolingHttpClientConnectionManager.java From caravan with Apache License 2.0 | 5 votes |
public AutoCleanedPoolingHttpClientConnectionManager(int connectionTtl, int inactivityTimeBeforeValidate, int connectionIdleTime, int cleanCheckInterval, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) { super(getDefaultRegistry(), connFactory, null, null, connectionTtl > 0 ? connectionTtl : DefaultConnectionConfig.DEFAULT_CONNECTION_TTL, TimeUnit.MILLISECONDS); if (inactivityTimeBeforeValidate <= 0) inactivityTimeBeforeValidate = DefaultConnectionConfig.DEFAULT_INACTIVITY_TIME_BEFORE_VALIDATE; setValidateAfterInactivity(inactivityTimeBeforeValidate); _idleConnectionMonitorThread = new IdleConnectionMonitorThread(this, connectionIdleTime, cleanCheckInterval); _idleConnectionMonitorThread.start(); }
Example #3
Source File: HttpClientTest.java From netcrusher-java with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { reactor = new NioReactor(); crusher = TcpCrusherBuilder.builder() .withReactor(reactor) .withBindAddress("127.0.0.1", CRUSHER_PORT) .withConnectAddress(REMOTE_HOST, REMOTE_PORT) .buildAndOpen(); DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase(REMOTE_HOST)) { return new InetAddress[] { InetAddress.getByAddress(new byte[] {127, 0, 0, 1}) }; } else { return super.resolve(host); } } }; HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> httpConnectionFactory = new ManagedHttpClientConnectionFactory(); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, httpConnectionFactory, dnsResolver); http = HttpClients.createMinimal(connectionManager); }
Example #4
Source File: SimpleHttpClientConnectionManager.java From lavaplayer with Apache License 2.0 | 5 votes |
public SimpleHttpClientConnectionManager( HttpClientConnectionOperator connectionOperator, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> factory ) { this.connectionOperator = connectionOperator; this.connectionFactory = factory != null ? factory : ManagedHttpClientConnectionFactory.INSTANCE; }
Example #5
Source File: HttpSyncClient.java From Almost-Famous with MIT License | 4 votes |
public CloseableHttpClient createSyncClient(boolean proxy) throws Exception { HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory(); HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory(); HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); SSLContext sslcontext = SSLContexts.createSystemDefault(); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); // Create a connection manager with custom configuration. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, connFactory); // Create socket configuration SocketConfig socketConfig = SocketConfig.custom() .setTcpNoDelay(true) .build(); // Configure the connection manager to use socket configuration either // by default or for a specific host. connManager.setDefaultSocketConfig(socketConfig); connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig); // Validate connections after 1 sec of inactivity connManager.setValidateAfterInactivity(1000); // Create message constraints MessageConstraints messageConstraints = MessageConstraints.custom() .setMaxHeaderCount(200) .setMaxLineLength(2000) .build(); // Create connection configuration ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints) .build(); // Configure the connection manager to use connection configuration either // by default or for a specific host. connManager.setDefaultConnectionConfig(connectionConfig); // Configure total max or per route limits for persistent connections // that can be kept in the pool or leased by the connection manager. connManager.setMaxTotal(poolSize); if (maxPerRoute > 0) { connManager.setDefaultMaxPerRoute(maxPerRoute); } else { connManager.setDefaultMaxPerRoute(10); } // Use custom cookie store if necessary. CookieStore cookieStore = new BasicCookieStore(); // Use custom credentials provider if necessary. CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); // Create global request configuration RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(connectTimeout) .setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(connectionRequestTimeout) .setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .build(); // Create an HttpClient with the given custom dependencies and configuration. CloseableHttpClient httpclient = HttpClients.custom() .setConnectionManager(connManager) .setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(defaultRequestConfig) .build(); return httpclient; }
Example #6
Source File: RestClientConfigurator.java From ats-framework with Apache License 2.0 | 4 votes |
/** <strong>Note</strong>: For internal usage only. Using this method may lead to undesired effects * Use org.glassfish.jersey.apache.connector.ApacheConnectorProvider as a provider<br> * @param connectorProvider - the connector provider's class * @param properties - (optional) configuration properties for the connection provider. * <strong>Note</strong>: If connections will be done over SSL (HTTPS), any of the needed configuration must be done by you. * ATS will NOT apply any of the logic, related to that functionality if connectionManager parameter is not null. * @param connectionManager - (optional) specify the connection manager to be used with the connector provider. If this parameter is not null, connection factory must also be provider * @param connectionFactory - (optional) specify the connection factory */ public void registerApacheConnectorProvider( ConnectorProvider connectorProvider, Map<String, Object> properties, HttpClientConnectionManager connectionManager, HttpConnectionFactory connectionFactory ) { try { Class<?> apacheClientProperties = Class.forName(RestClient.APACHE_CLIENT_PROPERTIES_CLASSNAME); if (properties != null) { if (properties.get((String) apacheClientProperties.getDeclaredField("REQUEST_CONFIG") .get(null)) != null) { // do nothing the user has provided such property } else { // add pool request timeout of 30 seconds RequestConfig requestConfig = (RequestConfig) properties.get((String) apacheClientProperties.getDeclaredField("REQUEST_CONFIG") .get(null)); if (requestConfig != null) { // Throw an org.apache.http.conn.ConnectionPoolTimeoutException exception if connection can not be leased/obtained from the pool after 30 sec requestConfig = RequestConfig.copy(requestConfig) .setConnectionRequestTimeout(30 * 1000) .build(); } else { // Throw an org.apache.http.conn.ConnectionPoolTimeoutException exception if connection can not be leased/obtained from the pool after 30 sec requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30 * 1000).build(); } } } else { // construct properties maps and add pool request timeout of 30 seconds properties = new HashMap<String, Object>(); // Throw an org.apache.http.conn.ConnectionPoolTimeoutException exception if connection can not be leased/obtained from the pool after 30 sec properties.put((String) apacheClientProperties.getDeclaredField("REQUEST_CONFIG").get(null), RequestConfig.custom().setConnectionRequestTimeout(30 * 1000).build()); } } catch (Exception e) { throw new RuntimeException("Unable to register connector provider '" + RestClient.APACHE_CONNECTOR_CLASSNAME + "'", e); } registerConnectorProvider(connectorProvider, properties); this.connectionManager = connectionManager; this.connectionFactory = connectionFactory; }
Example #7
Source File: RestClientConfigurator.java From ats-framework with Apache License 2.0 | 4 votes |
/** * Get the underlying connection factory that is used when ApacheConnectorProvider is used. Otherwise return null * */ HttpConnectionFactory getConnectionFactory() { return connectionFactory; }
Example #8
Source File: AutoCleanedPoolingHttpClientConnectionManager.java From caravan with Apache License 2.0 | 4 votes |
public AutoCleanedPoolingHttpClientConnectionManager(HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) { this(0, 0, 0, 0, connFactory); }
Example #9
Source File: ExtendedHttpClientBuilder.java From lavaplayer with Apache License 2.0 | 4 votes |
HttpClientConnectionManager create( HttpClientConnectionOperator operator, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory );
Example #10
Source File: RestTemplateConfig.java From spring-boot-cookbook with Apache License 2.0 | 4 votes |
private CloseableHttpClient getHttpClient() { //注册访问协议相关的Socket工厂 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); //HttpConnectionFactory:配置写请求/解析响应处理器 HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory = new ManagedHttpClientConnectionFactory( DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE ); //DNS解析器 DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE; //创建连接池管理器 PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connectionFactory, dnsResolver); //设置默认的socket参数 manager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); manager.setMaxTotal(300);//设置最大连接数。高于这个值时,新连接请求,需要阻塞,排队等待 //路由是对MaxTotal的细分。 // 每个路由实际最大连接数默认值是由DefaultMaxPerRoute控制。 // MaxPerRoute设置的过小,无法支持大并发:ConnectionPoolTimeoutException:Timeout waiting for connection from pool manager.setDefaultMaxPerRoute(200);//每个路由的最大连接 manager.setValidateAfterInactivity(5 * 1000);//在从连接池获取连接时,连接不活跃多长时间后需要进行一次验证,默认为2s //配置默认的请求参数 RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(2 * 1000)//连接超时设置为2s .setSocketTimeout(5 * 1000)//等待数据超时设置为5s .setConnectionRequestTimeout(2 * 1000)//从连接池获取连接的等待超时时间设置为2s // .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.2", 1234))) //设置代理 .build(); CloseableHttpClient closeableHttpClient = HttpClients.custom() .setConnectionManager(manager) .setConnectionManagerShared(false)//连接池不是共享模式,这个共享是指与其它httpClient是否共享 .evictIdleConnections(60, TimeUnit.SECONDS)//定期回收空闲连接 .evictExpiredConnections()//回收过期连接 .setConnectionTimeToLive(60, TimeUnit.SECONDS)//连接存活时间,如果不设置,则根据长连接信息决定 .setDefaultRequestConfig(defaultRequestConfig)//设置默认的请求参数 .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)//连接重用策略,即是否能keepAlive .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)//长连接配置,即获取长连接生产多长时间 .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))//设置重试次数,默认为3次;当前是禁用掉 .build(); /** *JVM停止或重启时,关闭连接池释放掉连接 */ Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { closeableHttpClient.close(); log.info("http client closed"); } catch (IOException e) { log.error(e.getMessage(), e); } } }); return closeableHttpClient; }
Example #11
Source File: RestTemplateConfig.java From spring-boot-cookbook with Apache License 2.0 | 4 votes |
private CloseableHttpClient getHttpClient() { //注册访问协议相关的Socket工厂 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); //HttpConnectionFactory:配置写请求/解析响应处理器 HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory = new ManagedHttpClientConnectionFactory( DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE ); //DNS解析器 DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE; //创建连接池管理器 PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connectionFactory, dnsResolver); //设置默认的socket参数 manager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); manager.setMaxTotal(300);//设置最大连接数。高于这个值时,新连接请求,需要阻塞,排队等待 //路由是对MaxTotal的细分。 // 每个路由实际最大连接数默认值是由DefaultMaxPerRoute控制。 // MaxPerRoute设置的过小,无法支持大并发:ConnectionPoolTimeoutException:Timeout waiting for connection from pool manager.setDefaultMaxPerRoute(200);//每个路由的最大连接 manager.setValidateAfterInactivity(5 * 1000);//在从连接池获取连接时,连接不活跃多长时间后需要进行一次验证,默认为2s //配置默认的请求参数 RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(2 * 1000)//连接超时设置为2s .setSocketTimeout(5 * 1000)//等待数据超时设置为5s .setConnectionRequestTimeout(2 * 1000)//从连接池获取连接的等待超时时间设置为2s // .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.2", 1234))) //设置代理 .build(); CloseableHttpClient closeableHttpClient = HttpClients.custom() .setConnectionManager(manager) .setConnectionManagerShared(false)//连接池不是共享模式,这个共享是指与其它httpClient是否共享 .evictIdleConnections(60, TimeUnit.SECONDS)//定期回收空闲连接 .evictExpiredConnections()//回收过期连接 .setConnectionTimeToLive(60, TimeUnit.SECONDS)//连接存活时间,如果不设置,则根据长连接信息决定 .setDefaultRequestConfig(defaultRequestConfig)//设置默认的请求参数 .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)//连接重用策略,即是否能keepAlive .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)//长连接配置,即获取长连接生产多长时间 .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))//设置重试次数,默认为3次;当前是禁用掉 .build(); /** *JVM停止或重启时,关闭连接池释放掉连接 */ Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { LOGGER.info("closing http client"); closeableHttpClient.close(); LOGGER.info("http client closed"); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } }); return closeableHttpClient; }