Azure / azure-notificationhubs-java-backend

Azure Notification Hubs SDK for Java
https://docs.microsoft.com/en-us/azure/notification-hubs/
Apache License 2.0
35 stars 49 forks source link

Environment variable to select the strategy of httpclient creation #30

Closed jmgrozas closed 4 years ago

jmgrozas commented 7 years ago

I have the same problem ... My work around is using environment var to select the strategy of httpclient creation ....

To select strategy:

System.setProperty(HttpClientManager.SYSTEM_HTTP_CLIENT_CREATE,HttpClientManager.HttpClientType.SYSTEM.name());

/** Name of system property to activate httpAsyncClient create with system properties */
public static final String SYSTEM_HTTP_CLIENT_CREATE = "http_client_type";

private static CloseableHttpAsyncClient httpAsyncClient;

public static CloseableHttpAsyncClient getHttpAsyncClient() {
    if(httpAsyncClient == null) {
        synchronized(HttpClientManager.class) {
            if(httpAsyncClient == null) {
                /* Create http client by type */
                HttpClientType clientType = HttpClientType.valueOf(System.getProperty(SYSTEM_HTTP_CLIENT_CREATE));
                CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
                if (HttpClientType.MINIMAL.equals(clientType)){
                    client = HttpAsyncClients.createMinimal();
                } else if (HttpClientType.SYSTEM.equals(clientType)){
                    client = HttpAsyncClients.createSystem();
                }else {
                    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.");
        }
    }
}

/**
 * Enum represent httpClients
 * 
 * @author manuel
 *
 */
public static enum HttpClientType {
    DEFAULT, MINIMAL, SYSTEM;
}

``

jmgrozas commented 7 years ago

To config proxy for example: set http.proxyHost set http.proxyPort or pass -D into java execution command.

java -Dhttp.proxyHost=myproxydomain.com -Dhttp.proxyPort=8080 -jar example.jar

All available system properties are:

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html

Regards.

mpodwysocki commented 4 years ago

Closing as per stale