List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager setDefaultMaxPerRoute
public void setDefaultMaxPerRoute(final int max)
From source file:com.ea.core.bridge.ws.rest.client.AbstractRestClient.java
public AbstractRestClient(URL httpUrl) { super(httpUrl); HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory() { @Override//from w w w . j a v a2s . c om public HttpMessageParser<HttpResponse> create(SessionInputBuffer buffer, MessageConstraints constraints) { LineParser lineParser = new BasicLineParser() { @Override public Header parseHeader(final CharArrayBuffer buffer) { try { return super.parseHeader(buffer); } catch (ParseException ex) { return new BasicHeader(buffer.toString(), null); } } }; return new DefaultHttpResponseParser(buffer, lineParser, DefaultHttpResponseFactory.INSTANCE, constraints) { @Override protected boolean reject(final CharArrayBuffer line, int count) { // try to ignore all garbage preceding a status line infinitely return false; } }; } }; HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory(); HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); SSLContext sslcontext = SSLContexts.createSystemDefault(); X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier(); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext, hostnameVerifier)).build(); DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("myhost") || host.equalsIgnoreCase("localhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) }; } else { return super.resolve(host); } } }; PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, connFactory, dnsResolver); SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build(); connManager.setDefaultSocketConfig(socketConfig); connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig); MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(200) .setMaxLineLength(2000).build(); ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints).build(); connManager.setDefaultConnectionConfig(connectionConfig); connManager.setConnectionConfig(new HttpHost("somehost", 80), ConnectionConfig.DEFAULT); connManager.setMaxTotal(100); connManager.setDefaultMaxPerRoute(10); connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20); CookieStore cookieStore = new BasicCookieStore(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH) .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setConnectionRequestTimeout(3000) .setConnectTimeout(3000).setSocketTimeout(3000).build(); client = HttpClients.custom().setConnectionManager(connManager).setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider) // .setProxy(new HttpHost("myproxy", 8080)) .setDefaultRequestConfig(defaultRequestConfig).build(); }
From source file:com.unboundid.scim.tools.SCIMQueryRate.java
/** * Performs the actual processing for this tool. In this case, it gets a * connection to the directory server and uses it to perform the requested * searches./* www . j a v a 2 s. com*/ * * @return The result code for the processing that was performed. */ @Override() public ResultCode doToolProcessing() { //Initalize the Debugger Debug.setEnabled(true); Debug.getLogger().addHandler(new ConsoleHandler()); Debug.getLogger().setUseParentHandlers(false); // Determine the random seed to use. final Long seed; if (randomSeed.isPresent()) { seed = Long.valueOf(randomSeed.getValue()); } else { seed = null; } // Create a value pattern for the filter. final ValuePattern filterPattern; boolean isQuery = true; if (filter.isPresent()) { try { filterPattern = new ValuePattern(filter.getValue(), seed); } catch (ParseException pe) { Debug.debugException(pe); err(ERR_QUERY_TOOL_BAD_FILTER_PATTERN.get(pe.getMessage())); return ResultCode.PARAM_ERROR; } } else if (resourceId.isPresent()) { isQuery = false; try { filterPattern = new ValuePattern(resourceId.getValue()); } catch (ParseException pe) { Debug.debugException(pe); err(ERR_QUERY_TOOL_BAD_RESOURCE_ID_PATTERN.get(pe.getMessage())); return ResultCode.PARAM_ERROR; } } else { filterPattern = null; } // Get the attributes to return. final String[] attrs; if (attributes.isPresent()) { final List<String> attrList = attributes.getValues(); attrs = new String[attrList.size()]; attrList.toArray(attrs); } else { attrs = NO_STRINGS; } // If the --ratePerSecond option was specified, then limit the rate // accordingly. FixedRateBarrier fixedRateBarrier = null; if (ratePerSecond.isPresent()) { final int intervalSeconds = collectionInterval.getValue(); final int ratePerInterval = ratePerSecond.getValue() * intervalSeconds; fixedRateBarrier = new FixedRateBarrier(1000L * intervalSeconds, ratePerInterval); } // Determine whether to include timestamps in the output and if so what // format should be used for them. final boolean includeTimestamp; final String timeFormat; if (timestampFormat.getValue().equalsIgnoreCase("with-date")) { includeTimestamp = true; timeFormat = "dd/MM/yyyy HH:mm:ss"; } else if (timestampFormat.getValue().equalsIgnoreCase("without-date")) { includeTimestamp = true; timeFormat = "HH:mm:ss"; } else { includeTimestamp = false; timeFormat = null; } // Determine whether any warm-up intervals should be run. final long totalIntervals; final boolean warmUp; int remainingWarmUpIntervals = warmUpIntervals.getValue(); if (remainingWarmUpIntervals > 0) { warmUp = true; totalIntervals = 0L + numIntervals.getValue() + remainingWarmUpIntervals; } else { warmUp = true; totalIntervals = 0L + numIntervals.getValue(); } // Create the table that will be used to format the output. final OutputFormat outputFormat; if (csvFormat.isPresent()) { outputFormat = OutputFormat.CSV; } else { outputFormat = OutputFormat.COLUMNS; } final ColumnFormatter formatter = new ColumnFormatter(includeTimestamp, timeFormat, outputFormat, " ", new FormattableColumn(15, HorizontalAlignment.RIGHT, "Recent", "Queries/Sec"), new FormattableColumn(15, HorizontalAlignment.RIGHT, "Recent", "Avg Dur ms"), new FormattableColumn(15, HorizontalAlignment.RIGHT, "Recent", "Resources/Query"), new FormattableColumn(15, HorizontalAlignment.RIGHT, "Recent", "Errors/Sec"), new FormattableColumn(15, HorizontalAlignment.RIGHT, "Overall", "Queries/Sec"), new FormattableColumn(15, HorizontalAlignment.RIGHT, "Overall", "Avg Dur ms")); // Create values to use for statistics collection. final AtomicLong queryCounter = new AtomicLong(0L); final AtomicLong resourceCounter = new AtomicLong(0L); final AtomicLong errorCounter = new AtomicLong(0L); final AtomicLong queryDurations = new AtomicLong(0L); // Determine the length of each interval in milliseconds. final long intervalMillis = 1000L * collectionInterval.getValue(); // We will use Apache's HttpClient library for this tool. SSLUtil sslUtil; try { sslUtil = createSSLUtil(); } catch (LDAPException e) { debugException(e); err(e.getMessage()); return e.getResultCode(); } RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create(); final String schemeName; if (sslUtil != null) { try { SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory( sslUtil.createSSLContext("TLS"), new NoopHostnameVerifier()); schemeName = "https"; registryBuilder.register(schemeName, sslConnectionSocketFactory); } catch (GeneralSecurityException e) { debugException(e); err(ERR_SCIM_TOOL_CANNOT_CREATE_SSL_CONTEXT.get(getExceptionMessage(e))); return ResultCode.LOCAL_ERROR; } } else { schemeName = "http"; registryBuilder.register(schemeName, new PlainConnectionSocketFactory()); } final Registry<ConnectionSocketFactory> socketFactoryRegistry = registryBuilder.build(); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30000) .setExpectContinueEnabled(true).build(); SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(30000).setSoReuseAddress(true).build(); final PoolingHttpClientConnectionManager mgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry); mgr.setMaxTotal(numThreads.getValue()); mgr.setDefaultMaxPerRoute(numThreads.getValue()); mgr.setDefaultSocketConfig(socketConfig); mgr.setValidateAfterInactivity(-1); ClientConfig jerseyConfig = new ClientConfig(); jerseyConfig.property(ApacheClientProperties.CONNECTION_MANAGER, mgr); jerseyConfig.property(ApacheClientProperties.REQUEST_CONFIG, requestConfig); ApacheConnectorProvider connectorProvider = new ApacheConnectorProvider(); jerseyConfig.connectorProvider(connectorProvider); if (authID.isPresent()) { try { final String password; if (authPassword.isPresent()) { password = authPassword.getValue(); } else if (authPasswordFile.isPresent()) { password = authPasswordFile.getNonBlankFileLines().get(0); } else { password = null; } BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(new AuthScope(host.getValue(), port.getValue()), new UsernamePasswordCredentials(authID.getValue(), password)); jerseyConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, provider); jerseyConfig.property(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, true); } catch (IOException e) { Debug.debugException(e); err(ERR_QUERY_TOOL_SET_BASIC_AUTH.get(e.getMessage())); return ResultCode.LOCAL_ERROR; } } else if (bearerToken.isPresent()) { jerseyConfig.register(new ClientRequestFilter() { public void filter(final ClientRequestContext clientRequestContext) throws IOException { try { clientRequestContext.getHeaders().add("Authorization", "Bearer " + bearerToken.getValue()); } catch (Exception ex) { throw new RuntimeException("Unable to add authorization handler", ex); } } }); } // Create the SCIM client to use for the queries. final URI uri; try { final String path; if (contextPath.getValue().startsWith("/")) { path = contextPath.getValue(); } else { path = "/" + contextPath.getValue(); } uri = new URI(schemeName, null, host.getValue(), port.getValue(), path, null, null); } catch (URISyntaxException e) { Debug.debugException(e); err(ERR_QUERY_TOOL_CANNOT_CREATE_URL.get(e.getMessage())); return ResultCode.OTHER; } final SCIMService service = new SCIMService(uri, jerseyConfig); if (xmlFormat.isPresent()) { service.setContentType(MediaType.APPLICATION_XML_TYPE); service.setAcceptType(MediaType.APPLICATION_XML_TYPE); } // Retrieve the resource schema. final ResourceDescriptor resourceDescriptor; try { resourceDescriptor = service.getResourceDescriptor(resourceName.getValue(), null); if (resourceDescriptor == null) { throw new ResourceNotFoundException( "Resource " + resourceName.getValue() + " is not defined by the service provider"); } } catch (SCIMException e) { Debug.debugException(e); err(ERR_QUERY_TOOL_RETRIEVE_RESOURCE_SCHEMA.get(e.getMessage())); return ResultCode.OTHER; } final SCIMEndpoint<? extends BaseResource> endpoint = service.getEndpoint(resourceDescriptor, BaseResource.BASE_RESOURCE_FACTORY); // Create the threads to use for the searches. final CyclicBarrier barrier = new CyclicBarrier(numThreads.getValue() + 1); final QueryRateThread[] threads = new QueryRateThread[numThreads.getValue()]; for (int i = 0; i < threads.length; i++) { threads[i] = new QueryRateThread(i, isQuery, endpoint, filterPattern, attrs, barrier, queryCounter, resourceCounter, queryDurations, errorCounter, fixedRateBarrier); threads[i].start(); } // Display the table header. for (final String headerLine : formatter.getHeaderLines(true)) { out(headerLine); } // Indicate that the threads can start running. try { barrier.await(); } catch (Exception e) { Debug.debugException(e); } long overallStartTime = System.nanoTime(); long nextIntervalStartTime = System.currentTimeMillis() + intervalMillis; boolean setOverallStartTime = false; long lastDuration = 0L; long lastNumEntries = 0L; long lastNumErrors = 0L; long lastNumSearches = 0L; long lastEndTime = System.nanoTime(); for (long i = 0; i < totalIntervals; i++) { final long startTimeMillis = System.currentTimeMillis(); final long sleepTimeMillis = nextIntervalStartTime - startTimeMillis; nextIntervalStartTime += intervalMillis; try { if (sleepTimeMillis > 0) { Thread.sleep(sleepTimeMillis); } } catch (Exception e) { Debug.debugException(e); } final long endTime = System.nanoTime(); final long intervalDuration = endTime - lastEndTime; final long numSearches; final long numEntries; final long numErrors; final long totalDuration; if (warmUp && (remainingWarmUpIntervals > 0)) { numSearches = queryCounter.getAndSet(0L); numEntries = resourceCounter.getAndSet(0L); numErrors = errorCounter.getAndSet(0L); totalDuration = queryDurations.getAndSet(0L); } else { numSearches = queryCounter.get(); numEntries = resourceCounter.get(); numErrors = errorCounter.get(); totalDuration = queryDurations.get(); } final long recentNumSearches = numSearches - lastNumSearches; final long recentNumEntries = numEntries - lastNumEntries; final long recentNumErrors = numErrors - lastNumErrors; final long recentDuration = totalDuration - lastDuration; final double numSeconds = intervalDuration / 1000000000.0d; final double recentSearchRate = recentNumSearches / numSeconds; final double recentErrorRate = recentNumErrors / numSeconds; final double recentAvgDuration; final double recentEntriesPerSearch; if (recentNumSearches > 0L) { recentEntriesPerSearch = 1.0d * recentNumEntries / recentNumSearches; recentAvgDuration = 1.0d * recentDuration / recentNumSearches / 1000000; } else { recentEntriesPerSearch = 0.0d; recentAvgDuration = 0.0d; } if (warmUp && (remainingWarmUpIntervals > 0)) { out(formatter.formatRow(recentSearchRate, recentAvgDuration, recentEntriesPerSearch, recentErrorRate, "warming up", "warming up")); remainingWarmUpIntervals--; if (remainingWarmUpIntervals == 0) { out(INFO_QUERY_TOOL_WARM_UP_COMPLETED.get()); setOverallStartTime = true; } } else { if (setOverallStartTime) { overallStartTime = lastEndTime; setOverallStartTime = false; } final double numOverallSeconds = (endTime - overallStartTime) / 1000000000.0d; final double overallSearchRate = numSearches / numOverallSeconds; final double overallAvgDuration; if (numSearches > 0L) { overallAvgDuration = 1.0d * totalDuration / numSearches / 1000000; } else { overallAvgDuration = 0.0d; } out(formatter.formatRow(recentSearchRate, recentAvgDuration, recentEntriesPerSearch, recentErrorRate, overallSearchRate, overallAvgDuration)); lastNumSearches = numSearches; lastNumEntries = numEntries; lastNumErrors = numErrors; lastDuration = totalDuration; } lastEndTime = endTime; } // Stop all of the threads. ResultCode resultCode = ResultCode.SUCCESS; for (final QueryRateThread t : threads) { t.signalShutdown(); } // Interrupt any blocked threads after a grace period. final WakeableSleeper sleeper = new WakeableSleeper(); sleeper.sleep(1000); mgr.shutdown(); for (final QueryRateThread t : threads) { final ResultCode r = t.waitForShutdown(); if (resultCode == ResultCode.SUCCESS) { resultCode = r; } } return resultCode; }
From source file:org.kuali.rice.ksb.messaging.serviceconnectors.DefaultHttpClientConfigurer.java
/** * Builds the HttpClientConnectionManager. * * <p>Note that this calls {@link #buildSslConnectionSocketFactory()} and registers the resulting {@link SSLConnectionSocketFactory} * (if non-null) with its socket factory registry.</p> * * @return the HttpClientConnectionManager *///from ww w . j a v a2 s . c o m protected HttpClientConnectionManager buildConnectionManager() { PoolingHttpClientConnectionManager poolingConnectionManager = null; SSLConnectionSocketFactory sslConnectionSocketFactory = buildSslConnectionSocketFactory(); if (sslConnectionSocketFactory != null) { Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("https", sslConnectionSocketFactory) .register("http", new PlainConnectionSocketFactory()).build(); poolingConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); } else { poolingConnectionManager = new PoolingHttpClientConnectionManager(); } // Configure the connection manager poolingConnectionManager .setMaxTotal(MAX_TOTAL_CONNECTIONS.getValueOrDefault(DEFAULT_MAX_TOTAL_CONNECTIONS)); // By default we'll set the max connections per route (essentially that means per host for us) to the max total poolingConnectionManager .setDefaultMaxPerRoute(MAX_TOTAL_CONNECTIONS.getValueOrDefault(DEFAULT_MAX_TOTAL_CONNECTIONS)); SocketConfig.Builder socketConfigBuilder = SocketConfig.custom(); socketConfigBuilder.setSoTimeout(SO_TIMEOUT.getValueOrDefault(DEFAULT_SOCKET_TIMEOUT)); Integer soLinger = SO_LINGER.getValue(); if (soLinger != null) { socketConfigBuilder.setSoLinger(soLinger); } Boolean isTcpNoDelay = TCP_NODELAY.getValue(); if (isTcpNoDelay != null) { socketConfigBuilder.setTcpNoDelay(isTcpNoDelay); } poolingConnectionManager.setDefaultSocketConfig(socketConfigBuilder.build()); ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom(); Integer sendBuffer = SO_SNDBUF.getValue(); Integer receiveBuffer = SO_RCVBUF.getValue(); // if either send or recieve buffer size is set, we'll set the buffer size to whichever is greater if (sendBuffer != null || receiveBuffer != null) { Integer bufferSize = -1; if (sendBuffer != null) { bufferSize = sendBuffer; } if (receiveBuffer != null && receiveBuffer > bufferSize) { bufferSize = receiveBuffer; } connectionConfigBuilder.setBufferSize(bufferSize); } String contentCharset = HTTP_CONTENT_CHARSET.getValue(); if (contentCharset != null) { connectionConfigBuilder.setCharset(Charset.forName(contentCharset)); } poolingConnectionManager.setDefaultConnectionConfig(connectionConfigBuilder.build()); return poolingConnectionManager; }
From source file:io.mandrel.requests.http.ApacheHttpRequester.java
public void init() { available = new Semaphore(maxParallel(), true); SSLContext sslContext = SSLContexts.createSystemDefault(); HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(); Registry<ConnectionSocketFactory> sessionStrategyRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build(); DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override//from ww w .ja va2s . co m public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("localhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) }; } else { return new InetAddress[] { nameResolver().resolve(host) }; } } }; // Create a connection manager with custom configuration. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( sessionStrategyRegistry, dnsResolver); // Create message constraints MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(maxHeaderCount) .setMaxLineLength(maxLineLength).build(); // Create connection configuration ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints).build(); 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(maxPersistentConnections()); connManager.setDefaultMaxPerRoute(maxPersistentConnections()); // TODO // Use custom credentials provider if necessary. // CredentialsProvider credentialsProvider = new // BasicCredentialsProvider(); // Create global request configuration defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setMaxRedirects(maxRedirects()) .setSocketTimeout(socketTimeout()).setConnectTimeout(connectTimeout()) .setConnectionRequestTimeout(requestTimeOut()).setRedirectsEnabled(followRedirects()).build(); // Create an HttpClient with the given custom dependencies and // configuration. client = HttpClients.custom().setConnectionManager(connManager) // .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(defaultRequestConfig).build(); }
From source file:com.jivesoftware.os.routing.bird.http.client.HttpClientFactoryProvider.java
public HttpClientFactory createHttpClientFactory(Collection<HttpClientConfiguration> configurations, boolean latentClient) { HttpClientConfig httpClientConfig = locateConfig(configurations, HttpClientConfig.class, HttpClientConfig.newBuilder().build()); HttpClientSSLConfig sslConfig = locateConfig(configurations, HttpClientSSLConfig.class, null); String scheme;//from w ww.j a va 2 s. c om PoolingHttpClientConnectionManager poolingHttpClientConnectionManager; if (sslConfig != null && sslConfig.isUseSsl()) { LayeredConnectionSocketFactory sslSocketFactory; if (sslConfig.getCustomSSLSocketFactory() != null) { sslSocketFactory = sslConfig.getCustomSSLSocketFactory(); } else { sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); } scheme = "https"; poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory).build()); } else { scheme = "http"; poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); } if (httpClientConfig.getMaxConnections() > 0) { poolingHttpClientConnectionManager.setMaxTotal(httpClientConfig.getMaxConnections()); } else { poolingHttpClientConnectionManager.setMaxTotal(Integer.MAX_VALUE); } if (httpClientConfig.getMaxConnectionsPerHost() > 0) { poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpClientConfig.getMaxConnectionsPerHost()); } else { poolingHttpClientConnectionManager.setDefaultMaxPerRoute(Integer.MAX_VALUE); } poolingHttpClientConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout( httpClientConfig.getSocketTimeoutInMillis() > 0 ? httpClientConfig.getSocketTimeoutInMillis() : 0) .build()); Closeable closeable; HttpClientConnectionManager clientConnectionManager; clientConnectionManager = poolingHttpClientConnectionManager; closeable = poolingHttpClientConnectionManager; return (OAuthSigner signer, String host, int port) -> { HttpClientBuilder httpClientBuilder = HttpClients.custom() .setConnectionManager(clientConnectionManager); CloseableHttpClient client = httpClientBuilder.build(); HttpClient httpClient = new ApacheHttpClient441BackedHttpClient(scheme, host, port, signer, client, closeable, httpClientConfig.getCopyOfHeadersForEveryRequest()); if (latentClient) { httpClient = new LatentHttpClient(httpClient); } return httpClient; }; }
From source file:com.thinkbiganalytics.rest.JerseyRestClient.java
public JerseyRestClient(JerseyClientConfig config) { useConnectionPooling = config.isUseConnectionPooling(); SSLContext sslContext = null; if (config.isHttps()) { SslConfigurator sslConfig = null; byte[] keyStoreFile = null; byte[] truststoreFile = null; try {/*from w w w . ja v a2s . c om*/ if (StringUtils.isNotBlank(config.getKeystorePath())) { InputStream keystore = JerseyRestClient.class.getResourceAsStream(config.getKeystorePath()); if (keystore != null) { keyStoreFile = ByteStreams.toByteArray(keystore); } } } catch (IOException e) { } try { if (StringUtils.isNotBlank(config.getTruststorePath())) { InputStream truststore = JerseyRestClient.class.getResourceAsStream(config.getTruststorePath()); if (truststore != null) { truststoreFile = ByteStreams.toByteArray(truststore); } } } catch (IOException e) { } if (keyStoreFile != null) { sslConfig = SslConfigurator.newInstance() .trustStoreBytes(truststoreFile != null ? truststoreFile : keyStoreFile) .trustStorePassword(config.getTruststorePassword() != null ? config.getTruststorePassword() : config.getKeystorePassword()) .trustStoreType(config.getTrustStoreType()) .keyStoreBytes(keyStoreFile != null ? keyStoreFile : truststoreFile) .keyStorePassword(config.getKeystorePassword()); } else { sslConfig = SslConfigurator.newInstance() .keyStoreFile(config.getKeystorePath() == null ? config.getTruststorePath() : config.getKeystorePath()) .keyStorePassword(config.getKeystorePassword() == null ? config.getTruststorePassword() : config.getKeystorePassword()) .trustStoreFile(config.getTruststorePath() == null ? config.getKeystorePath() : config.getTruststorePath()) .trustStorePassword(config.getTruststorePassword() == null ? config.getKeystorePassword() : config.getTruststorePassword()) .trustStoreType(config.getTrustStoreType()); } try { sslContext = sslConfig.createSSLContext(); } catch (Exception e) { log.error("ERROR creating CLient SSL Context. " + e.getMessage() + " Falling back to Jersey Client without SSL. Rest Integration with '" + config.getUrl() + "' will probably not work until this is fixed!"); } } ClientConfig clientConfig = new ClientConfig(); // Add in Timeouts if configured. Values are in milliseconds if (config.getReadTimeout() != null) { clientConfig.property(ClientProperties.READ_TIMEOUT, config.getReadTimeout()); } if (config.getConnectTimeout() != null) { clientConfig.property(ClientProperties.CONNECT_TIMEOUT, config.getConnectTimeout()); } if (useConnectionPooling) { PoolingHttpClientConnectionManager connectionManager = null; if (sslContext != null) { HostnameVerifier defaultHostnameVerifier = new DefaultHostnameVerifier(); LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, defaultHostnameVerifier); final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory).build(); connectionManager = new PoolingHttpClientConnectionManager(registry); } else { connectionManager = new PoolingHttpClientConnectionManager(); } connectionManager.setDefaultMaxPerRoute(100); // # of connections allowed per host/address connectionManager.setMaxTotal(200); // number of connections allowed in total clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager); HttpUrlConnectorProvider connectorProvider = new HttpUrlConnectorProvider(); clientConfig.connectorProvider(connectorProvider); } clientConfig.register(MultiPartFeature.class); // allow derived classes to modify the client config extendClientConfig(clientConfig); if (sslContext != null) { log.info("Created new Jersey Client with SSL connecting to {} ", config.getUrl()); client = new JerseyClientBuilder().withConfig(clientConfig).sslContext(sslContext).build(); } else { log.info("Created new Jersey Client without SSL connecting to {} ", config.getUrl()); client = JerseyClientBuilder.createClient(clientConfig); } // Register Jackson for the internal mapper objectMapper = new JacksonObjectMapperProvider().getContext(null); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //register custom features registerClientFeatures(client); // Configure authentication if (StringUtils.isNotBlank(config.getUsername())) { HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(config.getUsername(), config.getPassword()); client.register(feature); } this.uri = config.getUrl(); this.username = config.getUsername(); if (StringUtils.isNotBlank(config.getHost()) && !HOST_NOT_SET_VALUE.equals(config.getHost())) { this.isHostConfigured = true; } else { log.info("Jersey Rest Client not initialized. Host name is Not set!!"); } }
From source file:com.ibm.og.client.ApacheClient.java
private HttpClientConnectionManager createConnectionManager() { final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", createPlainConnectionSocketFactory()) .register("https", createSslConnectionSocketFactory()).build(), null, null, null, -1, TimeUnit.MILLISECONDS); manager.setDefaultSocketConfig(createSocketConfig()); manager.setMaxTotal(Integer.MAX_VALUE); manager.setDefaultMaxPerRoute(Integer.MAX_VALUE); manager.setValidateAfterInactivity(this.validateAfterInactivity); return manager; }
From source file:com.uber.stream.kafka.mirrormaker.manager.core.ControllerHelixManager.java
public ControllerHelixManager(SourceKafkaClusterValidationManager srcKafkaValidationManager, ManagerConf managerConf) {// w w w. jav a 2 s .c o m _conf = managerConf; _enableRebalance = managerConf.getEnableRebalance(); _srcKafkaValidationManager = srcKafkaValidationManager; _initMaxNumPartitionsPerRoute = managerConf.getInitMaxNumPartitionsPerRoute(); _maxNumPartitionsPerRoute = managerConf.getMaxNumPartitionsPerRoute(); _initMaxNumWorkersPerRoute = managerConf.getInitMaxNumWorkersPerRoute(); _maxNumWorkersPerRoute = managerConf.getMaxNumWorkersPerRoute(); _workloadRefreshPeriodInSeconds = managerConf.getWorkloadRefreshPeriodInSeconds(); _workerHelixManager = new WorkerHelixManager(managerConf); _pipelineWorkloadMap = new ConcurrentHashMap<>(); _helixZkURL = HelixUtils.getAbsoluteZkPathForHelix(managerConf.getManagerZkStr()); _helixClusterName = MANAGER_CONTROLLER_HELIX_PREFIX + "-" + managerConf.getManagerDeployment(); _instanceId = managerConf.getManagerInstanceId(); _topicToPipelineInstanceMap = new ConcurrentHashMap<>(); _pipelineToInstanceMap = new ConcurrentHashMap<>(); _availableControllerList = new ArrayList<>(); _routeToCounterMap = new ConcurrentHashMap<>(); _zkClient = new ZkClient(_helixZkURL, 30000, 30000, ZKStringSerializer$.MODULE$); registerMetrics(); PoolingHttpClientConnectionManager limitedConnMgr = new PoolingHttpClientConnectionManager(); // TODO: make it configurable limitedConnMgr.setDefaultMaxPerRoute(100); limitedConnMgr.setMaxTotal(100); _httpClient = HttpClients.createMinimal(limitedConnMgr); _controllerPort = managerConf.getControllerPort(); // requestConfig is immutable. These three timeouts are for // 1. getting connection from connection manager; // 2. establishing connection with server; // 3. getting next data snippet from server. _requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30000).setConnectTimeout(30000) .setSocketTimeout(30000).build(); }
From source file:com.spotify.docker.client.DefaultDockerClient.java
private PoolingHttpClientConnectionManager getConnectionManager(Builder builder) { final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager( getSchemeRegistry(builder)); // Use all available connections instead of artificially limiting ourselves to 2 per server. cm.setMaxTotal(builder.connectionPoolSize); cm.setDefaultMaxPerRoute(cm.getMaxTotal()); return cm;//from w ww . j a v a 2 s .c om }
From source file:de.tudarmstadt.ukp.shibhttpclient.ShibHttpClient.java
/** * Create a new client (with an explicit proxy and possibly transparent authentication) * /* w ww . ja va2s .c om*/ * @param aIdpUrl * the URL of the IdP. Should probably be something ending in "/SAML2/SOAP/ECP" * @param aUsername * the user name to log into the IdP. * @param aPassword * the password to log in to the IdP. * @param aProxy * if not {@code null}, use this proxy instead of the default system proxy (if any) * @param anyCert * if {@code true}, accept any certificate from any remote host. Otherwise, * certificates need to be installed in the JRE. * @param transparentAuth * if {@code true} (default), add a HttpRequestPostProcessor to transparently * authenticate. Otherwise, you must handle the authentication process yourself. */ public ShibHttpClient(String aIdpUrl, String aUsername, String aPassword, HttpHost aProxy, boolean anyCert, boolean transparentAuth) { setIdpUrl(aIdpUrl); setUsername(aUsername); setPassword(aPassword); // Use a pooling connection manager, because we'll have to do a call out to the IdP // while still being in a connection with the SP PoolingHttpClientConnectionManager connMgr; if (anyCert) { try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()) .register("https", new SSLConnectionSocketFactory(builder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)) .build(); connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry); } catch (GeneralSecurityException e) { // There shouldn't be any of these exceptions, because we do not use an actual // keystore throw new IllegalStateException(e); } } else { connMgr = new PoolingHttpClientConnectionManager(); } connMgr.setMaxTotal(10); connMgr.setDefaultMaxPerRoute(5); // The client needs to remember the auth cookie cookieStore = new BasicCookieStore(); RequestConfig globalRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY) .build(); // Let's throw all common client elements into one builder object HttpClientBuilder customClient = HttpClients.custom().setConnectionManager(connMgr) // The client needs to remember the auth cookie .setDefaultRequestConfig(globalRequestConfig).setDefaultCookieStore(cookieStore) // Add the ECP/PAOS headers - needs to be added first so the cookie we get from // the authentication can be handled by the RequestAddCookies interceptor later .addInterceptorFirst(new HttpRequestPreprocessor()); // Automatically log into IdP if transparent Shibboleth authentication handling is requested (default) if (transparentAuth) { customClient = customClient.addInterceptorFirst(new HttpRequestPostprocessor()); } // Build the client with/without proxy settings if (aProxy == null) { // use the proxy settings of the JVM, if specified client = customClient.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())) .build(); } else { // use the explicit proxy client = customClient.setProxy(aProxy).build(); } parserPool = new BasicParserPool(); parserPool.setNamespaceAware(true); }