Hi,
I need to make some "crash-tests" on web application, so I found the
example in CVS - MultiThreadedExample.java
But the problem is, that the request lasts much longer then in reality
and method.getResponseBody() returns completely wrong data - 18MB for
600kb file.
Here is a code. Sure, I'm doing something wrong, but what?
public class PhpMultiThreadTester {
public static final java.io.PrintStream OUT = System.out;
public static void main(String[] args) {
final Logger LOG = Logger.getLogger(PhpMultiThreadTester.class);
System.out.println("Started...");
// Create an HttpClient with the MultiThreadedHttpConnectionManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
HttpClient httpClient = new HttpClient(new
MultiThreadedHttpConnectionManager());
// Set the default host/protocol for the methods to connect to.
// This value will only be used if the methods are not given an absolute
URI
//httpClient.getHostConfiguration().setHost("jakarta.apache.org", 80,
"http");
httpClient.getState().setCredentials(null,null,new NTCredentials("user",
"password", "http://192.168.0.110/ags_dev__gja/index.php", "key-work"));
// create an array of URIs to perform GETs on
String[] urisToGet = {
"http://192.168.0.110/ags_dev__gja/index.php?a=test&b=result&searchPacka
geTypeKey=58" };
// create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = 0; i < threads.length; i++) {
GetMethod get = new GetMethod(urisToGet[i]);
get.setFollowRedirects(false);
threads[i] = new GetThread(httpClient, get, i + 1);
}
// start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
}
}
/**
* A thread that performs a GET.
*/
static class GetThread extends Thread {
private HttpClient httpClient;
private GetMethod method;
private int id;
public GetThread(HttpClient httpClient, GetMethod method, int id) {
this.httpClient = httpClient;
this.method = method;
this.id = id;
}
/**
* Executes the GetMethod and prints some status information.
*/
public void run() {
try {
long startTime = System.currentTimeMillis();
System.out.println(id + " - about to get something from " +
method.getURI());
// execute the method
httpClient.executeMethod(method);
System.out.println(id + " - get executed");
// get the response body as an array of bytes
//byte[] bytes = method.getResponseBody();
//System.out.println(id + " - " + bytes.length + " bytes read");
//System.out.println("Response body: " +
method.getResponseBodyAsString());
long endTime = System.currentTimeMillis();
System.out.println(
"********Request performed in "
+ ((endTime - startTime) / 1000 + "," + ((endTime - startTime) % 1000) /
100)
+ " seconds**********");
} catch (Exception e) {
System.out.println(id + " - error: " + e);
} finally {
// always release the connection after we're done
method.releaseConnection();
System.out.println(id + " - connection released");
}
}
}
}
Thank you