I am trying to use HttpClient with threads and while it works, it is
only sending one request at a time such that having multiple threads to
make my requests does not get any advantage from having multiple
threads.
I am using the same HttpClient instance among all of the threads, am
using the MultiThreadedHttpConnectionManager when creating the instance
and have set setDefaultMaxConnectionsPerHost to 20 and
setMaxTotalConnections to 30 and I still cannot get more than one
connection at the same time.
I am sending files over http. I am also setting some cookies (I do the
cookie handling myself) when doing the post and I do a releaseConnection
after I am done with each request.
I have been looking for examples and incorperated things I have seen but
none of them have many any change in being able to actually get multiple
requests to be happening at the same time.
here are the relavant parts of code (err is a window for debug messages
that is passed in):
public class SessionHttpRequest {
private static HttpClient client;
private static HttpConnectionManagerParams params = new
HttpConnectionManagerParams();
private static HttpConnectionManager connectionManager = new
MultiThreadedHttpConnectionManager();
static {
// Configure params for the Connection Manager
params.setDefaultMaxConnectionsPerHost( 20 );
params.setMaxTotalConnections( 30 );
// This next line may not be necessary since we specified
default 2 lines ago, but here it is anyway
params.setMaxConnectionsPerHost(
HostConfiguration.ANY_HOST_CONFIGURATION, 20 );
// Set up the connection manager
connectionManager.setParams( params );
// Finally set up the static multithreaded HttpClient
client = new HttpClient( connectionManager );
}
public SessionHttpRequest(ErrorWindow err) {
this.err = err;
}
public SessionHttpRequest(ErrorWindow err, String host, int port,
String protocol)
{
this(err);
client.getHostConfiguration().setHost(host, port, protocol);
//new
HttpConnectionManagerParams().setMaxConnectionsPerHost(client.getHostCon
figuration(), 20);
}
// each thread calls this method to get a clone. client is static, so
it will come for "free"
// I just need the session information
public SessionHttpRequest clone()
{
SessionHttpRequest request = new SessionHttpRequest(err);
request.setSaveSession(saveSession);
request.setSession(session);
return request;
}
public void setSession(HashMap<String,String> session)
{
this.session = (HashMap<String,String>)session.clone();
}
public void setSaveSession(boolean saveSession) {
this.saveSession = saveSession;
}
// This is the method that the threads call to do the file upload
public String post(boolean safe, String url, boolean clearParameters,
String requestMethod, int timeout) throws IOException
{
if (timeout > 0)
client.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new
Integer(timeout));
HttpMethodBase method;
if (requestMethod.equals("POST"))
method = new PostMethod(url);
else
method = new GetMethod(url);
if (saveSession)
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
// set all of the saved cookies
if (saveSession)
{
String setCookie = new String();
String key;
String value;
for (Iterator it=session.entrySet().iterator(); it.hasNext(); )
{
Map.Entry entry = (Map.Entry)it.next();
key = (String)entry.getKey();
value = (String)entry.getValue();
if (!setCookie.equals(""))
setCookie += " ";
setCookie += key + "=" + value + ";";
}
method.setRequestHeader("Cookie", setCookie);
if (debug)
err.println("sending cookie: " + setCookie);
}
if (requestMethod.equals("POST"))
{
String key;
Object value;
ArrayList<Part> parts = new ArrayList<Part>();
for (Iterator it=parameters.entrySet().iterator(); it.hasNext();
)
{
Map.Entry entry = (Map.Entry)it.next();
key = (String)entry.getKey();
value = entry.getValue();
if (value instanceof File)
parts.add(new FilePart(key , (File)value));
else
parts.add(new StringPart(key, value.toString()));
}
Part[] dummy = {};
((PostMethod)method).setRequestEntity(new
MultipartRequestEntity(parts.toArray(dummy), method.getParams()) );
}
String result="";
// make the request
int responseCode;
try
{
responseCode = client.executeMethod(method);
// read all just takes the stream and turns it into a string.
// I was getting warnings when trying to use
getResponseBodyAsString
result =
StaticMethods.readAll(method.getResponseBodyAsStream());
String location = "";
//catch all of the cookies being set now
//TODO need to turn cookies into a map and pay attention to the
name if things are being overwritten
if (saveSession)
{
String header;
String cookie;
int cookieEnd;
int cookieMiddle;
String cookieName;
String cookieValue;
Header[] headers = method.getResponseHeaders("Set-Cookie");
for(int x=0; x < headers.length ; x++)
{
cookie = headers[x].getValue();
cookieEnd = cookie.indexOf(";");
cookie = cookie.substring(0,cookieEnd);
cookieMiddle = cookie.indexOf("=");
cookieName = cookie.substring(0,cookieMiddle);
cookieValue = cookie.substring(cookieMiddle+1);
if (debug)
err.println("saving Cookie: " + cookieName + " = " +
cookieValue);
session.put(cookieName, cookieValue);
}
}
if (clearParameters) {
parameters.clear();
}
if ((responseCode == 302 || responseCode == 301) && location !=
"") {
location = url.substring(0, url.lastIndexOf("/")+1) +
method.getResponseHeader("Location").getValue();
if (debug)
err.println("redirecting to " + location);
result = post(safe, location, clearParameters, "GET");
}
}
catch (java.io.IOException e)
{
StaticMethods.prettyException(e, err);
}
finally
{
method.releaseConnection();
}
return result;
}
What am I doing wrong? Thanks.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]