Skip to content

Instantly share code, notes, and snippets.

@maximveksler
Created February 16, 2011 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximveksler/829061 to your computer and use it in GitHub Desktop.
Save maximveksler/829061 to your computer and use it in GitHub Desktop.
Trying to debug the problems with my code and ThreadSafeClientConnManager
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>829061</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>
</projectDescription>
import java.io.IOException;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.cliffc.high_scale_lib.Counter;
public class HTTPClientTest {
private Counter __counter = new Counter();
static HttpParams HTTP_PARAMS = new BasicHttpParams();
static HttpContext HTTP_CONTEXT = new BasicHttpContext();
static {
HTTP_PARAMS.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
HTTP_PARAMS.setParameter(CoreConnectionPNames.SO_TIMEOUT, 400);
HTTP_PARAMS.setParameter(CoreConnectionPNames.SO_REUSEADDR, Boolean.TRUE);
HTTP_PARAMS.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 400);
HTTP_PARAMS.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, Boolean.FALSE);
}
public static void main(String[] args) throws IllegalStateException, IOException, InterruptedException {
new HTTPClientTest().stress();
}
public void stress() throws InterruptedException {
System.out.println(new Date() + ": Starting thread execution");
ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager();
threadSafeClientConnManager.setDefaultMaxPerRoute(1);
threadSafeClientConnManager.setMaxTotal(1);
String httpGetURI = "http://maxim-lp/index.html";
GetThread[] threads = new GetThread[] {
new GetThread(threadSafeClientConnManager, httpGetURI),
new GetThread(threadSafeClientConnManager, httpGetURI),
new GetThread(threadSafeClientConnManager, httpGetURI),
new GetThread(threadSafeClientConnManager, httpGetURI)
};
for(int i = 0; i < threads.length; i++) {
threads[i].start();
}
for(int i = 0; i < threads.length; i++) {
threads[i].join();
}
}
class GetThread extends Thread {
private final HttpContext httpContext;
private final String uri;
private HttpClient httpClient;
private HttpGet httpGet;
long nOfFailuersForThisThread = 0;
public GetThread(ClientConnectionManager clientConnectionManager, String uri) {
this.httpClient = new DefaultHttpClient(clientConnectionManager, HTTP_PARAMS);
this.httpContext = new BasicHttpContext();
this.uri = uri;
}
public void run() {
while(true) {
try {
if(httpGet == null) {
System.out.println(new Date() + " " + Thread.currentThread().getName() + ": new HttpGet");
this.httpGet = new HttpGet(uri + "?tid=" + Thread.currentThread().getName() + "&failures=" + nOfFailuersForThisThread);
}
HttpResponse response = httpClient.execute(httpGet, httpContext);
HttpEntity entity = response.getEntity();
if(entity != null) {
EntityUtils.consume(entity);
}
__counter.increment();
} catch (Exception e) {
nOfFailuersForThisThread++;
System.out.println(new Date() + " " + Thread.currentThread().getName() + ": thread failure #" + nOfFailuersForThisThread + ", System did a total of " + __counter.get() + " GET requests");
System.out.println(new Date() + " " + Thread.currentThread().getName() + ": Exception");
e.printStackTrace();
httpGet.abort();
System.out.println(new Date() + " " + Thread.currentThread().getName() + ": Abort");
// Create new HttpGet for error recovery
httpGet = null;
if(nOfFailuersForThisThread > 3)
System.exit(1);
}
}
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.vekslers</groupId>
<artifactId>829061</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>829061</name>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>com.github.stephenc.high-scale-lib</groupId>
<artifactId>high-scale-lib</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment