コード例 #1
0
ファイル: HTTPSession.java プロジェクト: msdsoftware/thredds
 protected RequestConfig configureRequest(RequestConfig.Builder rcb, Settings settings)
     throws HTTPException {
   // Configure the RequestConfig
   for (Prop key : settings.getKeys()) {
     Object value = settings.getParameter(key);
     boolean tf = (value instanceof Boolean ? (Boolean) value : false);
     if (key == Prop.ALLOW_CIRCULAR_REDIRECTS) {
       rcb.setCircularRedirectsAllowed(tf);
     } else if (key == Prop.HANDLE_REDIRECTS) {
       rcb.setRedirectsEnabled(tf);
       rcb.setRelativeRedirectsAllowed(tf);
     } else if (key == Prop.MAX_REDIRECTS) {
       rcb.setMaxRedirects((Integer) value);
     } else if (key == Prop.SO_TIMEOUT) {
       rcb.setSocketTimeout((Integer) value);
     } else if (key == Prop.CONN_TIMEOUT) {
       rcb.setConnectTimeout((Integer) value);
     } else if (key == Prop.CONN_REQ_TIMEOUT) {
       rcb.setConnectionRequestTimeout((Integer) value);
     } else if (key == Prop.MAX_THREADS) {
       connmgr.setMaxTotal((Integer) value);
       connmgr.setDefaultMaxPerRoute((Integer) value);
     } /* else ignore */
   }
   RequestConfig cfg = rcb.build();
   return cfg;
 }
コード例 #2
0
      MyDefaultApacheHttpClient4Config() {
        PoolingHttpClientConnectionManager cm;

        if (systemSSL) {
          cm = createSystemSslCM();
        } else if (trustStoreFileName != null) {
          cm = createCustomSslCM();
        } else {
          cm = new PoolingHttpClientConnectionManager();
        }

        if (proxyHost != null) {
          addProxyConfiguration();
        }

        DiscoveryJerseyProvider discoveryJerseyProvider =
            new DiscoveryJerseyProvider(encoderWrapper, decoderWrapper);
        //                getSingletons().add(discoveryJerseyProvider);
        register(discoveryJerseyProvider);

        // Common properties to all clients
        cm.setDefaultMaxPerRoute(maxConnectionsPerHost);
        cm.setMaxTotal(maxTotalConnections);
        property(ApacheClientProperties.CONNECTION_MANAGER, cm);

        String fullUserAgentName =
            (userAgent == null ? clientName : userAgent) + "/v" + buildVersion();
        property(CoreProtocolPNames.USER_AGENT, fullUserAgentName);

        // To pin a client to specific server in case redirect happens, we handle redirects directly
        // (see DiscoveryClient.makeRemoteCall methods).
        property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE);
        property(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
      }
