use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project pinot by linkedin.
the class MultiGetRequestTest method testMultiGet.
@Test
public void testMultiGet() {
MultiGetRequest mget = new MultiGetRequest(Executors.newCachedThreadPool(), new MultiThreadedHttpConnectionManager());
List<String> urls = Arrays.asList("http://localhost:" + String.valueOf(portStart) + URI_PATH, "http://localhost:" + String.valueOf(portStart + 1) + URI_PATH, "http://localhost:" + String.valueOf(portStart + 2) + URI_PATH, // 2nd request to the same server
"http://localhost:" + String.valueOf(portStart) + URI_PATH);
// timeout value needs to be less than 5000ms set above for
// third server
final int requestTimeoutMs = 1000;
CompletionService<GetMethod> completionService = mget.execute(urls, requestTimeoutMs);
int success = 0;
int errors = 0;
int timeouts = 0;
for (int i = 0; i < urls.size(); i++) {
GetMethod getMethod = null;
try {
getMethod = completionService.take().get();
if (getMethod.getStatusCode() >= 300) {
++errors;
Assert.assertEquals(getMethod.getResponseBodyAsString(), ERROR_MSG);
} else {
++success;
Assert.assertEquals(getMethod.getResponseBodyAsString(), SUCCESS_MSG);
}
} catch (InterruptedException e) {
LOGGER.error("Interrupted", e);
++errors;
} catch (ExecutionException e) {
if (Throwables.getRootCause(e) instanceof SocketTimeoutException) {
LOGGER.debug("Timeout");
++timeouts;
} else {
LOGGER.error("Error", e);
++errors;
}
} catch (IOException e) {
++errors;
}
}
Assert.assertEquals(2, success);
Assert.assertEquals(1, errors);
Assert.assertEquals(1, timeouts);
}
use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project opennms by OpenNMS.
the class GoogleGeocoderService method ensureInitialized.
public void ensureInitialized() throws GeocoderException {
if (m_geocoder == null) {
final HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
if (notEmpty(m_clientId) && notEmpty(m_clientKey)) {
try {
LOG.info("Initializing Google Geocoder using Client ID and Key.");
m_geocoder = new AdvancedGeoCoder(httpClient, m_clientId, m_clientKey);
} catch (final InvalidKeyException e) {
throw new GeocoderException("Unable to initialize Google Geocoder.", e);
}
}
if (m_geocoder == null) {
LOG.info("Initializing Google Geocoder using default configuration.");
m_geocoder = new AdvancedGeoCoder(httpClient);
}
/* Configure proxying, if necessary... */
final String httpProxyHost = System.getProperty("http.proxyHost");
final Integer httpProxyPort = Integer.getInteger("http.proxyPort");
if (httpProxyHost != null && httpProxyPort != null) {
LOG.info("Proxy configuration found, using {}:{} as HTTP proxy.", httpProxyHost, httpProxyPort);
httpClient.getHostConfiguration().setProxy(httpProxyHost, httpProxyPort);
} else {
LOG.info("No proxy configuration found.");
}
/* Limit retries... */
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, true));
httpClient.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, timeout);
LOG.info("Google Geocoder initialized.");
}
}
use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project camel by apache.
the class WeatherComponent method createHttpClient.
private HttpClient createHttpClient(WeatherConfiguration configuration) {
HttpConnectionManager connectionManager = configuration.getHttpConnectionManager();
if (connectionManager == null) {
connectionManager = new MultiThreadedHttpConnectionManager();
}
HttpClient httpClient = new HttpClient(connectionManager);
if (configuration.getProxyHost() != null && configuration.getProxyPort() != null) {
httpClient.getHostConfiguration().setProxy(configuration.getProxyHost(), configuration.getProxyPort());
}
if (configuration.getProxyAuthUsername() != null && configuration.getProxyAuthMethod() == null) {
throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
}
CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
if (configuration.getProxyAuthMethod() != null) {
configureProxyAuth(configurer, configuration.getProxyAuthMethod(), configuration.getProxyAuthUsername(), configuration.getProxyAuthPassword(), configuration.getProxyAuthDomain(), configuration.getProxyAuthHost());
}
configurer.configureHttpClient(httpClient);
return httpClient;
}
use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project camel by apache.
the class GeoCoderEndpoint method createGeocoder.
Geocoder createGeocoder() throws InvalidKeyException {
HttpConnectionManager connectionManager = this.httpConnectionManager;
if (connectionManager == null) {
connectionManager = new MultiThreadedHttpConnectionManager();
}
HttpClient httpClient = new HttpClient(connectionManager);
if (proxyHost != null && proxyPort != null) {
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
}
// validate that if proxy auth username is given then the proxy auth method is also provided
if (proxyAuthUsername != null && proxyAuthMethod == null) {
throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication");
}
CompositeHttpConfigurer configurer = new CompositeHttpConfigurer();
if (proxyAuthMethod != null) {
configureProxyAuth(configurer, proxyAuthMethod, proxyAuthUsername, proxyAuthPassword, proxyAuthDomain, proxyAuthHost);
}
if (httpClientConfigurer != null) {
configurer.addConfigurer(httpClientConfigurer);
}
configurer.configureHttpClient(httpClient);
Geocoder geocoder;
if (clientId != null) {
geocoder = new AdvancedGeoCoder(httpClient, clientId, clientKey);
} else {
geocoder = new AdvancedGeoCoder(httpClient);
}
return geocoder;
}
use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project camel by apache.
the class HttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
String addressUri = "http://" + remaining;
if (uri.startsWith("https:")) {
addressUri = "https://" + remaining;
}
Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
// must extract well known parameters before we create the endpoint
HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
// http client can be configured from URI options
HttpClientParams clientParams = new HttpClientParams();
Map<String, Object> httpClientOptions = IntrospectionSupport.extractProperties(parameters, "httpClient.");
IntrospectionSupport.setProperties(clientParams, httpClientOptions);
// validate that we could resolve all httpClient. parameters as this component is lenient
validateParameters(uri, httpClientOptions, null);
// http client can be configured from URI options
HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
// setup the httpConnectionManagerParams
Map<String, Object> httpConnectionManagerOptions = IntrospectionSupport.extractProperties(parameters, "httpConnectionManager.");
IntrospectionSupport.setProperties(connectionManagerParams, httpConnectionManagerOptions);
// validate that we could resolve all httpConnectionManager. parameters as this component is lenient
validateParameters(uri, httpConnectionManagerOptions, null);
// make sure the component httpConnectionManager is take effect
HttpConnectionManager thisHttpConnectionManager = httpConnectionManager;
if (thisHttpConnectionManager == null) {
// only set the params on the new created http connection manager
thisHttpConnectionManager = new MultiThreadedHttpConnectionManager();
thisHttpConnectionManager.setParams(connectionManagerParams);
}
// create the configurer to use for this endpoint (authMethods contains the used methods created by the configurer)
final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters);
// create the endpoint and connectionManagerParams already be set
HttpEndpoint endpoint = createHttpEndpoint(endpointUri.toString(), this, clientParams, thisHttpConnectionManager, configurer);
// configure the endpoint with the common configuration from the component
if (getHttpConfiguration() != null) {
Map<String, Object> properties = new HashMap<>();
IntrospectionSupport.getProperties(getHttpConfiguration(), properties, null);
setProperties(endpoint, properties);
}
if (headerFilterStrategy != null) {
endpoint.setHeaderFilterStrategy(headerFilterStrategy);
} else {
setEndpointHeaderFilterStrategy(endpoint);
}
if (urlRewrite != null) {
// let CamelContext deal with the lifecycle of the url rewrite
// this ensures its being shutdown when Camel shutdown etc.
getCamelContext().addService(urlRewrite);
endpoint.setUrlRewrite(urlRewrite);
}
// prefer to use endpoint configured over component configured
if (binding == null) {
// fallback to component configured
binding = getHttpBinding();
}
if (binding != null) {
endpoint.setBinding(binding);
}
setProperties(endpoint, parameters);
// restructure uri to be based on the parameters left as we dont want to include the Camel internal options
URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);
// validate http uri that end-user did not duplicate the http part that can be a common error
String part = httpUri.getSchemeSpecificPart();
if (part != null) {
part = part.toLowerCase();
if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://") || part.startsWith("//https://")) {
throw new ResolveEndpointFailedException(uri, "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
}
}
endpoint.setHttpUri(httpUri);
endpoint.setHttpClientOptions(httpClientOptions);
return endpoint;
}
Aggregations