|
1 | 1 | package com.windowsazure.messaging;
|
2 | 2 |
|
3 | 3 | import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
4 |
| -import org.apache.http.impl.nio.client.HttpAsyncClients; |
| 4 | +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; |
| 5 | +import org.apache.http.client.config.RequestConfig; |
5 | 6 |
|
6 | 7 | public class HttpClientManager {
|
7 |
| - private static CloseableHttpAsyncClient httpAsyncClient; |
8 |
| - |
9 |
| - public static CloseableHttpAsyncClient getHttpAsyncClient() { |
10 |
| - if(httpAsyncClient == null) { |
11 |
| - synchronized(HttpClientManager.class) { |
12 |
| - if(httpAsyncClient == null) { |
13 |
| - CloseableHttpAsyncClient client = HttpAsyncClients.createDefault(); |
14 |
| - client.start(); |
15 |
| - httpAsyncClient = client; |
16 |
| - } |
17 |
| - } |
18 |
| - } |
19 |
| - |
20 |
| - return httpAsyncClient; |
21 |
| - } |
22 |
| - |
23 |
| - public static void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) { |
24 |
| - synchronized(HttpClientManager.class) { |
25 |
| - if(HttpClientManager.httpAsyncClient == null) { |
26 |
| - HttpClientManager.httpAsyncClient = httpAsyncClient; |
27 |
| - } |
28 |
| - else{ |
29 |
| - throw new RuntimeException("HttpAsyncClient was already set before or default one is being used."); |
30 |
| - } |
31 |
| - } |
32 |
| - } |
| 8 | + |
| 9 | + private static CloseableHttpAsyncClient httpAsyncClient; |
| 10 | + |
| 11 | + // A timeout value of zero is interpreted as an infinite timeout. |
| 12 | + // A negative value is interpreted as undefined (system default). |
| 13 | + // https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.html#getConnectionRequestTimeout() |
| 14 | + |
| 15 | + // The timeout in milliseconds used when requesting a connection from the connection manager. |
| 16 | + private static int connectionRequestTimeout = -1; |
| 17 | + |
| 18 | + // The timeout in milliseconds until a connection is established. |
| 19 | + private static int connectionTimeout = -1; |
| 20 | + |
| 21 | + // The socket timeout in milliseconds, which is the timeout for waiting for data or, |
| 22 | + // put differently, a maximum period inactivity between two consecutive data packets. |
| 23 | + private static int socketTimeout = -1; |
| 24 | + |
| 25 | + private static void initializeHttpAsyncClient() { |
| 26 | + synchronized (HttpClientManager.class) { |
| 27 | + if (httpAsyncClient == null) { |
| 28 | + RequestConfig config = RequestConfig.custom() |
| 29 | + .setConnectionRequestTimeout(connectionRequestTimeout) |
| 30 | + .setConnectTimeout(connectionTimeout) |
| 31 | + .setSocketTimeout(socketTimeout) |
| 32 | + .build(); |
| 33 | + CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create() |
| 34 | + .setDefaultRequestConfig(config) |
| 35 | + .build(); |
| 36 | + client.start(); |
| 37 | + httpAsyncClient = client; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static CloseableHttpAsyncClient getHttpAsyncClient() { |
| 43 | + if (httpAsyncClient == null) { |
| 44 | + initializeHttpAsyncClient(); |
| 45 | + } |
| 46 | + return httpAsyncClient; |
| 47 | + } |
| 48 | + |
| 49 | + public static void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) { |
| 50 | + synchronized (HttpClientManager.class) { |
| 51 | + if (HttpClientManager.httpAsyncClient == null) { |
| 52 | + HttpClientManager.httpAsyncClient = httpAsyncClient; |
| 53 | + } else { |
| 54 | + throw new RuntimeException("Cannot setHttpAsyncClient after having previously set, or after default already initialized from earlier call to getHttpAsyncClient."); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Sets the timeout in milliseconds used when requesting a connection from the connection manager. |
| 60 | + public static void setConnectionRequestTimeout(int timeout) { |
| 61 | + if (HttpClientManager.httpAsyncClient == null) { |
| 62 | + connectionRequestTimeout = timeout; |
| 63 | + } else { |
| 64 | + throw new RuntimeException("Cannot setConnectionRequestTimeout after previously setting httpAsyncClient, or after default already initialized from earlier call to getHttpAsyncClient."); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Sets the timeout in milliseconds until a connection is established. |
| 69 | + public static void setConnectTimeout(int timeout) { |
| 70 | + if (HttpClientManager.httpAsyncClient == null) { |
| 71 | + connectionTimeout = timeout; |
| 72 | + } else { |
| 73 | + throw new RuntimeException("Cannot setConnectTimeout after previously setting httpAsyncClient, or after default already initialized from earlier call to getHttpAsyncClient."); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Sets the timeout in milliseconds for waiting for data or, |
| 78 | + // put differently, a maximum period inactivity between two consecutive data packets. |
| 79 | + public static void setSocketTimeout(int timeout) { |
| 80 | + if (HttpClientManager.httpAsyncClient == null) { |
| 81 | + socketTimeout = timeout; |
| 82 | + } else { |
| 83 | + throw new RuntimeException("Cannot setSocketTimeout after previously setting httpAsyncClient, or after default already initialized from earlier call to getHttpAsyncClient."); |
| 84 | + } |
| 85 | + } |
33 | 86 | }
|
0 commit comments