コード例 #3
0
 /**
  * @param maxTotal maxTotal
  * @param maxPerRoute maxPerRoute
  * @param timeout timeout
  * @param retryExecutionCount retryExecutionCount
  * @return
  */
 public static CloseableHttpClient createHttpClient(
     int maxTotal, int maxPerRoute, int timeout, int retryExecutionCount) {
   try {
     SSLContext sslContext = SSLContexts.custom().useSSL().build();
     SSLConnectionSocketFactory sf =
         new SSLConnectionSocketFactory(
             sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
     PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
         new PoolingHttpClientConnectionManager();
     poolingHttpClientConnectionManager.setMaxTotal(maxTotal);
     poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
     SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(timeout).build();
     poolingHttpClientConnectionManager.setDefaultSocketConfig(socketConfig);
     return HttpClientBuilder.create()
         .setConnectionManager(poolingHttpClientConnectionManager)
         .setSSLSocketFactory(sf)
         .setRetryHandler(new HttpRequestRetryHandlerImpl(retryExecutionCount))
         .build();
   } catch (KeyManagementException e) {
     e.printStackTrace();
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   }
   return null;
 }
コード例 #4
0
 public HttpClientGenerator() {
   Registry<ConnectionSocketFactory> reg =
       RegistryBuilder.<ConnectionSocketFactory>create()
           .register("http", PlainConnectionSocketFactory.INSTANCE)
           .register("https", SSLConnectionSocketFactory.getSocketFactory())
           .build();
   connectionManager = new PoolingHttpClientConnectionManager(reg);
   connectionManager.setDefaultMaxPerRoute(100);
 }
コード例 #5
0
 /**
  * Sets the timeouts of the HTTP client.
  *
  * @param connectionTimeout timeout until connection established in milliseconds. Zero means no
  *     timeout.
  * @param socketTimeout timeout for waiting for data in milliseconds. Zero means no timeout.
  * @param maxRequests maximum number of connections to a particuar host
  */
 public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
   PrudentHttpEntityResolver.maxRequests = maxRequests;
   PoolingHttpClientConnectionManager phcConnMgr;
   Registry<ConnectionSocketFactory> registry = //
       RegistryBuilder.<ConnectionSocketFactory>create() //
           .register("http", PlainConnectionSocketFactory.getSocketFactory()) //
           .register("https", SSLConnectionSocketFactory.getSocketFactory()) //
           .build();
   HttpClientBuilder builder = HttpClients.custom();
   builder.setRedirectStrategy(new LaxRedirectStrategy());
   builder.setMaxConnPerRoute(maxRequests);
   builder.setMaxConnTotal(200);
   if ("true".equals(System.getProperty("nu.validator.xml.promiscuous-ssl", "false"))) { //
     try {
       SSLContext promiscuousSSLContext =
           new SSLContextBuilder() //
               .loadTrustMaterial(
                   null,
                   new TrustStrategy() {
                     @Override
                     public boolean isTrusted(X509Certificate[] arg0, String arg1)
                         throws CertificateException {
                       return true;
                     }
                   })
               .build();
       builder.setSslcontext(promiscuousSSLContext);
       HostnameVerifier verifier = //
           SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
       SSLConnectionSocketFactory promiscuousSSLConnSocketFactory = //
           new SSLConnectionSocketFactory(promiscuousSSLContext, verifier);
       registry =
           RegistryBuilder.<ConnectionSocketFactory>create() //
               .register("https", promiscuousSSLConnSocketFactory) //
               .register("http", PlainConnectionSocketFactory.getSocketFactory()) //
               .build();
     } catch (KeyManagementException
         | KeyStoreException
         | NoSuchAlgorithmException
         | NumberFormatException e) {
       e.printStackTrace();
     }
   }
   phcConnMgr = new PoolingHttpClientConnectionManager(registry);
   phcConnMgr.setDefaultMaxPerRoute(maxRequests);
   phcConnMgr.setMaxTotal(200);
   builder.setConnectionManager(phcConnMgr);
   RequestConfig.Builder config = RequestConfig.custom();
   config.setCircularRedirectsAllowed(true);
   config.setMaxRedirects(20); // Gecko default
   config.setConnectTimeout(connectionTimeout);
   config.setCookieSpec(CookieSpecs.BEST_MATCH);
   config.setSocketTimeout(socketTimeout);
   client = builder.setDefaultRequestConfig(config.build()).build();
 }
コード例 #6
0
 private static HttpClientConnectionManager makeConnectionManager(int maxConnections) {
   // Jersey requires an instance of the ClientConnectionManager interface which is deprecated in
   // the latest
   // version of the Apache HttpComponents. In a future version, this implementation should be
   // updated to
   // the PoolingHttpClientConnectionManager and the HttpClientConnectionManager.
   PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
   connectionManager.setMaxTotal(maxConnections);
   connectionManager.setDefaultMaxPerRoute(maxConnections);
   return connectionManager;
 }
コード例 #7
0
  @Bean
  RestTemplate restTemplate() {

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
    HttpClientBuilder httpClient =
        HttpClientBuilder.create().setConnectionManager(connectionManager);

    HttpComponentsClientHttpRequestFactory httpRequestFactory =
        new HttpComponentsClientHttpRequestFactory(httpClient.build());

    return new RestTemplate(httpRequestFactory);
  }
