hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <k...@instabit.com>
Subject Using HTTPClient through Servlet, threading and performance
Date Wed, 26 Oct 2005 19:31:38 GMT

I'm using HTTPClient in a servlet which essentially is working as a proxy routing
requests to other providers and communicating results back to caller.  This
servlet needs to be as efficient as possible due to potential heavy traffic.

I've been referencing the Guide on Threading and Optimization.
http://jakarta.apache.org/commons/httpclient/threading.html
http://jakarta.apache.org/commons/httpclient/performance.html

I'm wondering if it appropriate to use th MultiThreadedHttpConnectionManager for
my use.  I was considering establishing the connection manager and HttpClient in
my servlet init() method.


public class MyRouter extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
    private HttpClient client = null;
    MultiThreadedHttpConnectionManager connectionManager = null;

    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        
        connectionManager  = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        params.setDefaultMaxConnectionsPerHost(1000);
        params.setMaxTotalConnections(5000);
        connectionManager.setParams(params);
              client = new HttpClient(connectionManager);
    }



then in my doGet() or doPost() I would use the client created in the init()

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {                        
    PostMethod postMethod = null;
    try{
        HostConfiguration hostConfiguration = new HostConfiguration();
        hostConfiguration.setHost("www.google.com");

        postMethod = new PostMethod("/search");

        //construct a new InputStreamRequestEntity to pass incoming inputstream
to target host
        InputStreamRequestEntity isre = new InputStreamRequestEntity(new
BufferedInputStream(bais));
        postMethod.setRequestEntity(isre);
        
        client.executeMethod(hostConfiguration, postMethod);
        
        BufferedInputStream bis = new
BufferedInputStream(postMethod.getResponseBodyAsStream());
        ...
    }catch(Exception e){
        //log.error(e);            
        e.printStackTrace();
    }finally{
        if (postMethod != null) postMethod.releaseConnection();    
    }
}

looks like I may also want to ensure all connections are closed when servlet is
taken out of service

public void destroy(){
    connectionManager .shutdown();
}


I'm sure there are problems with the above code and the entire approach may be wrong.

I want to ensure that there are no limits on the number of connections to various
hosts, failiing/blocking requests because there are already x connections to the
host is not acceptable in this situation.

I should mention for this servlet router there is a known number of destinations
or hosts that it will be communicating with.  This may allow for a change in
design where a client is configured and preserved once established.  Or maybe
cache HostConfiguration objects for each destination once created.

Any input would be greatly appreciated.

Thanks,
Kyle





---------------------------------------------------------------------
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