Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Resolved SPOTLINK-48.

Added ThreadSafeClientConnManager to the HttpClient.
  • Loading branch information...
commit 90a2cf1a1ebd9335c4cdfd673b7e39901c1e7720 1 parent 56e21df
@mnarrell mnarrell authored
View
15 src/main/java/com/thistech/spotlink/engine/HttpTrackingEngine.java
@@ -32,6 +32,7 @@
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
+import java.io.IOException;
import java.util.Properties;
import java.util.UUID;
@@ -81,6 +82,7 @@ private void trackEvent(PlacementStatusEventType event) {
}
HttpGet get = new HttpGet(url);
+ HttpEntity entity = null;
try {
log.info(String.format("Executing ClickTracking GET on: %s", get.getURI()));
HttpResponse response = this.httpClient.execute(get);
@@ -89,12 +91,21 @@ private void trackEvent(PlacementStatusEventType event) {
if (HttpStatus.SC_OK != statusCode) {
log.error(String.format("HTTP GET failed on %s with HttpStatus %s", url, statusCode));
}
- HttpEntity entity = response.getEntity();
- entity.consumeContent();
+ entity = response.getEntity();
}
catch (Exception e) {
log.error(String.format("HTTP GET on %s failed. Exception:", url), e);
}
+ finally {
+ try {
+ if (entity != null) {
+ entity.consumeContent();
+ }
+ }
+ catch (IOException e) {
+ // do nothing;
+ }
+ }
}
}
}
View
59 src/main/java/com/thistech/spotlink/util/HttpClientFactory.java
@@ -19,27 +19,27 @@
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
+import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRoute;
import org.apache.http.conn.routing.HttpRoute;
+import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Properties;
public class HttpClientFactory {
- private static final Logger log = LoggerFactory.getLogger(HttpClientFactory.class);
@Resource(name = "com.thistech.spotlink.Properties")
private Properties properties = null;
public HttpClient newInstance() {
- HttpClient client = this.instantiateClass();
+ HttpClient client = new DefaultHttpClient();
HttpParams httpParams = client.getParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
@@ -47,61 +47,26 @@ public HttpClient newInstance() {
int timeout = Integer.parseInt(this.properties.getProperty("httpclient.timeout"));
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
- } // defaults to implementation
+ }
ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRoute() {
public int getMaxForRoute(HttpRoute route) {
- if (properties.containsKey("httpclient.conn-per-route")) {
- return Integer.parseInt(properties.getProperty("httpclient.conn-per-route"));
- } else {
- return 5;
- }
+ return Integer.parseInt(properties.getProperty("httpclient.conn-per-route", "5"));
}
});
- int totalConnections = 100;
- if (this.properties.containsKey("httpclient.total-connections")) {
- totalConnections = Integer.parseInt(this.properties.getProperty("httpclient.total-connections"));
- }
+ int totalConnections = Integer.parseInt(this.properties.getProperty("httpclient.total-connections", "100"));
ConnManagerParams.setMaxTotalConnections(httpParams, totalConnections);
- String userAgent = "Mozilla/5.0";
- if (this.properties.containsKey("httpclient.user-agent")) {
- userAgent = this.properties.getProperty("httpclient.user-agent");
- }
+ String userAgent = this.properties.getProperty("httpclient.user-agent", "Mozilla/5.0");
HttpProtocolParams.setUserAgent(httpParams, userAgent);
- String charset = "UTF-8";
- if (this.properties.containsKey("httpclient.content-charset")) {
- charset = this.properties.getProperty("httpclient.content-charset");
- }
+ String charset = this.properties.getProperty("httpclient.content-charset", "UTF-8");
HttpProtocolParams.setContentCharset(httpParams, charset);
- return client;
- }
-
- private HttpClient instantiateClass() {
- HttpClient client;
- if (this.properties.containsKey("httpclient.classname")) {
- try {
- @SuppressWarnings("unchecked")
- Class<HttpClient> clazz =
- (Class<HttpClient>) Class.forName(this.properties.getProperty("httpclient.classname"));
- Class[] classParams = null;
- Object[] objectParams = null;
- client = clazz.getConstructor(classParams).newInstance(objectParams);
- } catch (Exception e) {
- String warnMsg = String.format("Unable to create HttpClient from %s: %s. " +
- "Defaulting to org.apache.http.impl.client.DefaultHttpClient",
- this.properties.getProperty("httpclient.classname"),
- e.getLocalizedMessage());
- log.warn(warnMsg, e);
- client = new DefaultHttpClient();
- }
-
- } else {
- client = new DefaultHttpClient();
- }
+ ClientConnectionManager mgr = client.getConnectionManager();
+ SchemeRegistry schemeRegistry = mgr.getSchemeRegistry();
+ client = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
return client;
}
}
View
1  src/main/resources/spotlink-defaults.properties
@@ -49,7 +49,6 @@ trackingEventsDao=com.thistech.spotlink.persistence.CachingTrackingEventsDao
cxf.service.timeout=1000
# HTTP client configuration
-#httpclient.classname=org.apache.http.impl.client.DefaultHttpClient
httpclient.timeout=1000
#httpclient.conn-per-route=5
#httpclient.total-connections100
View
20 src/test/java/com/thistech/spotlink/util/HttpClientFactoryTest.java
@@ -18,11 +18,14 @@
*/
import com.thistech.spotlink.AbstractSpotlinkTest;
+import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;
import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.fail;
@ContextConfiguration
@@ -36,4 +39,21 @@ public void testNewInstance() throws Exception {
HttpClient client = httpClientFactory.newInstance();
assertNotNull(client);
}
+
+ @Test
+ public void testThreadedConnectionManager() throws Exception {
+ HttpClientFactory httpClientFactory =
+ (HttpClientFactory) this.applicationContext.getBean("httpClientFactory");
+ final HttpClient httpClient = httpClientFactory.newInstance();
+ for (int i = 0; i < 5; i++) {
+ try {
+ HttpResponse response = httpClient.execute(new HttpGet("http://www.thistech.com"));
+ response.getEntity().consumeContent();
+ }
+ catch (Exception e) {
+ fail(e.getMessage(), e);
+ }
+ }
+ System.out.println("done");
+ }
}
View
3  src/test/resources/com/thistech/spotlink/util/test.properties
@@ -16,8 +16,7 @@
#
# HTTP client configuration
-httpclient.classname=org.apache.http.impl.client.ContentEncodingHttpClient
-httpclient.timeout=2000
+#httpclient.timeout=2000
httpclient.conn-per-route=3
httpclient.total-connections=50
httpclient.user-agent=Bond
Please sign in to comment.
Something went wrong with that request. Please try again.