Kadlabalu, Hareesh wrote:
Hi all,

This is regarding some tcp connections left with CLOSE_WAIT state when using 
HttpClient 3.x.

I have done some googling for discussions around this and looked through the 
library code around this area. Just want to validate what seems to be the 
consensus:

1. When a program makes multiple http requests (hundreds of them), each one using a new instance of HttpClient, it is possible for some of the sockets to be left with CLOSE_WAIT state. This happens even when the HttoMethod is properly closed after use each time. 2. This happens because the HttpMethod's releaseConnection() does not close the underlying HttpConnection instance as this is available for other requests to reuse.


Correct.


 Solutions:

1.      Use MultiThreadedHttpConnectionManager and shut it down after use. This 
will close all connections. I have tested it and it does work.
2. If for whatever reason the first solution cannot be used, another way is to close the connection explicitly using
HttpClient.getHttpConnectionManager().getConnection(hostconfig).close();


Correct, but I would recommend using HttpConnectionManager#closeIdleConnections(0) instead, if you want to close all idle connections without shutting down the connection manager

http://jakarta.apache.org/httpcomponents/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpConnectionManager.html#closeIdleConnections(long)


This works too. But is there a problem doing this? (I know all the reuse of 
connections is not possible but is it harmful to use this?)

Are these conclusions right?

Yes, they are.

Cheers

Oleg


Attached below is a test program that produces CLOSE_WAIT when host is running Weblogic10MP1 on Red Hat AS 4 U5. I have also seen this happen on JBoss. This does not happen if I the host is tomcat 6, I am not sure if there is a correlation.

Your advice is very much appreciated

Thanks

-hareesh

---------------Test code

public final class TestHttpClient

{

    static String url;

    public static void main( String [] args )

    {

        url = args[ 0 ];

        int counter = args.length > 1 ? Integer.parseInt( args[ 1 ] ) : 100;

        long waitTime = args.length > 2 ? Long.parseLong( args[ 2 ] ) : 0;

int batch = args.length > 3 ? Integer.parseInt( args[ 3 ] ) : 1;
        try

        {

            for( int x = 0; x < batch; x++ )

            {

                for( int j = 0; j < counter; j++ )

                {

                    _postData( x, j );

                    if( waitTime > 0 )

                        Thread.sleep( waitTime );

                }

            }

            _waitForEver();

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }

    private static void _postData( int batch, int counter ) throws IOException

    {

        HttpClient client = new HttpClient();

        PostMethod postMethod = new PostMethod( url );

        StringPart sPart = new StringPart( "ftcmd", "pingdb", "UTF-8" );

        RequestEntity entity = new MultipartRequestEntity( new Part[] {sPart}, 
postMethod.getParams() );

        postMethod.setRequestEntity( entity );

        try

        {

            client.executeMethod( postMethod );

        }

        finally

        {

            postMethod.releaseConnection();

            System.out.println("batch " + batch + " iteration " + counter + " 
done");

        }

    }

    private static void _waitForEver() throws InterruptedException

    {

        while( true )

        {

            Thread.sleep( 10000 );

            System.out.println("Still alive..");

        }

    }

}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to