コード例 #8
0
ファイル: BeanConfig.java プロジェクト: junjies/boot-rest
  @Bean
  public Client webClient() {
    final PoolingHttpClientConnectionManager poolingConnectionManager =
        new PoolingHttpClientConnectionManager();
    poolingConnectionManager.setMaxTotal(webClientConnectionPoolMaxTotal);
    poolingConnectionManager.setDefaultMaxPerRoute(webClientConnectionPoolDefaultMaxPerRoute);

    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(READ_TIMEOUT, 2000);
    clientConfig.property(CONNECT_TIMEOUT, 500);
    clientConfig.property(CONNECTION_MANAGER, poolingConnectionManager);
    clientConfig.connectorProvider(new ApacheConnectorProvider());

    return newClient(clientConfig).register(jacksonJsonProvider());
  }
コード例 #9
0
  private MozuHttpClientPool() {

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // Increase max total connection to 200
    cm.setMaxTotal(MozuConfig.getMaxHttpClientConnections());
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(MozuConfig.getDefaultHttpClientMaxPerRoute());

    ConnectionKeepAliveStrategy myStrategy =
        new ConnectionKeepAliveStrategy() {
          @Override
          public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            HeaderElementIterator it =
                new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            while (it.hasNext()) {
              HeaderElement he = it.nextElement();
              String param = he.getName();
              String value = he.getValue();
              if (value != null && param.equalsIgnoreCase("timeout")) {
                return Long.parseLong(value) * 1000;
              }
            }
            return 10 * 1000;
          }
        };

    RequestConfig requestConfig =
        RequestConfig.custom()
            .setConnectionRequestTimeout(MozuConfig.getHttpClientTimeoutMillis())
            .setSocketTimeout(MozuConfig.getHttpClientTimeoutMillis())
            .setConnectionRequestTimeout(MozuConfig.getHttpClientTimeoutMillis())
            .build();

    // Build the client.
    threadSafeClient =
        HttpClients.custom()
            .setConnectionManager(cm)
            .useSystemProperties()
            .disableConnectionState()
            .setDefaultRequestConfig(requestConfig)
            .setKeepAliveStrategy(myStrategy)
            .build();

    monitor = new IdleConnectionMonitorThread(cm);

    monitor.setDaemon(true);
    monitor.start();
  }
コード例 #10
0
  @Override
  protected void configureClient(ClientConfig config) {
    // using AHC as a test client connector to avoid issues with HttpUrlConnection socket
    // management.
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    // adjusting max. connections just to be safe - the testEventSourceReconnect is quite greedy...
    cm.setMaxTotal(MAX_LISTENERS * MAX_ITEMS);
    cm.setDefaultMaxPerRoute(MAX_LISTENERS * MAX_ITEMS);

    config
        .register(SseFeature.class)
        .property(ApacheClientProperties.CONNECTION_MANAGER, cm)
        .property(ClientProperties.READ_TIMEOUT, 2000)
        .connector(new ApacheConnector(config));
  }
コード例 #11
0
  HttpClient() {
    try {
      PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
      SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
      connManager.setDefaultSocketConfig(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.setMaxTotal(MAX_TOTAL_CONNECTIONS);
      connManager.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS);
      connManager.setMaxPerRoute(new HttpRoute(DEFAULT_TARGETHOST), 50);

      HttpRequestRetryHandler retryHandler =
          new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(
                IOException exception, int executionCount, HttpContext context) {
              if (executionCount >= 5) return false;
              if (exception instanceof InterruptedIOException) return false;
              if (exception instanceof UnknownHostException) return false;
              if (exception instanceof ConnectTimeoutException) return false;
              if (exception instanceof SSLException) return false;
              HttpClientContext clientContext = HttpClientContext.adapt(context);
              HttpRequest request = clientContext.getRequest();
              boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
              if (idempotent) return true;
              return false;
            }
          };

      httpClient =
          HttpClients.custom()
              .setConnectionManager(connManager)
              .setRetryHandler(retryHandler)
              .build();
    } catch (Exception e) {
      logger.error("HttpClientUtil error : [{}].", e.getMessage(), e);
    }
  }
