Example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager

List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager.

Prototype

public PoolingHttpClientConnectionManager() 

Source Link

Usage

From source file:de.qucosa.dissemination.epicur.servlet.EpicurDisseminationServlet.java

@Override
public void init() {
    httpClient = HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager())
            .build();//from w w  w. j av a 2 s . c o  m

    marshallerPool = new GenericObjectPool<>(new BasePooledObjectFactory<Marshaller>() {
        @Override
        public Marshaller create() throws Exception {
            Marshaller marshaller = JAXBContext.newInstance(Epicur.class).createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XEPICUR_SCHEMA_LOCATION);
            return marshaller;
        }

        @Override
        public PooledObject<Marshaller> wrap(Marshaller marshaller) {
            return new DefaultPooledObject<>(marshaller);
        }
    });
}

From source file:com.sumologic.log4j.http.SumoHttpSender.java

public void init() {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectionTimeout).build();

    HttpClientBuilder builder = HttpClients.custom()
            .setConnectionManager(new PoolingHttpClientConnectionManager())
            .setDefaultRequestConfig(requestConfig);

    HttpProxySettingsCreator creator = new HttpProxySettingsCreator(proxySettings);
    creator.configureProxySettings(builder);

    httpClient = builder.build();//w w  w.  jav a  2  s  .  c  om
}

From source file:monasca.api.infrastructure.persistence.influxdb.InfluxV9RepoReader.java

