当前位置: 首页>>代码示例>>Java>>正文


Java PoolingHttpClientConnectionManager.setDefaultSocketConfig方法代码示例

本文整理汇总了Java中org.apache.http.impl.conn.PoolingHttpClientConnectionManager.setDefaultSocketConfig方法的典型用法代码示例。如果您正苦于以下问题:Java PoolingHttpClientConnectionManager.setDefaultSocketConfig方法的具体用法?Java PoolingHttpClientConnectionManager.setDefaultSocketConfig怎么用?Java PoolingHttpClientConnectionManager.setDefaultSocketConfig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.impl.conn.PoolingHttpClientConnectionManager的用法示例。


在下文中一共展示了PoolingHttpClientConnectionManager.setDefaultSocketConfig方法的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());
}
 
开发者ID:funtl,项目名称:framework,代码行数:17,代码来源:HttpClientUtil.java

示例2: 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();
}
 
开发者ID:ad-tech-group,项目名称:openssp,代码行数:19,代码来源:DefaultConnector.java

示例3: 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;
}
 
开发者ID:aws,项目名称:aws-sdk-java-v2,代码行数:20,代码来源:ApacheConnectionManagerFactory.java

示例4: create

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
@Override
public HttpClientConnectionManager create(final HttpClientSettings settings) {
    ConnectionSocketFactory sslsf = getPreferredSocketFactory(settings);

    final PoolingHttpClientConnectionManager cm = new
            PoolingHttpClientConnectionManager(
            createSocketFactoryRegistry(sslsf),
            null,
            DefaultSchemePortResolver.INSTANCE,
            new DelegatingDnsResolver(settings.getDnsResolver()),
            settings.getConnectionPoolTTL(),
            TimeUnit.MILLISECONDS);

    cm.setValidateAfterInactivity(settings.getValidateAfterInactivityMillis());
    cm.setDefaultMaxPerRoute(settings.getMaxConnections());
    cm.setMaxTotal(settings.getMaxConnections());
    cm.setDefaultSocketConfig(buildSocketConfig(settings));
    cm.setDefaultConnectionConfig(buildConnectionConfig(settings));

    return cm;
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:22,代码来源:ApacheConnectionManagerFactory.java

示例5: init

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
 * 创建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) {

    }
}
 
开发者ID:xjtushilei,项目名称:ScriptSpider,代码行数:27,代码来源:HttpUtils.java

示例6: createConnectionManager

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private HttpClientConnectionManager createConnectionManager() {
    SocketConfig socketConfig = createSocketConfig();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(config.getThreadCount());
    connectionManager.setDefaultMaxPerRoute(config.getThreadCount());
    connectionManager.setDefaultSocketConfig(socketConfig);
    return connectionManager;
}
 
开发者ID:vy,项目名称:hrrs,代码行数:9,代码来源:ApacheHttpClientFactory.java

示例7: SwiftConnectionManager

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
 * 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();
}
 
开发者ID:SparkTC,项目名称:stocator,代码行数:36,代码来源:SwiftConnectionManager.java

示例8: prepare

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
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);
	}

}
 
开发者ID:benyzhous,项目名称:springboot-quick-build,代码行数:31,代码来源:DefaultApacheHttpClientBuilder.java

示例9: prepare

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private synchronized void prepare() {
  if(prepared.get()){
    return;
  }
  Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
    .register("http", this.plainConnectionSocketFactory)
    .register("https", this.sslConnectionSocketFactory)
    .build();

  @SuppressWarnings("resource")
  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)
    .setConnectionManagerShared(true)
    .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);
  }
  prepared.set(true);
}
 
开发者ID:11590692,项目名称:Wechat-Group,代码行数:53,代码来源:DefaultApacheHttpClientBuilder.java

示例10: createHttpClient

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private CloseableHttpClient createHttpClient() throws NSPException {
	try {
		initSocketFactory();

		DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
			public InetAddress[] resolve(String host)
					throws UnknownHostException {
				if (host.equalsIgnoreCase("localhost")) {
					return new InetAddress[] { InetAddress
							.getByAddress(new byte[] { 127, 0, 0, 1 }) };
				}

				return super.resolve(host);
			}
		};
		PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
				this.socketFactoryRegistry, dnsResolver);

		SocketConfig socketConfig = SocketConfig.custom()
				.setTcpNoDelay(true).setSoKeepAlive(true).setSoLinger(0)
				.setSoReuseAddress(true).build();

		connManager.setDefaultSocketConfig(socketConfig);

		ConnectionConfig connectionConfig = ConnectionConfig.custom()
				.setCharset(Consts.UTF_8).build();
		connManager.setDefaultConnectionConfig(connectionConfig);
		connManager.setConnectionConfig(new HttpHost("localhost", 80),
				ConnectionConfig.DEFAULT);

		connManager.setMaxTotal(this.mMaxConnections);

		if ((this.mMaxConnections <= this.mConnectionsPerRoute)
				&& (this.mMaxConnections > 0)) {
			this.mConnectionsPerRoute = this.mMaxConnections;
		}

		connManager.setDefaultMaxPerRoute(this.mConnectionsPerRoute);

		connManager.setMaxPerRoute(new HttpRoute(new HttpHost("localhost",
				80)), 100);

		CloseableHttpClient httpclient = HttpClients
				.custom()
				.setConnectionManager(connManager)
				.setDefaultRequestConfig(
						RequestConfig.copy(defaultRequestConfig).build())
				.build();

		return ((CloseableHttpClient) new WeakReference(httpclient).get());
	} catch (Exception e) {
		throw new NSPException(2, "Service unavailable.", e);
	}
}
 