コード例 #12
0
ファイル: HttpClientUtils.java プロジェクト: sirplain87/a
 private static PoolingHttpClientConnectionManager bulidConnectionManager() {
   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();
   PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
   connectionManager.setMaxTotal(600);
   connectionManager.setDefaultMaxPerRoute(200);
   connectionManager.setDefaultConnectionConfig(connectionConfig);
   connectionManager.setValidateAfterInactivity(1000);
   return connectionManager;
 }
コード例 #13
0
  private static synchronized void setup() {
    if (properties == null) {
      properties = new Properties();
      try {
        InputStream is = ConanUtils.class.getResourceAsStream("/sampletabconverters.properties");
        properties.load(is);
      } catch (IOException e) {
        log.error("Unable to read resource sampletabconverters.properties", e);
      }
    }
    if (conman == null) {
      conman = new PoolingHttpClientConnectionManager();
      conman.setMaxTotal(128);
      conman.setDefaultMaxPerRoute(64);
      conman.setValidateAfterInactivity(0);

      ConnectionKeepAliveStrategy keepAliveStrategy =
          new ConnectionKeepAliveStrategy() {
            @Override
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
              // see if the user provides a live time
              HeaderElementIterator it =
                  new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
              while (it.hasNext()) {
                HeaderElement he = it.nextElement();
                String param = he.getName();
                String value = he.getValue();
                if (value != null && param.equalsIgnoreCase("timeout")) {
                  return Long.parseLong(value) * 1000;
                }
              }
              // default to one second live time
              return 1 * 1000;
            }
          };

      httpClient =
          HttpClients.custom()
              .setKeepAliveStrategy(keepAliveStrategy)
              .setConnectionManager(conman)
              .build();
    }
  }
