public IdleAsyncConnectionEvictor( final NHttpClientConnectionManager connectionManager, final ThreadFactory threadFactory, final long sleepTime, final TimeUnit sleepTimeUnit, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { ThreadFactory threadFactory1 = threadFactory != null ? threadFactory : new DefaultThreadFactory(); this.sleepTimeMs = sleepTimeUnit != null ? sleepTimeUnit.toMillis(sleepTime) : sleepTime; this.maxIdleTimeMs = maxIdleTimeUnit != null ? maxIdleTimeUnit.toMillis(maxIdleTime) : maxIdleTime; this.thread = threadFactory1.newThread(new Runnable() { @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { Thread.sleep(sleepTimeMs); connectionManager.closeExpiredConnections(); if (maxIdleTimeMs > 0) { connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS); } } } catch (final Exception ex) { log.warn("Evictor exception", ex); } } }); }
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; }
public IdleAsyncConnectionEvictor( final NHttpClientConnectionManager connectionManager, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { this(connectionManager, null, maxIdleTime > 0 ? maxIdleTime : 5, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS, maxIdleTime, maxIdleTimeUnit); }
public NIdleConnectionEvictor( final NHttpClientConnectionManager connMgr, final ThreadFactory threadFactory, final long sleepTime, final TimeUnit sleepTimeUnit, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { //this.connMgr = Args.notNull(connMgr, "Connection manager"); this.connMgr = connMgr; this.threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory(); this.sleepTimeMs = sleepTimeUnit != null ? sleepTimeUnit.toMillis(sleepTime) : sleepTime; this.maxIdleTimeMs = maxIdleTimeUnit != null ? maxIdleTimeUnit.toMillis(maxIdleTime) : maxIdleTime; this.thread = this.threadFactory.newThread(new Runnable() { @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { Thread.sleep(sleepTimeMs); connMgr.closeExpiredConnections(); if (maxIdleTimeMs > 0) { connMgr.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS); } } } catch (Exception ex) { exception = ex; } } }); }
public NIdleConnectionEvictor( final NHttpClientConnectionManager connMgr, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { this(connMgr, null, maxIdleTime > 0 ? maxIdleTime : 5, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS, maxIdleTime, maxIdleTimeUnit); }
/** * Create connection manager for asynchronous http client. * * @return Connection manager for asynchronous http client. * @throws IOReactorException in case if a non-recoverable I/O error. */ protected NHttpClientConnectionManager createNHttpClientConnectionManager() throws IOReactorException { ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(IOReactorConfig.custom() .setSoTimeout(this.config.getSocketTimeoutInMillis()).setTcpNoDelay(true).build()); PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor); connectionManager.setDefaultMaxPerRoute(this.config.getMaxConnections()); connectionManager.setMaxTotal(this.config.getMaxConnections()); return connectionManager; }
/** * 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(); }
/** * Factory method for Apache HttpAsyncClient * * @return */ protected CloseableHttpAsyncClient createHttpAsyncClient() { log.info("Creating HttpAsyncClient"); HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom(); clientBuilder.setSSLStrategy(createSSLIOSessionStrategy()); /* * defaultMaxPerRoute and maxTotal will be overridden by connection manager * if it is set. */ ConnectionSettings connSettings = createConnectionSettings(); clientBuilder.setConnectionManagerShared(connSettings.shared); clientBuilder.setMaxConnPerRoute(connSettings.defaultMaxPerRoute); clientBuilder.setMaxConnTotal(connSettings.maxTotal); NHttpClientConnectionManager connectionManager = createConnectionManager(); if(connectionManager != null){ log.info("Connection manager is set"); clientBuilder.setConnectionManager(connectionManager); } HttpHost proxy = detectHttpProxy(); if (proxy != null) { clientBuilder.setProxy(proxy); CredentialsProvider credsProvider = createDefaultCredentialsProvider( proxy.getHostName(), proxy.getPort()); if (credsProvider != null) { clientBuilder.setDefaultCredentialsProvider(credsProvider); } } String userAgent = getUserAgent(); log.debug("User-Agent: {}", userAgent); clientBuilder.setUserAgent(userAgent); clientBuilder.setDefaultRequestConfig(createDefaultRequestConfig()); return clientBuilder.build(); }
public IdleAsyncConnectionEvictor( final NHttpClientConnectionManager connectionManager, final long sleepTime, final TimeUnit sleepTimeUnit, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { this(connectionManager, null, sleepTime, sleepTimeUnit, maxIdleTime, maxIdleTimeUnit); }
public IdleConnectionEvictor(NHttpClientConnectionManager connMgr) { super(); this.connMgr = connMgr; }
public IdleConnectionEvictor(NHttpClientConnectionManager connMgr, long closePeriod) { this.connMgr = connMgr; this.closePeriod = closePeriod; }
public NIdleConnectionEvictor( final NHttpClientConnectionManager connMgr, final long sleepTime, final TimeUnit sleepTimeUnit, final long maxIdleTime, final TimeUnit maxIdleTimeUnit) { this(connMgr, null, sleepTime, sleepTimeUnit, maxIdleTime, maxIdleTimeUnit); }
public NIdleConnectionEvictor setConnMgr(final NHttpClientConnectionManager connMgr){ this.connMgr = Args.notNull(connMgr, "Connection manager"); return this; }
/** * Implement this to override connection manager settings * @return */ protected NHttpClientConnectionManager createConnectionManager() { return null; }