开发者ID:marlonwang,项目名称:raven,代码行数:55,代码来源:HttpConnectionAdaptor.java

示例11: buildClient

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
@Override
protected ExecREST buildClient(RESTPool pool) throws IOException {
    SSLContext sslContext;
    try {
        sslContext = SSLContext.getDefault();
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e);
    }

    Registry<ConnectionSocketFactory> socketRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE))
            .build();

    //TODO buffers size
    SocketConfig socketConfig = SocketConfig.custom()
            .setSoTimeout(new Long(pool.getSocketTimeout()).intValue())
            .setTcpNoDelay(true)
            .setSoKeepAlive(true)
            .setSoReuseAddress(true)
            .build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setCharset(StandardCharsets.UTF_8)
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .build();

    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(
            new DefaultHttpRequestWriterFactory(),
            new DefaultHttpResponseParserFactory()
    );

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(new Long(pool.getMaxPoolWait()).intValue())
            .setConnectTimeout(new Long(pool.getConnectionTimeout()).intValue())
            .setExpectContinueEnabled(pool.expectContinue())
            .build();


    PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(socketRegistry, connFactory);

    ccm.setMaxTotal(pool.getMaxTotal());
    ccm.setDefaultMaxPerRoute(pool.getMaxPerRoute());
    ccm.setDefaultSocketConfig(socketConfig);
    ccm.setDefaultConnectionConfig(connectionConfig);
    ccm.setValidateAfterInactivity(pool.getValidationOnInactivity());

    HttpClientBuilder builder = HttpClients.custom()
            .setConnectionManager(ccm)
            .setDefaultRequestConfig(requestConfig)
            .disableAutomaticRetries()
            .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
            .disableCookieManagement()
            .disableContentCompression();

    addProxy(pool, builder);

    handleRedirects(pool, builder);

    CloseableHttpClient servClient = builder.build();

    IdleConnectionEvictor evictor = new IdleConnectionEvictor(ccm, pool.getEvictorSleep(), TimeUnit.MILLISECONDS, pool.getMaxIdleTime(), TimeUnit.MILLISECONDS);

    HTTPCClientMonitor monitor = pool.hasConnectionMetrics() ? new HTTPCSyncClientMonitor(pool.getName(), ccm) : null;

    return new HTTPCClient(servClient, evictor, monitor);
}
 
开发者ID:mercadolibre,项目名称:java-restclient,代码行数:69,代码来源:HTTPCBuilder.java

示例12: prepare

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private synchronized void prepare() {
  if (prepared.get()) {
    return;
  }
  Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
    .register("http", this.plainConnectionSocketFactory)
    .register("https", this.sslConnectionSocketFactory)
    .build();

  @SuppressWarnings("resource")
  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();

  HttpClientBuilder httpClientBuilder = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .setConnectionManagerShared(true)
    .setSSLSocketFactory(this.buildSSLConnectionSocketFactory())
    .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));
    httpClientBuilder.setDefaultCredentialsProvider(provider);
  }

  if (StringUtils.isNotBlank(this.userAgent)) {
    httpClientBuilder.setUserAgent(this.userAgent);
  }
  this.closeableHttpClient = httpClientBuilder.build();
  prepared.set(true);
}
 
开发者ID:binarywang,项目名称:weixin-java-tools,代码行数:55,代码来源:DefaultApacheHttpClientBuilder.java

示例13: prepare

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
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)
    .setConnectionManagerShared(true)
    .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);
  }

}
 
开发者ID:binarywang,项目名称:weixin-java-tools-for-JDK6,代码行数:49,代码来源:DefaultApacheHttpClientBuilder.java

