public static void main(final String[] args) throws Exception { System.out.println("-------------------------------------------------------------------"); System.out.println("java.version: " + System.getProperty("java.version")); System.out.println("java.vendor.url: " + System.getProperty("java.vendor.url")); System.out.println("java.vendor: " + System.getProperty("java.vendor")); System.out.println("java.home: " + System.getProperty("java.home")); System.out.println("os.arch: " + System.getProperty("os.arch")); System.out.println("os.name: " + System.getProperty("os.name")); System.out.println("os.version: " + System.getProperty("os.version")); System.out.println("-------------------------------------------------------------------"); CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); httpclient.start(); try { long start = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { Future<Boolean> future = httpclient.execute( HttpAsyncMethods.createGet("http://www.google.com/"), new MyResponseConsumer(), null); Boolean result = future.get(); System.out.println("result: " + result); if (!result) { System.out.println("Request failed"); } } long end = System.currentTimeMillis(); System.out.println("Total time (sec):" + ((double) (end - start)) / 1000); } finally { httpclient.close(); } System.out.println("Done"); }
public Object get( String url, AbstractAsyncResponseConsumer myConsumer, Map<String, String> headerMap) throws IOException { URI uri = URI.create(url); CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); Object obj = null; try { httpclient.start(); HttpGet httpget = new HttpGet(uri); HttpHost httphost = URIUtils.extractHost(uri); if (headerMap != null) { for (String key : headerMap.keySet()) { httpget.addHeader(key, headerMap.get(key)); } } BasicAsyncRequestProducer requset = new BasicAsyncRequestProducer(httphost, httpget); Future<Object> future = httpclient.execute(requset, myConsumer, null); obj = future.get(); } catch (InterruptedException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (ExecutionException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } finally { httpclient.close(); return obj; } }
public Object post(String url, AbstractAsyncResponseConsumer myConsumer, String json) throws IOException { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); Object obj = null; try { httpclient.start(); Future<Object> future = httpclient.execute( HttpAsyncMethods.createPost(url, json, ContentType.APPLICATION_JSON), myConsumer, null); obj = future.get(); } catch (InterruptedException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (ExecutionException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } finally { httpclient.close(); return obj; } }
/** * Close the asynchronous client and its event loop. Use this method to close all the threads and * allow an application to exit. */ public static void shutdown() throws IOException { // Closing the sync client CloseableHttpClient syncClient = (CloseableHttpClient) Options.getOption(Option.HTTPCLIENT); syncClient.close(); SyncIdleConnectionMonitorThread syncIdleConnectionMonitorThread = (SyncIdleConnectionMonitorThread) Options.getOption(Option.SYNC_MONITOR); syncIdleConnectionMonitorThread.shutdown(); // Closing the async client (if running) CloseableHttpAsyncClient asyncClient = (CloseableHttpAsyncClient) Options.getOption(Option.ASYNCHTTPCLIENT); if (asyncClient.isRunning()) { asyncClient.close(); AsyncIdleConnectionMonitorThread asyncIdleConnectionMonitorThread = (AsyncIdleConnectionMonitorThread) Options.getOption(Option.ASYNC_MONITOR); asyncIdleConnectionMonitorThread.shutdown(); } }
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try { httpclient.start(); Future<Boolean> future = httpclient.execute( HttpAsyncMethods.createGet("http://www.sina.com.cn/"), new MyResponseConsumer(), null); Boolean result = future.get(); if (result != null && result.booleanValue()) { System.out.println("Request successfully executed"); } else { System.out.println("Request failed"); } System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }