-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from Azure/timeout
Capability to configure timeouts
- Loading branch information
Showing
2 changed files
with
81 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 80 additions & 27 deletions
107
NotificationHubs/src/com/windowsazure/messaging/HttpClientManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,86 @@ | ||
package com.windowsazure.messaging; | ||
|
||
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; | ||
import org.apache.http.impl.nio.client.HttpAsyncClients; | ||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; | ||
import org.apache.http.client.config.RequestConfig; | ||
|
||
public class HttpClientManager { | ||
private static CloseableHttpAsyncClient httpAsyncClient; | ||
|
||
public static CloseableHttpAsyncClient getHttpAsyncClient() { | ||
if(httpAsyncClient == null) { | ||
synchronized(HttpClientManager.class) { | ||
if(httpAsyncClient == null) { | ||
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault(); | ||
client.start(); | ||
httpAsyncClient = client; | ||
} | ||
} | ||
} | ||
|
||
return httpAsyncClient; | ||
} | ||
|
||
public static void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) { | ||
synchronized(HttpClientManager.class) { | ||
if(HttpClientManager.httpAsyncClient == null) { | ||
HttpClientManager.httpAsyncClient = httpAsyncClient; | ||
} | ||
else{ | ||
throw new RuntimeException("HttpAsyncClient was already set before or default one is being used."); | ||
} | ||
} | ||
} | ||
|
||
private static CloseableHttpAsyncClient httpAsyncClient; | ||
|
||
// A timeout value of zero is interpreted as an infinite timeout. | ||
// A negative value is interpreted as undefined (system default). | ||
// https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.html#getConnectionRequestTimeout() | ||
|
||
// The timeout in milliseconds used when requesting a connection from the connection manager. | ||
private static int connectionRequestTimeout = -1; | ||
|
||
// The timeout in milliseconds until a connection is established. | ||
private static int connectionTimeout = -1; | ||
|
||
// The socket timeout in milliseconds, which is the timeout for waiting for data or, | ||
// put differently, a maximum period inactivity between two consecutive data packets. | ||
private static int socketTimeout = -1; | ||
|
||
private static void initializeHttpAsyncClient() { | ||
synchronized (HttpClientManager.class) { | ||
if (httpAsyncClient == null) { | ||
RequestConfig config = RequestConfig.custom() | ||
.setConnectionRequestTimeout(connectionRequestTimeout) | ||
.setConnectTimeout(connectionTimeout) | ||
.setSocketTimeout(socketTimeout) | ||
.build(); | ||
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create() | ||
.setDefaultRequestConfig(config) | ||
.build(); | ||
client.start(); | ||
httpAsyncClient = client; | ||
} | ||
} | ||
} | ||
|
||
public static CloseableHttpAsyncClient getHttpAsyncClient() { | ||
if (httpAsyncClient == null) { | ||
initializeHttpAsyncClient(); | ||
} | ||
return httpAsyncClient; | ||
} | ||
|
||
public static void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) { | ||
synchronized (HttpClientManager.class) { | ||
if (HttpClientManager.httpAsyncClient == null) { | ||
HttpClientManager.httpAsyncClient = httpAsyncClient; | ||
} else { | ||
throw new RuntimeException("Cannot setHttpAsyncClient after having previously set, or after default already initialized from earlier call to getHttpAsyncClient."); | ||
} | ||
} | ||
} | ||
|
||
// Sets the timeout in milliseconds used when requesting a connection from the connection manager. | ||
public static void setConnectionRequestTimeout(int timeout) { | ||
if (HttpClientManager.httpAsyncClient == null) { | ||
connectionRequestTimeout = timeout; | ||
} else { | ||
throw new RuntimeException("Cannot setConnectionRequestTimeout after previously setting httpAsyncClient, or after default already initialized from earlier call to getHttpAsyncClient."); | ||
} | ||
} | ||
|
||
// Sets the timeout in milliseconds until a connection is established. | ||
public static void setConnectTimeout(int timeout) { | ||
if (HttpClientManager.httpAsyncClient == null) { | ||
connectionTimeout = timeout; | ||
} else { | ||
throw new RuntimeException("Cannot setConnectTimeout after previously setting httpAsyncClient, or after default already initialized from earlier call to getHttpAsyncClient."); | ||
} | ||
} | ||
|
||
// Sets the timeout in milliseconds for waiting for data or, | ||
// put differently, a maximum period inactivity between two consecutive data packets. | ||
public static void setSocketTimeout(int timeout) { | ||
if (HttpClientManager.httpAsyncClient == null) { | ||
socketTimeout = timeout; | ||
} else { | ||
throw new RuntimeException("Cannot setSocketTimeout after previously setting httpAsyncClient, or after default already initialized from earlier call to getHttpAsyncClient."); | ||
} | ||
} | ||
} |