/** Set the connection timeout and socket timeout */ public static void setTimeouts(long connectionTimeout, long socketTimeout) { Options.setOption(Option.CONNECTION_TIMEOUT, connectionTimeout); Options.setOption(Option.SOCKET_TIMEOUT, socketTimeout); // Reload the client implementations Options.refresh(); }
public static void refresh() { // Load timeouts Object connectionTimeout = Options.getOption(Option.CONNECTION_TIMEOUT); if (connectionTimeout == null) connectionTimeout = CONNECTION_TIMEOUT; Object socketTimeout = Options.getOption(Option.SOCKET_TIMEOUT); if (socketTimeout == null) socketTimeout = SOCKET_TIMEOUT; // Create common default configuration RequestConfig clientConfig = RequestConfig.custom() .setConnectTimeout(((Long) connectionTimeout).intValue()) .setSocketTimeout(((Long) socketTimeout).intValue()) .build(); // Create clients setOption( Option.HTTPCLIENT, HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build()); CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create().setDefaultRequestConfig(clientConfig).build(); asyncClient.start(); setOption(Option.ASYNCHTTPCLIENT, asyncClient); }
/** Set default header */ @SuppressWarnings("unchecked") public static void setDefaultHeader(String name, String value) { Object headers = Options.getOption(Option.DEFAULT_HEADERS); if (headers == null) { headers = new HashMap<String, String>(); } ((Map<String, String>) headers).put(name, value); Options.setOption(Option.DEFAULT_HEADERS, headers); }
/** * 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(); } }
/** * Set the asynchronous AbstractHttpAsyncClient implementation to use for every asynchronous * request */ public static void setAsyncHttpClient(CloseableHttpAsyncClient asyncHttpClient) { Options.setOption(Option.ASYNCHTTPCLIENT, asyncHttpClient); }
/** Clear default headers */ public static void clearDefaultHeaders() { Options.setOption(Option.DEFAULT_HEADERS, null); }
/** Set the HttpClient implementation to use for every synchronous request */ public static void setHttpClient(HttpClient httpClient) { Options.setOption(Option.HTTPCLIENT, httpClient); }