示例14: createClient

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private void createClient() {

        try {
            SSLContext sslContext;
            SSLConnectionSocketFactory sslsf;
            if (configuration.isDisableHttpsVerification()) {
                sslContext = SSLContext.getInstance("SSL");
                TrustManager mytm[] = {new MyTrustManager()};
                sslContext.init(null, mytm, null);
                sslsf = new SSLConnectionSocketFactory(
                        sslContext,
                        SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            } else {
                sslContext = SSLContexts.custom().build();
                sslsf = new SSLConnectionSocketFactory(
                        sslContext,
                        SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            }

            Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", new PlainConnectionSocketFactory())
                    .register("https", sslsf)
                    .build();

            poolManager = new PoolingHttpClientConnectionManager(r);

            if (configuration.getMaxConnTotal() > 0) {
                poolManager.setMaxTotal(configuration.getMaxConnTotal());
            }
            if (configuration.getMaxConnPerRoute() > 0) {
                poolManager.setDefaultMaxPerRoute(configuration.getMaxConnPerRoute());
            }

            poolManager.setDefaultSocketConfig(SocketConfig.custom().setSoKeepAlive(true).setSoReuseAddress(true).setTcpNoDelay(false).setSoTimeout(configuration.getSotimeout()).build());

            ConnectionKeepAliveStrategy myStrategy = (HttpResponse response, HttpContext context) -> configuration.getKeepAlive();

            httpclient = HttpClients
                    .custom()
                    .setConnectionManager(poolManager)
                    .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)
                    .setKeepAliveStrategy(myStrategy)
                    .build();
        } catch (NoSuchAlgorithmException | KeyManagementException ex) {
            throw new RuntimeException(ex);
        }

    }
 
开发者ID:diennea,项目名称:majordodo,代码行数:49,代码来源:Client.java

示例15: buildHttpClient

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
@SuppressWarnings("resource")
private static YamjHttpClient buildHttpClient() {
    LOG.trace("Create new YAMJ http client");
    
    // create proxy
    HttpHost proxy = null;
    CredentialsProvider credentialsProvider = null;
    
    if (StringUtils.isNotBlank(PROXY_HOST) && PROXY_PORT > 0) {
        proxy = new HttpHost(PROXY_HOST, PROXY_PORT);
      
        if (StringUtils.isNotBlank(PROXY_USERNAME) && StringUtils.isNotBlank(PROXY_PASSWORD)) {
            credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(
                    new AuthScope(PROXY_HOST, PROXY_PORT),
                    new UsernamePasswordCredentials(PROXY_USERNAME, PROXY_PASSWORD));
        }
    }

    
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT_SOCKET).build());
    connManager.setMaxTotal(20);
    connManager.setDefaultMaxPerRoute(2);
    
    CacheConfig cacheConfig = CacheConfig.custom()
                    .setMaxCacheEntries(1000)
                    .setMaxObjectSize(8192)
                    .build();
    
    HttpClientBuilder builder = CachingHttpClientBuilder.create()
            .setCacheConfig(cacheConfig)
            .setConnectionManager(connManager)
            .setProxy(proxy)
            .setDefaultCredentialsProvider(credentialsProvider)
            .setDefaultRequestConfig(RequestConfig.custom()
                    .setConnectionRequestTimeout(TIMEOUT_READ)
                    .setConnectTimeout(TIMEOUT_CONNECT)
                    .setSocketTimeout(TIMEOUT_SOCKET)
                    .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
                    .setProxy(proxy)
                    .build());

    // show status
    showStatus();
    
    // build the client
    YamjHttpClient wrapper = new YamjHttpClient(builder.build(), connManager);
    wrapper.setUserAgentSelector(new WebBrowserUserAgentSelector());
    wrapper.addGroupLimit(".*", 1); // default limit, can be overwritten
    
    // First we have to read/create the rules
    String maxDownloadSlots = PropertiesUtil.getProperty("mjb.MaxDownloadSlots");
    if (StringUtils.isNotBlank(maxDownloadSlots)) {
        LOG.debug("Using download limits: {}", maxDownloadSlots);

        Pattern pattern = Pattern.compile(",?\\s*([^=]+)=(\\d+)");
        Matcher matcher = pattern.matcher(maxDownloadSlots);
        while (matcher.find()) {
            String group = matcher.group(1);
            try {
                final Integer maxResults = Integer.valueOf(matcher.group(2));
                wrapper.addGroupLimit(group, maxResults);
                LOG.trace("Added download slot '{}' with max results {}", group, maxResults);
            } catch (NumberFormatException error) {
                LOG.debug("Rule '{}' is no valid regexp, ignored", group);
            }
        }
    }
    
    return wrapper;
}
 
开发者ID:YAMJ,项目名称:yamj-v2,代码行数:73,代码来源:YamjHttpClientBuilder.java


注:本文中的org.apache.http.impl.conn.PoolingHttpClientConnectionManager.setDefaultSocketConfig方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。