com.newrelic.insights.publish.MultiThreadedInsightsClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of insights_client Show documentation
Show all versions of insights_client Show documentation
a java client library for posting events to New Relic Insights
The newest version!
package com.newrelic.insights.publish;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
public class MultiThreadedInsightsClient extends InsightsClient {
public MultiThreadedInsightsClient(String insightsAPIUrl, String insightsAPIInsertKey) {
super(insightsAPIUrl, insightsAPIInsertKey);
}
@Override
public void init(ClientConnectionConfiguration connConfig) {
RequestConfig requestConfig;
List headers = new ArrayList();
//Header contentTypeHeader = new BasicHeader(HttpHeaders.CONTENT_TYPE, PROPERTY_VALUE_HEADER_CONTENT_TYPE);
Header insertKeyHeader = new BasicHeader(PROPERTY_NAME_HEADER_X_INSERT_KEY, insightsAPIInsertKey);
headers.add(insertKeyHeader);
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(connConfig.getMaximumConnections());
connManager.setDefaultMaxPerRoute(connConfig.getMaximumConnectionsPerRoute());
if (connConfig.isUseProxy()) {
HttpHost proxy = new HttpHost(connConfig.getProxyHost(), connConfig.getProxyPort(), connConfig.getProxyScheme());
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connConfig.getConnectionRequestTimeout())
.setConnectTimeout(connConfig.getConnectTimeout()).setSocketTimeout(connConfig.getSocketTimeout())
.setProxy(proxy )
.build();
if ((connConfig.getProxyUsername() != null) && (connConfig.getProxyPassword() != null)) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(connConfig.getProxyHost(), connConfig.getProxyPort()),
new UsernamePasswordCredentials((connConfig.getProxyUsername()).trim(), connConfig.getProxyPassword().trim()));
httpclient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setDefaultHeaders(headers).setConnectionManager(connManager).setDefaultCredentialsProvider(credentialsProvider).build();
} else {
httpclient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setDefaultHeaders(headers).setConnectionManager(connManager).build();
}
} else {
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connConfig.getConnectionRequestTimeout())
.setConnectTimeout(connConfig.getConnectTimeout()).setSocketTimeout(connConfig.getSocketTimeout())
.build();
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setDefaultHeaders(headers).setConnectionManager(connManager);
httpclient = builder.build();
}
}
}