Skip to content

Commit

Permalink
Merge pull request #43 from Azure/timeout
Browse files Browse the repository at this point in the history
Capability to configure timeouts
  • Loading branch information
stankovski authored Mar 20, 2019
2 parents 18c63be + f4121fa commit f855a18
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 28 deletions.
2 changes: 1 addition & 1 deletion NotificationHubs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.windowsazure</groupId>
<artifactId>Notification-Hubs-java-sdk</artifactId>
<version>0.0.5</version>
<version>0.1.0</version>
<name>Windows Azure Notification Hubs - Java REST wrapper</name>
<build>
<sourceDirectory>src</sourceDirectory>
Expand Down
107 changes: 80 additions & 27 deletions NotificationHubs/src/com/windowsazure/messaging/HttpClientManager.java
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.");
}
}
}

0 comments on commit f855a18

Please sign in to comment.