コード例 #14
0
 public static HttpClient createHttpClient(int maxTotal, int maxPerRoute) {
   try {
     SSLContext sslContext = SSLContexts.custom().useSSL().build();
     SSLConnectionSocketFactory sf =
         new SSLConnectionSocketFactory(
             sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
     PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
         new PoolingHttpClientConnectionManager();
     poolingHttpClientConnectionManager.setMaxTotal(maxTotal);
     poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
     return HttpClientBuilder.create()
         .setConnectionManager(poolingHttpClientConnectionManager)
         .setSSLSocketFactory(sf)
         .build();
   } catch (KeyManagementException e) {
     e.printStackTrace();
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   }
   return null;
 }
コード例 #15
0
ファイル: PostHTTP.java プロジェクト: agent001/incubator-nifi
  private Config getConfig(final String url, final ProcessContext context) {
    final String baseUrl = getBaseUrl(url);
    Config config = configMap.get(baseUrl);
    if (config != null) {
      return config;
    }

    final PoolingHttpClientConnectionManager conMan;
    final SSLContextService sslContextService =
        context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    if (sslContextService == null) {
      conMan = new PoolingHttpClientConnectionManager();
    } else {
      final SSLContext sslContext;
      try {
        sslContext = createSSLContext(sslContextService);
      } catch (final Exception e) {
        throw new ProcessException(e);
      }

      final SSLConnectionSocketFactory sslsf =
          new SSLConnectionSocketFactory(
              sslContext,
              new String[] {"TLSv1"},
              null,
              SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

      final Registry<ConnectionSocketFactory> socketFactoryRegistry =
          RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build();

      conMan = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    }

    conMan.setDefaultMaxPerRoute(context.getMaxConcurrentTasks());
    conMan.setMaxTotal(context.getMaxConcurrentTasks());
    config = new Config(conMan);
    final Config existingConfig = configMap.putIfAbsent(baseUrl, config);

    return (existingConfig == null) ? config : existingConfig;
  }
コード例 #16
0
  protected PoolingHttpClientConnectionManager newConnectionManager() {
    try {
      final SSLContext sslContext = SSLContext.getInstance("SSL");
      sslContext.init(
          null,
          new TrustManager[] {
            new X509TrustManager() {
              @Override
              public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                  throws CertificateException {}

              @Override
              public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                  throws CertificateException {}

              @Override
              public X509Certificate[] getAcceptedIssuers() {
                return null;
              }
            }
          },
          new SecureRandom());

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

      connectionManager = new PoolingHttpClientConnectionManager(registry);
      connectionManager.setMaxTotal(
          Integer.parseInt(System.getProperty("zuul.max.host.connections", "200")));
      connectionManager.setDefaultMaxPerRoute(
          Integer.parseInt(System.getProperty("zuul.max.host.connections", "20")));
      return connectionManager;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
コード例 #17
0
  /**
   * Key store 类型HttpClient
   *
   * @param keystore
   * @param keyPassword
   * @param supportedProtocols
   * @param maxTotal
   * @param maxPerRoute
   * @return
   */
  public static HttpClient createKeyMaterialHttpClient(
      KeyStore keystore,
      String keyPassword,
      String[] supportedProtocols,
      int maxTotal,
      int maxPerRoute) {
    try {

      SSLContext sslContext =
          SSLContexts.custom()
              .useSSL()
              .loadKeyMaterial(keystore, keyPassword.toCharArray())
              .build();
      SSLConnectionSocketFactory sf =
          new SSLConnectionSocketFactory(
              sslContext,
              supportedProtocols,
              null,
              SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
      PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
          new PoolingHttpClientConnectionManager();
      poolingHttpClientConnectionManager.setMaxTotal(maxTotal);
      poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
      return HttpClientBuilder.create()
          .setConnectionManager(poolingHttpClientConnectionManager)
          .setSSLSocketFactory(sf)
          .build();
    } catch (KeyManagementException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
      e.printStackTrace();
    } catch (KeyStoreException e) {
      e.printStackTrace();
    }
    return null;
  }
コード例 #18
0
 public SpiderSupporter(int infoType, String url, String indexSelector) {
   cm = new PoolingHttpClientConnectionManager();
   cm.setMaxTotal(200);
   cm.setDefaultMaxPerRoute(20);
   cookieStore = new BasicCookieStore();
   defaultRequestConfig =
       RequestConfig.custom()
           .setCookieSpec(CookieSpecs.DEFAULT)
           .setExpectContinueEnabled(true)
           .build();
   httpclient =
       HttpClients.custom()
           .setConnectionManager(cm)
           .setDefaultCookieStore(cookieStore)
           // .setProxy(new HttpHost("127.0.0.1", 8888)) //fiddler proxy
           .setConnectionManagerShared(true)
           .setDefaultRequestConfig(defaultRequestConfig)
           .setKeepAliveStrategy(new ChangePerResponse())
           .build();
   this.INFO_TYPE = infoType;
   this.URL = url; // 索引所在页的地址
   indexExtractor = new IndexExtractor(URL, httpclient, indexSelector);
 }
コード例 #19
0
ファイル: QHttpClient.java プロジェクト: ayumiono/lmspider
  /**
   * 个性化配置连接管理器
   *
   * @param maxConnectionsPerHost 设置默认的连接到每个主机的最大连接数
   * @param maxTotalConnections 设置整个管理连接器的最大连接数
   * @param conTimeOutMs 连接超时
   * @param soTimeOutMs socket超时
   * @param routeCfgList 特殊路由配置列表,若无请填null
   * @param proxy 代理设置,若无请填null
   */
  public QHttpClient(
      int maxConnectionsPerHost,
      int maxTotalConnections,
      int conTimeOutMs,
      int soTimeOutMs,
      List<RouteCfg> routeCfgList,
      HttpHost proxy) {

    // 使用默认的 socket factories 注册 "http" & "https" protocol scheme
    Registry<ConnectionSocketFactory> reg =
        RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", SSLConnectionSocketFactory.getSocketFactory())
            .build();
    PoolingHttpClientConnectionManager connectionManager =
        new PoolingHttpClientConnectionManager(reg);

    SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(soTimeOutMs).build();
    connectionManager.setDefaultMaxPerRoute(maxConnectionsPerHost);
    connectionManager.setMaxTotal(maxTotalConnections);
    connectionManager.setDefaultSocketConfig(socketConfig);
    // 对特定路由修改最大连接数
    if (null != routeCfgList) {
      for (RouteCfg routeCfg : routeCfgList) {
        HttpHost localhost = new HttpHost(routeCfg.getHost(), routeCfg.getPort());
        connectionManager.setMaxPerRoute(new HttpRoute(localhost), routeCfg.getMaxConnetions());
      }
    }

    // 设置代理
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    httpClient =
        HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setRoutePlanner(routePlanner)
            .build();
  }
コード例 #20
0
 static {
   cm.setMaxTotal(256);
   cm.setDefaultMaxPerRoute(16);
   client = HttpClients.custom().setConnectionManager(cm).build();
 }
コード例 #21
0
  @Override
  public void init(DockerClientConfig dockerClientConfig) {
    checkNotNull(dockerClientConfig, "config was not specified");

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.connectorProvider(new ApacheConnectorProvider());
    clientConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);

    clientConfig.register(ResponseStatusExceptionFilter.class);
    clientConfig.register(JsonClientFilter.class);
    clientConfig.register(JacksonJsonProvider.class);

    // logging may disabled via log level
    clientConfig.register(new SelectiveLoggingFilter(LOGGER, true));

    if (readTimeout != null) {
      clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout);
    }

    if (connectTimeout != null) {
      clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout);
    }

    if (clientResponseFilters != null) {
      for (ClientResponseFilter clientResponseFilter : clientResponseFilters) {
        if (clientResponseFilter != null) clientConfig.register(clientResponseFilter);
      }
    }

    if (clientRequestFilters != null) {
      for (ClientRequestFilter clientRequestFilter : clientRequestFilters) {
        if (clientRequestFilter != null) clientConfig.register(clientRequestFilter);
      }
    }

    URI originalUri = dockerClientConfig.getUri();

    SSLContext sslContext = null;

    if (dockerClientConfig.getSslConfig() != null) {
      try {
        sslContext = dockerClientConfig.getSslConfig().getSSLContext();
      } catch (Exception ex) {
        throw new DockerClientException("Error in SSL Configuration", ex);
      }
    }

    PoolingHttpClientConnectionManager connManager =
        new PoolingHttpClientConnectionManager(getSchemeRegistry(originalUri, sslContext));

    if (maxTotalConnections != null) connManager.setMaxTotal(maxTotalConnections);
    if (maxPerRouteConnections != null) connManager.setDefaultMaxPerRoute(maxPerRouteConnections);

    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connManager);

    // Configure connection pool timeout
    // clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, RequestConfig.custom()
    // .setConnectionRequestTimeout(1000).build());

    ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);

    if (sslContext != null) {
      clientBuilder.sslContext(sslContext);
    }

    client = clientBuilder.build();

    if (originalUri.getScheme().equals("unix")) {
      dockerClientConfig.setUri(UnixConnectionSocketFactory.sanitizeUri(originalUri));
    }
    WebTarget webResource = client.target(dockerClientConfig.getUri());

    if (dockerClientConfig.getVersion() == null || dockerClientConfig.getVersion().isEmpty()) {
      baseResource = webResource;
    } else {
      baseResource = webResource.path("v" + dockerClientConfig.getVersion());
    }
  }
