Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0.6</version>
<name>Windows Azure Notification Hubs - Java REST wrapper</name>
<build>
<sourceDirectory>src</sourceDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,66 @@
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;


// A timeout value of zero is interpreted as an infinite timeout.
// A negative value is interpreted as undefined (system default).

// 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;

public static CloseableHttpAsyncClient getHttpAsyncClient() {
if(httpAsyncClient == null) {
if (httpAsyncClient == null) {
synchronized(HttpClientManager.class) {
if(httpAsyncClient == null) {
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
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;
httpAsyncClient = client;
}
}
}

return httpAsyncClient;
}

public static void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) {
synchronized(HttpClientManager.class) {
if(HttpClientManager.httpAsyncClient == null) {
if (HttpClientManager.httpAsyncClient == null) {
HttpClientManager.httpAsyncClient = httpAsyncClient;
}
else{
else {
throw new RuntimeException("HttpAsyncClient was already set before or default one is being used.");
}
}
}

public static void setConnectionRequestTimeout(int timeout) {
ConnectionRequestTimeout = timeout;
}

public static void setConnectTimeout(int timeout) {
ConnectionTimeout = timeout;
}

public static void setSocketTimeout(int timeout) {
SocketTimeout = timeout;
}
}