/**
* Demonstrate Mule concurrency issues when using http endpoints.
*
* Just serve two text files containing the single character '1' and '2'
* from a web server.
*
* Test against the web server. See it OK
* Test against Mule. See it KO.
*/
package test;
import java.util.Vector;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
public class Test {
private static String PORT = "8080";
private static String URL1 = "
http://localhost:" + PORT + "/test/1.txt";
private static String URL2 = "
http://localhost:" + PORT + "/test/2.txt";
private static byte ONE = '0' + 1;
private static byte TWO = '0' + 2;
private static int NUMBER_OF_THREADS=100;
protected static Thread dotest(int i) {
return (new Thread() {
public void run() {
String url = (getThreadId() % 2 == 0) ? URL1 : URL2;
GetMethod get = new GetMethod(url);
HttpClient client = new HttpClient();
try {
int status = client.executeMethod(get);
if (status==HttpStatus.SC_OK){
byte b[] = new byte[1];
get.getResponseBodyAsStream().read(b, 0, 1);
if (URL1.equals(url) && b[0] != ONE) {
System.out.println("KO "+ URL1 + " thread: "+ getThreadId() + " byte: " + b[0]);
System.exit(-1);
} else if(URL2.equals(url) && b[0] != TWO) {
System.out.println("KO "+ URL2 + " thread: "+ getThreadId() + " byte: " + b[0]);
System.exit(-1);
}else {
System.out.println("OK " + getThreadId() + " url=" + url + " byte: " + b[0]);
}
} else {
System.out.println("HTTP Status " + status);
}
} catch(Exception e){
e.printStackTrace();
}
}
});
}
private static Vector<Thread> v=new Vector<Thread>();
public static void main(String [] args) throws Exception {
long startTime=System.currentTimeMillis();
for(int i=0;i<NUMBER_OF_THREADS;i++) {
Thread t=dotest(i);
t.start();
v.add(t);
}
for(int i=0;i<v.size();i++) {
Thread t=(Thread) v.get(i);
t.join();
}
System.out.println("Time: "+(System.currentTimeMillis()-startTime));
}
private static long getThreadId() {
return Thread.currentThread().getId();
}
}
Test Class