コード例 #22
0
 public PoolingHttpRequester setConnPoolMaxPerRoute(final int max) {
   cm.setDefaultMaxPerRoute(max);
   return this;
 }
コード例 #23
0
 public AbstractRolesIT() {
   connectionManager.setMaxTotal(Integer.MAX_VALUE);
   connectionManager.setDefaultMaxPerRoute(20);
   connectionManager.closeIdleConnections(3, TimeUnit.SECONDS);
   client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
 }
コード例 #24
0
 static {
   // Increase max total connection to 200
   CONNECTION_MANAGER.setMaxTotal(200);
   // Increase default max connection per route to 20
   CONNECTION_MANAGER.setDefaultMaxPerRoute(20);
 }
コード例 #25
0
  public EasyHttpClient() {

    CookieSpecProvider easySpecProvider =
        new CookieSpecProvider() {
          public CookieSpec create(HttpContext context) {

            return new BrowserCompatSpec() {
              public void validate(Cookie cookie, CookieOrigin origin)
                  throws MalformedCookieException {}
            };
          }
        };

    SSLContext sslContext = SSLContexts.createDefault(); // 忽略证书主机名验证
    SSLConnectionSocketFactory sslsf =
        new SSLConnectionSocketFactory(
            sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    Registry<ConnectionSocketFactory> reg =
        RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", sslsf)
            .build();
    connectionManager = new PoolingHttpClientConnectionManager(reg);
    connectionManager.setDefaultMaxPerRoute(100); // 同一个路由允许最大连接数
    // connectionManager.setMaxTotal(poolSize);
    HttpHost localhost = new HttpHost("locahost", 80); // 如果是多网卡,这里选择出口IP?
    connectionManager.setMaxPerRoute(new HttpRoute(localhost), 50);

    Registry<CookieSpecProvider> r =
        RegistryBuilder.<CookieSpecProvider>create()
            .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
            .register(CookieSpecs.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory())
            .register("easy", easySpecProvider)
            .build();

    RequestConfig requestConfig =
        RequestConfig.custom()
            .setCookieSpec("easy")
            .setSocketTimeout(10000)
            .setConnectTimeout(10000)
            .build();
    ConnectionConfig connectioncfg = ConnectionConfig.custom().setCharset(Charsets.UTF_8).build();
    SocketConfig socketConfig =
        SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();

    HttpRequestRetryHandler myRetryHandler =
        new HttpRequestRetryHandler() {
          public boolean retryRequest(
              IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= 3) {
              return false;
            }
            if (exception instanceof SSLHandshakeException) {
              return false;
            } else {
              return true;
            }
          }
        };

    HttpClientBuilder builder =
        HttpClients.custom() // 默认
            .setConnectionManager(connectionManager) // 链接管理器
            .setDefaultSocketConfig(socketConfig) // socket管理器
            .setRetryHandler(myRetryHandler) // 重试3次
            .setDefaultConnectionConfig(connectioncfg) // 链接配置,如默认字符编码
            .setDefaultCookieSpecRegistry(r) // cookie策略
            .setUserAgent(Chorme_User_Agent) // 浏览器请求头
            .setDefaultRequestConfig(requestConfig) // 链接配置,超时等
            .setDefaultCookieStore(cookieStore); // cookie

    builder.addInterceptorFirst(
        new HttpRequestInterceptor() {

          public void process(final HttpRequest request, final HttpContext context)
              throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
              request.addHeader("Accept-Encoding", "gzip");
            }
          }
        });

    httpclient = builder.build();
  }
