List of usage examples for org.apache.http.impl.nio.reactor DefaultConnectingIOReactor DefaultConnectingIOReactor
DefaultConnectingIOReactor
From source file:com.boonya.http.async.examples.nio.client.AsyncClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor); cm.setMaxTotal(100);//from w w w . ja va2 s. co m CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setConnectionManager(cm).build(); try { httpclient.start(); // 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(); final CountDownLatch latch = new CountDownLatch(urisToGet.length); for (final String uri : urisToGet) { final HttpGet httpget = new HttpGet(uri); httpclient.execute(httpget, new FutureCallback<HttpResponse>() { @Override public void completed(final HttpResponse response) { latch.countDown(); System.out.println(httpget.getRequestLine() + "->" + response.getStatusLine()); } @Override public void failed(final Exception ex) { latch.countDown(); System.out.println(httpget.getRequestLine() + "->" + ex); } @Override public void cancelled() { latch.countDown(); System.out.println(httpget.getRequestLine() + " cancelled"); } }); } latch.await(); // 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.yulore.demo.NHttpClient.java
public static void main(String[] args) throws Exception { // Create HTTP protocol processing chain HttpProcessor httpproc = HttpProcessorBuilder.create() // Use standard client-side protocol interceptors .add(new RequestContent()).add(new RequestTargetHost()).add(new RequestConnControl()) .add(new RequestUserAgent("Test/1.1")).add(new RequestExpectContinue(true)).build(); // Create client-side HTTP protocol handler HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor(); // Create client-side I/O event dispatch final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT);//from www .j a v a2 s . c o m // Create client-side I/O reactor final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); // Create HTTP connection pool BasicNIOConnPool pool = new BasicNIOConnPool(ioReactor, ConnectionConfig.DEFAULT); // Limit total number of connections to just two pool.setDefaultMaxPerRoute(2); pool.setMaxTotal(2); // Run the I/O reactor in a separate thread Thread t = new Thread(new Runnable() { public void run() { try { // Ready to go! ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } System.out.println("Shutdown"); } }); // Start the client thread t.start(); // Create HTTP requester HttpAsyncRequester requester = new HttpAsyncRequester(httpproc); // Execute HTTP GETs to the following hosts and HttpHost[] targets = new HttpHost[] { new HttpHost("www.apache.org", 80, "http"), new HttpHost("www.verisign.com", 443, "https"), new HttpHost("www.google.com", 80, "http") }; final CountDownLatch latch = new CountDownLatch(targets.length); for (final HttpHost target : targets) { BasicHttpRequest request = new BasicHttpRequest("GET", "/"); HttpCoreContext coreContext = HttpCoreContext.create(); requester.execute(new BasicAsyncRequestProducer(target, request), new BasicAsyncResponseConsumer(), pool, coreContext, // Handle HTTP response from a callback new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { latch.countDown(); System.out.println(target + "->" + response.getStatusLine()); } public void failed(final Exception ex) { latch.countDown(); System.out.println(target + "->" + ex); } public void cancelled() { latch.countDown(); System.out.println(target + " cancelled"); } }); } latch.await(); System.out.println("Shutting down I/O reactor"); ioReactor.shutdown(); System.out.println("Done"); }
From source file:framework.httpclient.nio.NHttpClient.java
public static void main(String[] args) throws Exception { // Create HTTP protocol processing chain HttpProcessor httpproc = HttpProcessorBuilder.create() // Use standard client-side protocol interceptors .add(new RequestContent()).add(new RequestTargetHost()).add(new RequestConnControl()) .add(new RequestUserAgent("LinkedHashSetVsTreeSet/1.1")).add(new RequestExpectContinue(true)) .build();/* w w w .j a v a 2s. c o m*/ // Create client-side HTTP protocol handler HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor(); // Create client-side I/O event dispatch // IO final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT); // Create client-side I/O reactor // IO reactor final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); // Create HTTP connection pool // HTTP BasicNIOConnPool pool = new BasicNIOConnPool(ioReactor, ConnectionConfig.DEFAULT); // Limit total number of connections to just two pool.setDefaultMaxPerRoute(2); pool.setMaxTotal(2); // Run the I/O reactor in a separate thread Thread t = new Thread(new Runnable() { public void run() { try { // Ready to go! ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } System.out.println("Shutdown"); } }); // Start the client thread t.start(); // Create HTTP requester // HTTP HttpAsyncRequester requester = new HttpAsyncRequester(httpproc); // Execute HTTP GETs to the following hosts and HttpHost[] targets = new HttpHost[] { new HttpHost("www.baidu.org", -1, "https"), // new HttpHost("www.zhihu.com", -1, "https"), new HttpHost("www.bilibili.com", -1, "https") }; final CountDownLatch latch = new CountDownLatch(targets.length); for (final HttpHost target : targets) { BasicHttpRequest request = new BasicHttpRequest("GET", "/"); HttpCoreContext coreContext = HttpCoreContext.create(); requester.execute(new BasicAsyncRequestProducer(target, request), new BasicAsyncResponseConsumer(), pool, coreContext, // Handle HTTP response from a callback new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { latch.countDown(); System.out.println(target + "->" + response.getStatusLine()); } public void failed(final Exception ex) { latch.countDown(); System.err.println(target + "->" + ex); ex.printStackTrace(); } public void cancelled() { latch.countDown(); System.out.println(target + " cancelled"); } }); } latch.await(); System.out.println("Shutting down I/O reactor"); ioReactor.shutdown(); System.out.println("Done"); }
From source file:org.voltdb.exportclient.TeradataListenerExportClient.java
private void connect() throws IOReactorException { if (m_connManager == null) { ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); m_connManager = new PoolingNHttpClientConnectionManager(ioReactor); m_connManager.setMaxTotal(HTTP_EXPORT_MAX_CONNS); m_connManager.setDefaultMaxPerRoute(HTTP_EXPORT_MAX_CONNS); }/*ww w . j a v a2s .co m*/ if (m_client == null || !m_client.isRunning()) { HttpAsyncClientBuilder client = HttpAsyncClients.custom().setConnectionManager(m_connManager); m_client = client.build(); m_client.start(); } }
From source file:org.apache.zeppelin.notebook.repo.zeppelinhub.rest.HttpProxyClient.java
private PoolingNHttpClientConnectionManager getAsyncConnectionManager() { ConnectingIOReactor ioReactor = null; PoolingNHttpClientConnectionManager cm = null; try {/*from w ww . j ava2s .c o m*/ ioReactor = new DefaultConnectingIOReactor(); // ssl setup SSLContext sslcontext = SSLContexts.createSystemDefault(); X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier(); @SuppressWarnings("deprecation") Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder .<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE) .register("https", new SSLIOSessionStrategy(sslcontext, hostnameVerifier)).build(); cm = new PoolingNHttpClientConnectionManager(ioReactor, sessionStrategyRegistry); } catch (IOReactorException e) { LOG.error("Couldn't initialize multi-threaded async client ", e); return null; } return cm; }
From source file:org.apache.gobblin.http.ApacheHttpAsyncClient.java
private NHttpClientConnectionManager getNHttpConnManager(Config config) throws IOException { NHttpClientConnectionManager httpConnManager; String connMgrStr = config.getString(HTTP_CONN_MANAGER); switch (ApacheHttpClient.ConnManager.valueOf(connMgrStr.toUpperCase())) { case POOLING: ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); PoolingNHttpClientConnectionManager poolingConnMgr = new PoolingNHttpClientConnectionManager(ioReactor); poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN)); poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN)); httpConnManager = poolingConnMgr; break;//from w w w .j av a2 s .c o m default: throw new IllegalArgumentException(connMgrStr + " is not supported"); } LOG.info("Using " + httpConnManager.getClass().getSimpleName()); return httpConnManager; }
From source file:org.springframework.integration.splunk.support.SplunkHECWriter.java
@Override public void start() { try {/*from www . ja v a2 s . c om*/ this.batchBuffer = Collections.synchronizedList(new LinkedList<String>()); this.lastEventReceivedTime = System.currentTimeMillis(); Registry<SchemeIOSessionStrategy> sslSessionStrategy = RegistryBuilder.<SchemeIOSessionStrategy>create() .register("http", NoopIOSessionStrategy.INSTANCE) .register("https", new SSLIOSessionStrategy(getSSLContext(), HOSTNAME_VERIFIER)).build(); ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor, sslSessionStrategy); cm.setMaxTotal(getPoolsize()); HttpHost splunk = new HttpHost(getHost(), getPort()); cm.setMaxPerRoute(new HttpRoute(splunk), getPoolsize()); httpClient = HttpAsyncClients.custom().setConnectionManager(cm).build(); uri = new URIBuilder().setScheme(isHttps() ? "https" : "http").setHost(getHost()).setPort(getPort()) .setPath("/services/collector").build(); httpClient.start(); if (isBatchMode()) { new BatchBufferActivityCheckerThread(this).start(); } } catch (Exception e) { } this.running = true; }
From source file:com.liferay.petra.json.web.service.client.BaseJSONWebServiceClientImpl.java
protected PoolingNHttpClientConnectionManager getPoolingNHttpClientConnectionManager() throws IOReactorException { PoolingNHttpClientConnectionManager poolingNHttpClientConnectionManager = null; ConnectingIOReactor connectingIOReactor = new DefaultConnectingIOReactor(); if (_keyStore != null) { poolingNHttpClientConnectionManager = new PoolingNHttpClientConnectionManager(connectingIOReactor, null, getSchemeIOSessionStrategyRegistry(), null, null, 60000, TimeUnit.MILLISECONDS); } else {/*from ww w.jav a 2 s . c o m*/ poolingNHttpClientConnectionManager = new PoolingNHttpClientConnectionManager(connectingIOReactor); } poolingNHttpClientConnectionManager.setMaxTotal(20); return poolingNHttpClientConnectionManager; }