hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joan Balagueró <joan.balagu...@grupoventus.com>
Subject RE: PROBLEM WITH setMaxTotalConnections
Date Mon, 26 Mar 2007 13:39:38 GMT
Hello (again),

I write a small program to test the maxTotalConnections.

It's an application with a static httpClient (multithreaded). The main
program inits this variable and starts 20 threads that use it to connect to
our website. Again, I set the limit of active connections to 1. With these
conditions, all threads are able to connect to destination perfectly (I've
repeated the same with 200 threads).

I don't know what to do to limit the number of connections. I don't know if
I'm doing something wrong or if this is a bug of httpClient 3.1

Any suggestions will be much appreciated.

Thanks,

Joan.


The code is the following:

package com.vcfw.admin.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.util.concurrent.CountDownLatch;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;

public class testHttp
{
 public static HttpClient objHttp = null;
 public static CountDownLatch cdlStart = new CountDownLatch(1);
	 
 // Main.
public static void main(String[] args) 
{
 // The thread
 Runnable r = new Runnable() 
 {
  public void run()
  {
   try
   {
    // Wait until start...
    testHttp.cdlStart.await();
		
    // Send the POST request.
    PostMethod objPost = new PostMethod("http://www.grupoventus.com");
    testHttp.objHttp.executeMethod(objPost);
	 	
    // Read the response.
    BufferedInputStream bis = new
BufferedInputStream(objPost.getResponseBodyAsStream());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buffer = new byte[4098];
    int numBytes;

    while ((numBytes = bis.read(buffer)) >= 0) bos.write(buffer, 0,
numBytes);
    byte[] byteResponse = bos.toByteArray();
	   
    // Print the response.
    System.out.println(new String(byteResponse));

    // Close all
    objPost.releaseConnection();
    bis.close();
    bos.close();
   }
   catch (Exception e) { e.printStackTrace(); }
  }
 };
 
 // MAIN PROGRAM.
 try
 {
  // Init the static httpClient
  objHttp = new HttpClient(new MultiThreadedHttpConnectionManager());
  objHttp.getHttpConnectionManager().getParams().setMaxTotalConnections(1);
  objHttp.getHttpConnectionManager().getParams().setConnectionTimeout(2000);
  objHttp.getHttpConnectionManager().getParams().setSoTimeout(10000);
objHttp.getHttpConnectionManager().getParams().setStaleCheckingEnabled(true)
;
  objHttp.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

  // Start 20 threads that connect to "http://www.grupoventus.com".
  for (int i = 0; i < 20; i++)
  {
   new Thread(r).start();
  }

  // Start !!
  cdlStart.countDown();
 }
 catch (Exception e) { e.printStackTrace(); }
}

}



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message