コード例 #26
0
ファイル: ClientConfiguration.java プロジェクト: gzwfdy/scs
  public static final void main(String[] args) throws Exception {

    // Use custom message parser / writer to customize the way HTTP
    // messages are parsed from and written out to the data stream.
    HttpMessageParserFactory<HttpResponse> responseParserFactory =
        new DefaultHttpResponseParserFactory() {

          @Override
          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();

    // Use a custom connection factory to customize the process of
    // initialization of outgoing HTTP connections. Beside standard connection
    // configuration parameters HTTP connection factory can define message
    // parser / writer routines to be employed by individual connections.
    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory =
        new ManagedHttpClientConnectionFactory(requestWriterFactory, responseParserFactory);

    // Client HTTP connection objects when fully initialized can be bound to
    // an arbitrary network socket. The process of network socket initialization,
    // its connection to a remote address and binding to a local one is controlled
    // by a connection socket factory.

    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    SSLContext sslcontext = SSLContexts.createSystemDefault();

    // Create a registry of custom connection socket factories for supported
    // protocol schemes.
    Registry<ConnectionSocketFactory> socketFactoryRegistry =
        RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslcontext))
            .build();

    // Use custom DNS resolver to override the system DNS resolution.
    DnsResolver dnsResolver =
        new SystemDefaultDnsResolver() {

          @Override
          public InetAddress[] resolve(final String host) throws UnknownHostException {
            if (host.equalsIgnoreCase("myhost")) {
              return new InetAddress[] {InetAddress.getByAddress(new byte[] {127, 0, 0, 1})};
            } else {
              return super.resolve(host);
            }
          }
        };

    // Create a connection manager with custom configuration.
    PoolingHttpClientConnectionManager connManager =
        new PoolingHttpClientConnectionManager(socketFactoryRegistry, connFactory, dnsResolver);

    // Create socket configuration
    SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
    // Configure the connection manager to use socket configuration either
    // by default or for a specific host.
    connManager.setDefaultSocketConfig(socketConfig);
    connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig);
    // Validate connections after 1 sec of inactivity
    connManager.setValidateAfterInactivity(1000);

    // Create message constraints
    MessageConstraints messageConstraints =
        MessageConstraints.custom().setMaxHeaderCount(200).setMaxLineLength(2000).build();
    // Create connection configuration
    ConnectionConfig connectionConfig =
        ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Consts.UTF_8)
            .setMessageConstraints(messageConstraints)
            .build();
    // Configure the connection manager to use connection configuration either
    // by default or for a specific host.
    connManager.setDefaultConnectionConfig(connectionConfig);
    connManager.setConnectionConfig(new HttpHost("somehost", 80), ConnectionConfig.DEFAULT);

    // 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(100);
    connManager.setDefaultMaxPerRoute(10);
    connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20);

    // Use custom cookie store if necessary.
    CookieStore cookieStore = new BasicCookieStore();
    // Use custom credentials provider if necessary.
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    // Create global request configuration
    RequestConfig defaultRequestConfig =
        RequestConfig.custom()
            .setCookieSpec(CookieSpecs.DEFAULT)
            .setExpectContinueEnabled(true)
            .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
            .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
            .build();

    // Create an HttpClient with the given custom dependencies and configuration.
    CloseableHttpClient httpclient =
        HttpClients.custom()
            .setConnectionManager(connManager)
            .setDefaultCookieStore(cookieStore)
            .setDefaultCredentialsProvider(credentialsProvider)
            .setProxy(new HttpHost("myproxy", 8080))
            .setDefaultRequestConfig(defaultRequestConfig)
            .build();

    try {
      HttpGet httpget = new HttpGet("http://httpbin.org/get");
      // Request configuration can be overridden at the request level.
      // They will take precedence over the one set at the client level.
      RequestConfig requestConfig =
          RequestConfig.copy(defaultRequestConfig)
              .setSocketTimeout(5000)
              .setConnectTimeout(5000)
              .setConnectionRequestTimeout(5000)
              .setProxy(new HttpHost("myotherproxy", 8080))
              .build();
      httpget.setConfig(requestConfig);

      // Execution context can be customized locally.
      HttpClientContext context = HttpClientContext.create();
      // Contextual attributes set the local context level will take
      // precedence over those set at the client level.
      context.setCookieStore(cookieStore);
      context.setCredentialsProvider(credentialsProvider);

      System.out.println("executing request " + httpget.getURI());
      CloseableHttpResponse response = httpclient.execute(httpget, context);
      try {
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
        System.out.println("----------------------------------------");

        // Once the request has been executed the local context can
        // be used to examine updated state and various objects affected
        // by the request execution.

        // Last executed request
        context.getRequest();
        // Execution route
        context.getHttpRoute();
        // Target auth state
        context.getTargetAuthState();
        // Proxy auth state
        context.getTargetAuthState();
        // Cookie origin
        context.getCookieOrigin();
        // Cookie spec used
        context.getCookieSpec();
        // User security token
        context.getUserToken();

      } finally {
        response.close();
      }
    } finally {
      httpclient.close();
    }
  }