@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()); }
/** * */ 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(); }
public HttpFederationClient(HomeserverState global, FederationDomainResolver resolver) { this.global = global; this.resolver = resolver; try { SocketConfig sockConf = SocketConfig.custom().setSoTimeout(30000).build(); // FIXME properly handle SSL context by validating certificate hostname SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustAllStrategy()).build(); HostnameVerifier hostnameVerifier = new NoopHostnameVerifier(); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); this.client = HttpClientBuilder.create() .disableAuthCaching() .disableAutomaticRetries() .disableCookieManagement() .disableRedirectHandling() .setDefaultSocketConfig(sockConf) .setSSLSocketFactory(sslSocketFactory) .setUserAgent(global.getAppName() + "/" + global.getAppVersion()) .build(); } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) { throw new RuntimeException(e); } }
@Override public CrawlerHttpClient gen(CrawlerHttpClientBuilder proxyFeedBackDecorateHttpClientBuilder) { SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoLinger(-1).setSoReuseAddress(false) .setSoTimeout(ProxyConstant.SOCKETSO_TIMEOUT).setTcpNoDelay(true).build(); return proxyFeedBackDecorateHttpClientBuilder .setDefaultSocketConfig(socketConfig) // .setSSLSocketFactory(sslConnectionSocketFactory) // dungproxy0.0.6之后的版本,默认忽略https证书检查 .setRedirectStrategy(new LaxRedirectStrategy()) //注意,这里使用ua生产算法自动产生ua,如果是mobile,可以使用 // com.virjar.vscrawler.core.net.useragent.UserAgentBuilder.randomAppUserAgent() .setUserAgent(UserAgentBuilder.randomUserAgent()) //对于爬虫来说,连接池没啥卵用,直接禁止掉(因为我们可能创建大量HttpClient,每个HttpClient一个连接池,会把系统socket资源撑爆) //测试开80个httpClient抓数据大概一个小时系统就会宕机 .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE) .build(); }
public static CloseableHttpClient getHttpClient(HttpClientConnectionManager connectionManager, int maxConnections) { RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build(); ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build(); SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true) .setSoReuseAddress(true).build(); String userAgent = MessageFormat .format("MyCoRe/{0} ({1}; java {2})", MCRCoreVersion.getCompleteVersion(), MCRConfiguration.instance() .getString("MCR.NameOfProject", "undefined"), System.getProperty("java.version")); //setup http client return HttpClients.custom().setConnectionManager(connectionManager) .setUserAgent(userAgent).setRetryHandler(new MCRRetryHandler(maxConnections)) .setDefaultRequestConfig(requestConfig).setDefaultConnectionConfig(connectionConfig) .setDefaultSocketConfig(socketConfig).build(); }
public HttpRestClient(String user, String password) { SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(DEFAULT_SOCKET_TIMEOUT * 1000).build(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(DEFAULT_REQUEST_TIMEOUT * 1000) .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT * 1000) .setConnectionRequestTimeout(DEFAULT_REQUEST_TIMEOUT * 1000).build(); httpConnectionManager.setMaxTotal(DEFAULT_CONN_NUM); httpConnectionManager.setDefaultMaxPerRoute(DEFAULT_CONN_NUM); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, password)); client = HttpClients.custom().setConnectionManager(httpConnectionManager) .setDefaultRequestConfig(requestConfig) .setDefaultSocketConfig(socketConfig) .setDefaultCredentialsProvider(credsProvider).build(); }
public static RestOperations createCommonsHttpRestTemplate(int maxConnPerRoute, int maxConnTotal, int connectTimeout, int soTimeout, int retryTimes, RetryPolicyFactory retryPolicyFactory) { HttpClient httpClient = HttpClientBuilder.create() .setMaxConnPerRoute(maxConnPerRoute) .setMaxConnTotal(maxConnTotal) .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(soTimeout).build()) .setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(connectTimeout).build()) .build(); ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); RestTemplate restTemplate = new RestTemplate(factory); //set jackson mapper for (HttpMessageConverter<?> hmc : restTemplate.getMessageConverters()) { if (hmc instanceof MappingJackson2HttpMessageConverter) { ObjectMapper objectMapper = createObjectMapper(); MappingJackson2HttpMessageConverter mj2hmc = (MappingJackson2HttpMessageConverter) hmc; mj2hmc.setObjectMapper(objectMapper); } } return (RestOperations) Proxy.newProxyInstance(RestOperations.class.getClassLoader(), new Class[]{RestOperations.class}, new RetryableRestOperationsHandler(restTemplate, retryTimes, retryPolicyFactory)); }
@NotNull private HttpClient getClient(@NotNull SVNURL repositoryUrl) { // TODO: Implement algorithm of resolving necessary enabled protocols (TLSv1 vs SSLv3) instead of just using values from Settings. SSLContext sslContext = createSslContext(repositoryUrl); List<String> supportedProtocols = getSupportedSslProtocols(); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, ArrayUtil.toStringArray(supportedProtocols), null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // TODO: Seems more suitable here to read timeout values directly from config file - without utilizing SvnAuthenticationManager. final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (haveDataForTmpConfig()) { IdeHttpClientHelpers.ApacheHttpClient4.setProxyIfEnabled(requestConfigBuilder); IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsIfEnabled(credentialsProvider); } return HttpClients.custom() .setSSLSocketFactory(socketFactory) .setDefaultSocketConfig(SocketConfig.custom() .setSoTimeout(getAuthenticationManager().getReadTimeout(repositoryUrl)) .build()) .setDefaultRequestConfig(requestConfigBuilder .setConnectTimeout(getAuthenticationManager().getConnectTimeout(repositoryUrl)) .build()) .setDefaultCredentialsProvider(credentialsProvider) .build(); }
@Before public void setUp() throws Exception { final SocketConfig socketConfig = SocketConfig.custom() .setSoTimeout(15000) .build(); this.serverBootstrap = ServerBootstrap.bootstrap() .setSocketConfig(socketConfig) .setServerInfo(ORIGIN) .registerHandler("/echo/*", new EchoHandler()) .registerHandler("/random/*", new RandomHandler()); if (this.scheme.equals(ProtocolScheme.https)) { this.serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext()); } this.connManager = new PoolingHttpClientConnectionManager(); this.clientBuilder = HttpClientBuilder.create() .setDefaultSocketConfig(socketConfig) .setConnectionManager(this.connManager); }
@Test(expected=ConnectTimeoutException.class) public void testConnectTimeout() throws Exception { final HttpContext context = new BasicHttpContext(); final HttpHost host = new HttpHost("somehost"); final InetAddress ip1 = InetAddress.getByAddress(new byte[] {10, 0, 0, 1}); final InetAddress ip2 = InetAddress.getByAddress(new byte[] {10, 0, 0, 2}); Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] { ip1, ip2 }); Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); Mockito.when(schemePortResolver.resolve(host)).thenReturn(80); Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(plainSocketFactory.connectSocket( Mockito.anyInt(), Mockito.<Socket>any(), Mockito.<HttpHost>any(), Mockito.<InetSocketAddress>any(), Mockito.<InetSocketAddress>any(), Mockito.<HttpContext>any())).thenThrow(new SocketTimeoutException()); connectionOperator.connect(conn, host, null, 1000, SocketConfig.DEFAULT, context); }
@Test(expected=HttpHostConnectException.class) public void testConnectFailure() throws Exception { final HttpContext context = new BasicHttpContext(); final HttpHost host = new HttpHost("somehost"); final InetAddress ip1 = InetAddress.getByAddress(new byte[] {10, 0, 0, 1}); final InetAddress ip2 = InetAddress.getByAddress(new byte[] {10, 0, 0, 2}); Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] { ip1, ip2 }); Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); Mockito.when(schemePortResolver.resolve(host)).thenReturn(80); Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(plainSocketFactory.connectSocket( Mockito.anyInt(), Mockito.<Socket>any(), Mockito.<HttpHost>any(), Mockito.<InetSocketAddress>any(), Mockito.<InetSocketAddress>any(), Mockito.<HttpContext>any())).thenThrow(new ConnectException()); connectionOperator.connect(conn, host, null, 1000, SocketConfig.DEFAULT, context); }
protected final CloseableHttpClient getHttpClient(final boolean useSpnego) throws Exception { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); final HttpClientBuilder hcb = HttpClients.custom(); if (useSpnego) { //SPNEGO/Kerberos setup log.debug("SPNEGO activated"); final AuthSchemeProvider nsf = new SPNegoSchemeFactory(true);// new NegotiateSchemeProvider(); final Credentials jaasCreds = new JaasCredentials(); credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.SPNEGO), jaasCreds); credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.NTLM), new NTCredentials("Guest", "Guest", "Guest", "Guest")); final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create() .register(AuthSchemes.SPNEGO, nsf).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).build(); hcb.setDefaultAuthSchemeRegistry(authSchemeRegistry); } hcb.setDefaultCredentialsProvider(credsProvider); hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(10 * 1000).build()); final CloseableHttpClient httpClient = hcb.build(); return httpClient; }
protected HttpClient getHttpClient(Map<String, Object> options) throws GeneralSecurityException { SocketConfig socketConfig = SocketConfig.custom() .setSoKeepAlive(true).build(); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setDefaultSocketConfig(socketConfig); httpClientBuilder.disableAuthCaching(); httpClientBuilder.disableAutomaticRetries(); if(options.containsKey("sslVerify") && !Boolean.parseBoolean(options.get("sslVerify").toString())) { log.debug("Disabling all SSL certificate verification."); SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }); httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); httpClientBuilder.setSSLContext(sslContextBuilder.build()); } return httpClientBuilder.build(); }
@Override public void connect( final ManagedHttpClientConnection conn, final HttpHost host, final InetSocketAddress localAddress, final int connectTimeout, final SocketConfig socketConfig, final HttpContext context) throws IOException { try { super.connect(conn, host, localAddress, connectTimeout, socketConfig, context); } catch (SSLProtocolException e) { Boolean enableSniValue = (Boolean) context.getAttribute(SSLConnectionSocketFactoryImpl.ENABLE_SNI); boolean enableSni = enableSniValue == null || enableSniValue; if (enableSni && e.getMessage() != null && e.getMessage().equals("handshake alert: unrecognized_name")) { //print.e("Server received saw wrong SNI host, retrying without SNI"); context.setAttribute(SSLConnectionSocketFactoryImpl.ENABLE_SNI, false); super.connect(conn, host, localAddress, connectTimeout, socketConfig, context); } else throw e; } }
protected AbstractMigrateUsersToIncludeUsernames() { final String host = System.getProperty(COMMUNITY_HOST); final int port = Integer.parseInt(System.getProperty(COMMUNITY_PORT)); final AciServerDetails.TransportProtocol transportProtocol = AciServerDetails.TransportProtocol.valueOf(System.getProperty(COMMUNITY_PROTOCOL)); serverDetails = new AciServerDetails(transportProtocol, host, port); processorFactory = new ProcessorFactoryImpl(new Jaxb2MarshallerFactory()); final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(HTTP_SOCKET_TIMEOUT).build(); final HttpClient httpClient = HttpClientBuilder.create() .setMaxConnPerRoute(MAX_CONNECTIONS_PER_ROUTE) .setMaxConnTotal(MAX_CONNECTIONS_TOTAL) .setDefaultSocketConfig(socketConfig) .build(); final AciHttpClient aciHttpClient = new AciHttpClientImpl(httpClient); aciService = new AciServiceImpl(aciHttpClient, serverDetails); }
@Autowired public DhisWebServiceImpl(@Qualifier("dhisSettingsService") SettingsService settingsService, StatusMessageService statusMessageService, HttpClientBuilderFactory httpClientBuilderFactory) throws InterruptedException { this.settingsService = settingsService; this.statusMessageService = statusMessageService; SocketConfig socketConfig = SocketConfig.custom() .setSoTimeout(SO_TIMEOUT) .build(); poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); poolingHttpClientConnectionManager.setMaxTotal(MAX_CONNECTIONS); poolingHttpClientConnectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE); poolingHttpClientConnectionManager.setDefaultSocketConfig(socketConfig); this.client = httpClientBuilderFactory.newBuilder() .setConnectionManager(poolingHttpClientConnectionManager) .build(); ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.execute(new IdleConnectionMonitorRunnable(poolingHttpClientConnectionManager)); }
@Produces @AddReservationUrl public String getAddNewReservationUrl() { if (lastCheck.isBefore(now().minusMinutes(MINUTES_PER_CHECK))) { available = false; // this is obviously bad...but ok for our example LOGGER.info(() -> "Checking availability of " + ADD_NEW_RESERVATION_URL); SocketConfig socketConfig = copy(DEFAULT).setSoTimeout(2000).build(); try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultSocketConfig(socketConfig).build(); CloseableHttpResponse response = client.execute(HttpHost.create(HOST_URL), new HttpGet(ADD_NEW_RESERVATION_URL))) { available = SC_OK == response.getStatusLine().getStatusCode(); if (SC_OK != response.getStatusLine().getStatusCode()) { LOGGER.log(WARNING, () -> "Cannot access url for new reservations!"); } } catch (IOException e) { LOGGER.log(SEVERE, e, () -> "Cannot access url for new reservations!"); } } return available ? ADD_NEW_RESERVATION_URL : null; }
/** * Create connection manager for http client. * * @return The connection manager for http client. */ private HttpClientConnectionManager createHttpClientConnectionManager() { ConnectionSocketFactory socketFactory = PlainConnectionSocketFactory.getSocketFactory(); LayeredConnectionSocketFactory sslSocketFactory; try { sslSocketFactory = new SSLConnectionSocketFactory(SSLContext.getDefault(), SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER); } catch (NoSuchAlgorithmException e) { throw new BceClientException("Fail to create SSLConnectionSocketFactory", e); } Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register(Protocol.HTTP.toString(), socketFactory) .register(Protocol.HTTPS.toString(), sslSocketFactory).build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); connectionManager.setDefaultMaxPerRoute(this.config.getMaxConnections()); connectionManager .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(this.config.getSocketTimeoutInMillis()) .setTcpNoDelay(true).build()); connectionManager.setMaxTotal(this.config.getMaxConnections()); return connectionManager; }
private static HttpClientConnectionManager buildHttpClientManager(int socketTimeout) { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(3); cm.setDefaultMaxPerRoute(2); cm.setValidateAfterInactivity(1); cm.setDefaultSocketConfig(SocketConfig.custom() .setSoTimeout(socketTimeout) .setSoLinger(0) .setTcpNoDelay(true) .build()); return cm; }
private HttpClientBuilder createHttpClientBuilder(final GoodDataSettings settings) { final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setDefaultMaxPerRoute(settings.getMaxConnections()); connectionManager.setMaxTotal(settings.getMaxConnections()); final SocketConfig.Builder socketConfig = SocketConfig.copy(SocketConfig.DEFAULT); socketConfig.setSoTimeout(settings.getSocketTimeout()); connectionManager.setDefaultSocketConfig(socketConfig.build()); final RequestConfig.Builder requestConfig = RequestConfig.copy(RequestConfig.DEFAULT); requestConfig.setConnectTimeout(settings.getConnectionTimeout()); requestConfig.setConnectionRequestTimeout(settings.getConnectionRequestTimeout()); requestConfig.setSocketTimeout(settings.getSocketTimeout()); return HttpClientBuilder.create() .setUserAgent(StringUtils.isNotBlank(settings.getUserAgent()) ? String.format("%s %s", settings.getUserAgent(), getUserAgent()) : getUserAgent()) .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig.build()); }
private String post(final String url) throws IOException { final HttpClient client = HttpClients .custom() .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT).build()) .build(); final HttpUriRequest request = RequestBuilder .post() .addParameter("handle", userId) .addParameter("passwd", password) .setUri(url) .setConfig(RequestConfig.custom().setConnectTimeout(TIMEOUT).build()) .build(); return IOUtils.toString( client.execute(request) .getEntity() .getContent()); }
@Override protected void before() throws Throwable { final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build(); serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo(ORIGIN); if(ProtocolScheme.https.equals(protocolScheme)) { serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext()); } registerHandlers(); server = serverBootstrap.create(); server.start(); host = new HttpHost("127.0.0.1", server.getLocalPort(), protocolScheme.name()); uri = URIUtils.rewriteURI(new URI("/"), host); }
/** * 创建httpclient连接池,并初始化httpclient */ public void init() { try { SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()) .build(); HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.getDefaultHostnameVerifier(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslsf) .build(); httpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); // Increase max total connection to 200 httpClientConnectionManager.setMaxTotal(maxTotalPool); // Increase default max connection per route to 20 httpClientConnectionManager.setDefaultMaxPerRoute(maxConPerRoute); SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(socketTimeout).build(); httpClientConnectionManager.setDefaultSocketConfig(socketConfig); } catch (Exception e) { } }
private HttpClientConnectionManager createConnectionManager() { SocketConfig socketConfig = createSocketConfig(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(config.getThreadCount()); connectionManager.setDefaultMaxPerRoute(config.getThreadCount()); connectionManager.setDefaultSocketConfig(socketConfig); return connectionManager; }
private static SocketConfig createSocketConfig() { return SocketConfig .custom() .setSoKeepAlive(true) .setTcpNoDelay(true) .setSoReuseAddress(true) .build(); }
private SocketConfig buildSocketConfig(AttributeMap standardOptions) { return SocketConfig.custom() // TODO do we want to keep SO keep alive .setSoKeepAlive(false) .setSoTimeout( saturatedCast(standardOptions.get(SdkHttpConfigurationOption.SOCKET_TIMEOUT).toMillis())) .setTcpNoDelay(true) .build(); }
private SocketConfig buildSocketConfig(HttpClientSettings settings) { return SocketConfig.custom() .setSoKeepAlive(settings.useTcpKeepAlive()) .setSoTimeout(settings.getSocketTimeout()) .setTcpNoDelay(true) .build(); }
/** * Default constructor * * @param connectionConfigurationT connection conf */ public SwiftConnectionManager(ConnectionConfiguration connectionConfigurationT) { connectionConfiguration = connectionConfigurationT; connectionPool = new PoolingHttpClientConnectionManager(); LOG.trace( "SwiftConnectionManager: setDefaultMaxPerRoute {}", connectionConfiguration.getMaxPerRoute() ); connectionPool.setDefaultMaxPerRoute(connectionConfiguration.getMaxPerRoute()); LOG.trace( "SwiftConnectionManager: getMaxTotal {}", connectionConfiguration.getMaxTotal() ); connectionPool.setMaxTotal(connectionConfiguration.getMaxTotal()); LOG.trace( "Generate SocketConfig with soTimeout of {}", connectionConfiguration.getSoTimeout() ); SocketConfig socketConfig = SocketConfig.custom() .setSoKeepAlive(false) .setSoTimeout(connectionConfiguration.getSoTimeout()) .build(); connectionPool.setDefaultSocketConfig(socketConfig); rConfig = RequestConfig.custom() .setExpectContinueEnabled(true) .setConnectTimeout(connectionConfiguration.getReqConnectTimeout()) .setConnectionRequestTimeout( connectionConfiguration.getReqConnectionRequestTimeout()) .setSocketTimeout(connectionConfiguration.getReqSocketTimeout()) .build(); }
private HttpClient createHttpClient(final int httpSocketTimeout, final int maxConnectionsPerRoute, final int maxConnectionsTotal) { final SocketConfig socketConfig = SocketConfig.custom() .setSoTimeout(httpSocketTimeout) .build(); return HttpClientBuilder.create() .setMaxConnPerRoute(maxConnectionsPerRoute) .setMaxConnTotal(maxConnectionsTotal) .setDefaultSocketConfig(socketConfig) .build(); }
public static SocketConfig getSocketConfig(final HttpParams params) { return SocketConfig.custom() .setSoTimeout(params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0)) .setSoLinger(params.getIntParameter(CoreConnectionPNames.SO_LINGER, -1)) .setTcpNoDelay(params.getBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)) .build(); }
/** * @since 4.3 */ public BasicConnFactory( final SocketFactory plainfactory, final SSLSocketFactory sslfactory, final int connectTimeout, final SocketConfig sconfig, final ConnectionConfig cconfig) { super(); this.plainfactory = plainfactory; this.sslfactory = sslfactory; this.connectTimeout = connectTimeout; this.sconfig = sconfig != null ? sconfig : SocketConfig.DEFAULT; this.connFactory = new DefaultBHttpClientConnectionFactory( cconfig != null ? cconfig : ConnectionConfig.DEFAULT); }
public BasicHttpClientConnectionManager( final Lookup<ConnectionSocketFactory> socketFactoryRegistry, final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory, final SchemePortResolver schemePortResolver, final DnsResolver dnsResolver) { super(); this.connectionOperator = new HttpClientConnectionOperator( socketFactoryRegistry, schemePortResolver, dnsResolver); this.connFactory = connFactory != null ? connFactory : ManagedHttpClientConnectionFactory.INSTANCE; this.expiry = Long.MAX_VALUE; this.socketConfig = SocketConfig.DEFAULT; this.connConfig = ConnectionConfig.DEFAULT; this.isShutdown = new AtomicBoolean(false); }
public void connect( final HttpClientConnection managedConn, final HttpRoute route, final int connectTimeout, final HttpContext context) throws IOException { Args.notNull(managedConn, "Managed Connection"); Args.notNull(route, "HTTP route"); final ManagedHttpClientConnection conn; synchronized (managedConn) { final CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn); conn = entry.getConnection(); } final HttpHost host; if (route.getProxyHost() != null) { host = route.getProxyHost(); } else { host = route.getTargetHost(); } final InetSocketAddress localAddress = route.getLocalAddress() != null ? new InetSocketAddress(route.getLocalAddress(), 0) : null;; SocketConfig socketConfig = this.configData.getSocketConfig(host); if (socketConfig == null) { socketConfig = this.configData.getDefaultSocketConfig(); } if (socketConfig == null) { socketConfig = SocketConfig.DEFAULT; } this.connectionOperator.connect( conn, host, localAddress, connectTimeout, socketConfig, context); }
private void prepare() { Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", this.plainConnectionSocketFactory) .register("https", this.sslConnectionSocketFactory).build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); connectionManager.setMaxTotal(this.maxTotalConn); connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost); connectionManager.setDefaultSocketConfig(SocketConfig.copy(SocketConfig.DEFAULT).setSoTimeout(this.soTimeout).build()); this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, this.idleConnTimeout, this.checkWaitTime); this.idleConnectionMonitorThread.setDaemon(true); this.idleConnectionMonitorThread.start(); this.httpClientBuilder = HttpClients .custom().setConnectionManager(connectionManager).setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(this.soTimeout) .setConnectTimeout(this.connectionTimeout).setConnectionRequestTimeout(this.connectionRequestTimeout).build()) .setRetryHandler(this.httpRequestRetryHandler); if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) { // 使用代理服务器 需要用户认证的代理服务器 CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort), new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword)); this.httpClientBuilder.setDefaultCredentialsProvider(provider); } if (StringUtils.isNotBlank(this.userAgent)) { this.httpClientBuilder.setUserAgent(this.userAgent); } }
@Before public void start() throws Exception { this.localServer = ServerBootstrap.bootstrap() .setSocketConfig(SocketConfig.custom() .setSoTimeout(5000) .build()) .registerHandler(url + "*", new EchoViaHeaderHandler()) .create(); this.localServer.start(); port = this.localServer.getLocalPort(); final CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(100) .setMaxObjectSize(15) //1574 .setAsynchronousWorkerIdleLifetimeSecs(60) .setAsynchronousWorkersMax(1) .setAsynchronousWorkersCore(1) .setRevalidationQueueSize(100) .setSharedCache(true) .build(); final HttpClientBuilder clientBuilder = CachingHttpClientBuilder.create().setCacheConfig(cacheConfig); clientBuilder.setMaxConnTotal(1); clientBuilder.setMaxConnPerRoute(1); final RequestConfig config = RequestConfig.custom() .setSocketTimeout(10000) .setConnectTimeout(10000) .setConnectionRequestTimeout(1000) .build(); clientBuilder.setDefaultRequestConfig(config); client = clientBuilder.build(); }
/** * @since 4.4 */ public BasicHttpClientConnectionManager( final HttpClientConnectionOperator httpClientConnectionOperator, final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) { super(); this.connectionOperator = Args.notNull(httpClientConnectionOperator, "Connection operator"); this.connFactory = connFactory != null ? connFactory : ManagedHttpClientConnectionFactory.INSTANCE; this.expiry = Long.MAX_VALUE; this.socketConfig = SocketConfig.DEFAULT; this.connConfig = ConnectionConfig.DEFAULT; this.isShutdown = new AtomicBoolean(false); }
@Override public void connect( final HttpClientConnection managedConn, final HttpRoute route, final int connectTimeout, final HttpContext context) throws IOException { Args.notNull(managedConn, "Managed Connection"); Args.notNull(route, "HTTP route"); final ManagedHttpClientConnection conn; synchronized (managedConn) { final CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn); conn = entry.getConnection(); } final HttpHost host; if (route.getProxyHost() != null) { host = route.getProxyHost(); } else { host = route.getTargetHost(); } final InetSocketAddress localAddress = route.getLocalSocketAddress(); SocketConfig socketConfig = this.configData.getSocketConfig(host); if (socketConfig == null) { socketConfig = this.configData.getDefaultSocketConfig(); } if (socketConfig == null) { socketConfig = SocketConfig.DEFAULT; } this.connectionOperator.connect( conn, host, localAddress, connectTimeout, socketConfig, context); }
void connect( ManagedHttpClientConnection conn, HttpHost host, InetSocketAddress localAddress, int connectTimeout, SocketConfig socketConfig, HttpContext context) throws IOException;
@Test public void testConnectFailover() throws Exception { final HttpContext context = new BasicHttpContext(); final HttpHost host = new HttpHost("somehost"); final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 0}); final InetAddress ip1 = InetAddress.getByAddress(new byte[] {10, 0, 0, 1}); final InetAddress ip2 = InetAddress.getByAddress(new byte[] {10, 0, 0, 2}); Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] { ip1, ip2 }); Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); Mockito.when(schemePortResolver.resolve(host)).thenReturn(80); Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(plainSocketFactory.connectSocket( Mockito.anyInt(), Mockito.<Socket>any(), Mockito.<HttpHost>any(), Mockito.eq(new InetSocketAddress(ip1, 80)), Mockito.<InetSocketAddress>any(), Mockito.<HttpContext>any())).thenThrow(new ConnectException()); Mockito.when(plainSocketFactory.connectSocket( Mockito.anyInt(), Mockito.<Socket>any(), Mockito.<HttpHost>any(), Mockito.eq(new InetSocketAddress(ip2, 80)), Mockito.<InetSocketAddress>any(), Mockito.<HttpContext>any())).thenReturn(socket); final InetSocketAddress localAddress = new InetSocketAddress(local, 0); connectionOperator.connect(conn, host, localAddress, 1000, SocketConfig.DEFAULT, context); Mockito.verify(plainSocketFactory).connectSocket( 1000, socket, host, new InetSocketAddress(ip2, 80), localAddress, context); Mockito.verify(conn, Mockito.times(3)).bind(socket); }