List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager()
From source file:com.yee.httpclient.ClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(200);//from w w w .j a v a2 s . c om cm.setDefaultMaxPerRoute(20); new IdleConnectionEvictor(cm).start(); }
From source file:cn.itcast.httpclient.ClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // /*from w w w. ja va 2 s .com*/ cm.setMaxTotal(200); // ??? cm.setDefaultMaxPerRoute(20); new IdleConnectionEvictor(cm).start(); }
From source file:org.sourcepit.consul.forwarder.Main.java
public static void main(String[] args) { final String dockerUri = "http://192.168.56.101:2375"; final String consulUri = "http://192.168.56.101:8500"; final int fetchConsulStateInterval = 30; final int fetchDockerStateInterval = 30; final int dispatchItemsInterval = 2; final int requestDockerStateDelay = 5; PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultMaxPerRoute(10); final HttpClient httpClient = HttpClients.createMinimal(connManager); final BlockingQueue<JsonObject> queue = new LinkedBlockingQueue<>(); final FetchConsulStateCmd fetchConsulStateCmd = new FetchConsulStateCmd(httpClient, consulUri) { @Override/*from w w w . ja va2 s . co m*/ protected void doHandle(JsonObject consulState) { final JsonObject item = createItem("ConsulState", consulState); LOG.debug(item.toString()); queue.add(item); } }; final FetchDockerStateCmd fetchDockerStateCmd = new FetchDockerStateCmd(httpClient, dockerUri) { @Override protected void doHandle(JsonArray dockerState) { final JsonObject item = createItem("DockerState", dockerState); LOG.debug(item.toString()); queue.add(item); } }; final ConsulForwarderState forwarderState = new ConsulForwarderState(); final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); scheduler.scheduleAtFixedRate(fetchConsulStateCmd, 0, fetchConsulStateInterval, TimeUnit.SECONDS); scheduler.scheduleAtFixedRate(fetchDockerStateCmd, 0, fetchDockerStateInterval, TimeUnit.SECONDS); scheduler.scheduleAtFixedRate(new DispatchItemsCmd(queue) { @Override protected void handleDockerState(JsonArray dockerState) { forwarderState.applyDockerState(dockerState); } @Override protected void handleDockerEvents(JsonArray dockerEvents) { // trigger docker state update scheduler.schedule(fetchDockerStateCmd, requestDockerStateDelay, TimeUnit.SECONDS); } @Override protected void handleConsulState(JsonObject consulState) { forwarderState.applyConsulState(consulState); } }, 0, dispatchItemsInterval, TimeUnit.SECONDS); final DockerEventObserver eventObserver = new DockerEventObserver(httpClient, dockerUri) { @Override protected void handle(JsonObject event) { queue.add(createItem("DockerEvent", event)); } }; final Thread eventObserverThread = new Thread(eventObserver, "Docker Event Observer") { @Override public void interrupt() { eventObserver.die(); super.interrupt(); } }; eventObserverThread.start(); }
From source file:interoperabilite.webservice.client.ClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100);/* ww w .ja v a 2s .c om*/ CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).evictExpiredConnections() .evictIdleConnections(5L, TimeUnit.SECONDS).build(); try { // create an array of URIs to perform GETs on String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/", "http://hc.apache.org/httpcomponents-client-ga/", }; for (int i = 0; i < urisToGet.length; i++) { String requestURI = urisToGet[i]; HttpGet request = new HttpGet(requestURI); System.out.println("Executing request " + requestURI); CloseableHttpResponse response = httpclient.execute(request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); } finally { response.close(); } } PoolStats stats1 = cm.getTotalStats(); System.out.println("Connections kept alive: " + stats1.getAvailable()); // Sleep 10 sec and let the connection evictor do its job Thread.sleep(10000); PoolStats stats2 = cm.getTotalStats(); System.out.println("Connections kept alive: " + stats2.getAvailable()); } finally { httpclient.close(); } }
From source file:com.lxf.spider.client.ClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100);//from ww w . j a v a 2s . c o m CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try { // create an array of URIs to perform GETs on String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/", "http://hc.apache.org/httpcomponents-client-ga/", }; IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm); connEvictor.start(); for (int i = 0; i < urisToGet.length; i++) { String requestURI = urisToGet[i]; HttpGet request = new HttpGet(requestURI); System.out.println("Executing request " + requestURI); CloseableHttpResponse response = httpclient.execute(request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } // Sleep 10 sec and let the connection evictor do its job Thread.sleep(20000); // Shut down the evictor thread connEvictor.shutdown(); connEvictor.join(); } finally { httpclient.close(); } }
From source file:com.lxf.spider.client.ClientMultiThreadedExecution.java
public static void main(String[] args) throws Exception { // Create an HttpClient with the ThreadSafeClientConnManager. // This connection manager must be used if more than one thread will // be using the HttpClient. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100);/*from w w w .j av a2 s . c om*/ CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try { // create an array of URIs to perform GETs on String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/", "http://hc.apache.org/httpcomponents-client-ga/", }; // create a thread for each URI GetThread[] threads = new GetThread[urisToGet.length]; for (int i = 0; i < threads.length; i++) { HttpGet httpget = new HttpGet(urisToGet[i]); threads[i] = new GetThread(httpclient, httpget, i + 1); } // start the threads for (int j = 0; j < threads.length; j++) { threads[j].start(); } // join the threads for (int j = 0; j < threads.length; j++) { threads[j].join(); } } finally { httpclient.close(); } }
From source file:com.scutdm.summary.extract.ClientMultiThreadedExecution.java
public static void main(String[] args) throws Exception { // Create an HttpClient with the ThreadSafeClientConnManager. // This connection manager must be used if more than one thread will // be using the HttpClient. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100);/*from w w w . j a v a 2 s .co m*/ CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try { // create an array of URIs to perform GETs on String[] urisToGet = { "http://money.21cbh.com/2014/5-8/zOMDA2NzZfMTE1OTYzOA.html", "http://aoxiaobai.baijia.baidu.com/article/14834", "http://biz.21cbh.com/2014/5-8/yOMDA0MTVfMTE1OTEyOA.html", }; // create a thread for each URI GetThread[] threads = new GetThread[urisToGet.length]; for (int i = 0; i < threads.length; i++) { HttpGet httpget = new HttpGet(urisToGet[i]); threads[i] = new GetThread(httpclient, httpget, i + 1); } // start the threads for (int j = 0; j < threads.length; j++) { threads[j].start(); } // join the threads for (int j = 0; j < threads.length; j++) { threads[j].join(); } } finally { httpclient.close(); } }
From source file:com.cloudera.oryx.app.traffic.TrafficUtil.java
public static void main(String[] args) throws Exception { if (args.length < 3) { System.err.println("usage: TrafficUtil [hosts] [requestIntervalMS] [threads] [... other args]"); return;/*w w w . j av a 2s . c o m*/ } String[] hostStrings = COMMA.split(args[0]); Preconditions.checkArgument(hostStrings.length >= 1); int requestIntervalMS = Integer.parseInt(args[1]); Preconditions.checkArgument(requestIntervalMS >= 0); int numThreads = Integer.parseInt(args[2]); Preconditions.checkArgument(numThreads >= 1); String[] otherArgs = new String[args.length - 3]; System.arraycopy(args, 3, otherArgs, 0, otherArgs.length); List<URI> hosts = Arrays.stream(hostStrings).map(URI::create).collect(Collectors.toList()); int perClientRequestIntervalMS = numThreads * requestIntervalMS; Endpoints alsEndpoints = new Endpoints(ALSEndpoint.buildALSEndpoints()); AtomicLong requestCount = new AtomicLong(); AtomicLong serverErrorCount = new AtomicLong(); AtomicLong clientErrorCount = new AtomicLong(); AtomicLong exceptionCount = new AtomicLong(); long start = System.currentTimeMillis(); ExecUtils.doInParallel(numThreads, numThreads, true, i -> { RandomGenerator random = RandomManager.getRandom(Integer.toString(i).hashCode() ^ System.nanoTime()); ExponentialDistribution msBetweenRequests; if (perClientRequestIntervalMS > 0) { msBetweenRequests = new ExponentialDistribution(random, perClientRequestIntervalMS); } else { msBetweenRequests = null; } ClientConfig clientConfig = new ClientConfig(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(numThreads); connectionManager.setDefaultMaxPerRoute(numThreads); clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager); clientConfig.connectorProvider(new ApacheConnectorProvider()); Client client = ClientBuilder.newClient(clientConfig); try { while (true) { try { WebTarget target = client.target("http://" + hosts.get(random.nextInt(hosts.size()))); Endpoint endpoint = alsEndpoints.chooseEndpoint(random); Invocation invocation = endpoint.makeInvocation(target, otherArgs, random); long startTime = System.currentTimeMillis(); Response response = invocation.invoke(); try { response.readEntity(String.class); } finally { response.close(); } long elapsedMS = System.currentTimeMillis() - startTime; int statusCode = response.getStatusInfo().getStatusCode(); if (statusCode >= 400) { if (statusCode >= 500) { serverErrorCount.incrementAndGet(); } else { clientErrorCount.incrementAndGet(); } } endpoint.recordTiming(elapsedMS); if (requestCount.incrementAndGet() % 10000 == 0) { long elapsed = System.currentTimeMillis() - start; log.info("{}ms:\t{} requests\t({} client errors\t{} server errors\t{} exceptions)", elapsed, requestCount.get(), clientErrorCount.get(), serverErrorCount.get(), exceptionCount.get()); for (Endpoint e : alsEndpoints.getEndpoints()) { log.info("{}", e); } } if (msBetweenRequests != null) { int desiredElapsedMS = (int) Math.round(msBetweenRequests.sample()); if (elapsedMS < desiredElapsedMS) { Thread.sleep(desiredElapsedMS - elapsedMS); } } } catch (Exception e) { exceptionCount.incrementAndGet(); log.warn("{}", e.getMessage()); } } } finally { client.close(); } }); }
From source file:eu.esdihumboldt.hale.common.cache.ClientUtil.java
/** * Create a thread safe HTTP client/*from w w w . j ava 2 s . co m*/ * * @return the created HTTP client */ public static HttpClientBuilder threadSafeHttpClientBuilder() { // create HTTP client builder PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); return HttpClientBuilder.create().setConnectionManager(cm); }
From source file:eu.delving.sip.base.HttpClientFactory.java
public static HttpClient createHttpClient(File storageDirectory) { HttpClientBuilder builder = HttpClientBuilder.create().disableAutomaticRetries(); builder.setConnectionManager(new PoolingHttpClientConnectionManager()); if (!StorageFinder.isStandalone(storageDirectory)) { String serverUrl = String.format("http://%s", getHostPort(storageDirectory)); handleProxy(serverUrl, builder); }//w w w . java2 s. co m return builder.build(); }