@Inject
public InfluxV9RepoReader(final ApiConfig config) {

    this.influxName = config.influxDB.getName();
    logger.debug("Influxdb database name: {}", this.influxName);

    this.influxUrl = config.influxDB.getUrl() + "/query";
    logger.debug("Influxdb URL: {}", this.influxUrl);

    this.influxUser = config.influxDB.getUser();
    this.influxPass = config.influxDB.getPassword();
    this.influxCreds = this.influxUser + ":" + this.influxPass;

    this.gzip = config.influxDB.getGzip();
    logger.debug("Influxdb gzip responses: {}", this.gzip);

    logger.debug("Setting up basic Base64 authentication");
    this.baseAuthHeader = "Basic " + new String(Base64.encodeBase64(this.influxCreds.getBytes()));

    // We inject InfluxV9RepoReader as a singleton. So, we must share connections safely.
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(config.influxDB.getMaxHttpConnections());

    if (this.gzip) {

        logger.debug("Setting up gzip responses from Influxdb");

        this.httpClient = HttpClients.custom().setConnectionManager(cm)
                .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");
                        }/*from   ww w.j  a  va  2s .  co m*/
                    }
                }).addInterceptorFirst(new HttpResponseInterceptor() {

                    public void process(final HttpResponse response, final HttpContext context)
                            throws HttpException, IOException {
                        HttpEntity entity = response.getEntity();
                        if (entity != null) {
                            Header ceheader = entity.getContentEncoding();
                            if (ceheader != null) {
                                HeaderElement[] codecs = ceheader.getElements();
                                for (int i = 0; i < codecs.length; i++) {
                                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }).build();

    } else {

        logger.debug("Setting up non-gzip responses from Influxdb");

        this.httpClient = HttpClients.custom().setConnectionManager(cm).build();

    }
}

From source file:com.jfrog.bintray.client.impl.HttpClientConfigurator.java

public CloseableHttpClient getClient() {
    if (hasCredentials()) {
        builder.setDefaultCredentialsProvider(credsProvider);
    }//  www .  j  a  v  a2 s.c  o  m
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); //Threadsafe
    cm.setDefaultMaxPerRoute(maxConnectionsPerRoute);
    cm.setMaxTotal(maxTotalConnections);
    builder.setConnectionManager(cm);

    return builder.setDefaultRequestConfig(config.build()).build();
}

From source file:fi.helsinki.opintoni.config.CoursePageConfiguration.java

private ClientHttpRequestFactory clientHttpRequestFactory() {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setReadTimeout(appConfiguration.getInteger("httpClient.readTimeout"));
    factory.setConnectTimeout(appConfiguration.getInteger("httpClient.connectTimeout"));

    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    poolingHttpClientConnectionManager.setMaxTotal(appConfiguration.getInteger("httpClient.maxTotal"));
    poolingHttpClientConnectionManager//  w  w  w .  j a v  a  2  s .c om
            .setDefaultMaxPerRoute(appConfiguration.getInteger("httpClient.defaultMaxPerRoute"));

    CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(poolingHttpClientConnectionManager).build();

    factory.setHttpClient(httpClient);

    return factory;
}

From source file:org.auraframework.integration.test.http.AuraResourceServletLoggingHttpTest.java

/**
 * test add for W-2792895//from  ww  w  .  j a  v  a  2  s  . com
   also since I ask cache to log something when hit miss, this kind of verify W-2105858 as well
 */
@Test
public void testConcurrentGetRequests() throws Exception {
    // I tried to use obtainGetMethod(url) then perform(HttpGet) , but
    // our default httpClient use BasicClientConnectionManager, which doesn't work well with MultiThread
    // let's use PoolingHttpClientConnectionManager instead.
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // Increase max total connection to 200 -- just some big number
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20 -- again, just some big numer
    cm.setDefaultMaxPerRoute(20);
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

    String modeAndContext = getSimpleContext(Format.JS, false);
    String url = "/l/" + AuraTextUtil.urlencode(modeAndContext) + "/app.js";

    int numOfRequest = 5;
    List<Request> requests = new ArrayList<>();
    for (int i = 1; i <= numOfRequest; i++) {
        requests.add(new Request(httpClient, url));
    }

    ExecutorService excutor = Executors.newFixedThreadPool(numOfRequest);
    List<Future<Integer>> responses = new ArrayList<>();
    for (Request request : requests) {
        responses.add(excutor.submit(request));
    }
    for (Future<Integer> response : responses) {
        response.get();
    }

    int counter = 0;
    String message;
    List<LoggingEvent> logs = appender.getLog();
    for (LoggingEvent le : logs) {
        message = le.getMessage().toString();
        if (message.contains("StringsCache")) {
            counter++;
            assertTrue("get unexpected logging message for cache miss:" + message,
                    message.contains("cache miss for key: JS:DEV:"));
        }
    }
    //run this test right after server is up, we get one miss. second time, what we looking for is cached already, no miss.
    assertTrue("we should only have no more than one cache miss, instead we have " + counter, counter <= 1);
}

From source file:org.aliuge.crawler.fetcher.DefaultFetcher.java

public DefaultFetcher createFetcher(FetchConfig config) {
    // //from  w  ww  .j a v a2  s  . co m
    connectionManager = new PoolingHttpClientConnectionManager();

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

            return new BrowserCompatSpec() {
                @Override
                public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException {
                    // Oh, I am easy
                }
            };
        }

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

    // Create global request configuration
    defaultRequestConfig = RequestConfig.custom().setCookieSpec("easy").setSocketTimeout(10000)
            .setConnectTimeout(10000).build();

    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());

    // Create an HttpClient with the given custom dependencies and
    // configuration.
    httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultCookieStore(cookieStore)
            .setDefaultCookieSpecRegistry(r)
            /* .setProxy(new HttpHost("myproxy", 8080)) */
            .setDefaultRequestConfig(defaultRequestConfig).build();

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    /*
     * connectionMonitorThread.start(); try {
     * connectionMonitorThread.join(); } catch (InterruptedException e) { //
     * TODO Auto-generated catch block e.printStackTrace(); }
     */
    return this;
}

From source file:org.mule.modules.apifortress.ApiFortressClient.java

/**
 * Constructor//  ww w  .j a  va2  s .  c  om
 * @param config the connector config. Won't be referenced
 */
public ApiFortressClient(Config config) {
    super();

    connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(config.getTotalConnections());

    final RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(config.getConnectTimeout() * 1000)
            .setSocketTimeout(config.getSocketTimeout() * 1000).build();
    client = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
            .build();
    silent = config.isSilent();
    dryRun = config.isDryRun();
    threshold = config.getThreshold();

}