Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:29:18+00:00 2026-06-10T20:29:18+00:00

I am using httpcomponents 4.1.2 and the ThreadSafeClientConnManager runs out of connections. There does

  • 0

I am using httpcomponents 4.1.2 and the ThreadSafeClientConnManager runs out of connections. There does not appear to be any way to release a connection unlike the older commons HttpClient. My goal is to have a number of concurrent threads doing HTTP POSTs to a URL. Anyone have an idea of how to ‘release’ the connections? Or should i be doing it a different way?

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


public class HttpThread implements Runnable
{
    private static ThreadSafeClientConnManager cm;
    private static HttpClient httpClient;

    static
    {
        cm = new ThreadSafeClientConnManager();
        cm.setDefaultMaxPerRoute( 10 );
        cm.setMaxTotal( 100 );
        httpClient = new DefaultHttpClient( cm );
    }

    private String id;
    private int iterations;


    public HttpThread( String id, int iterations )
    {
        this.id= id;
        this.iterations = iterations;
    }


    @Override
    public void run()
    {       
        long start = System.currentTimeMillis();

        for( int i = 0; i < this.iterations; i++ )
        {
            HttpPost post = new HttpPost( "http://127.0.0.1:7001/in/iServlet" );            

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add( new BasicNameValuePair( "p1", "0123456789" ) );
            nvps.add( new BasicNameValuePair( "p2", "7" ) );
            nvps.add( new BasicNameValuePair( "p3", "80027-48taPS4lQVu7q6RjEA40kg-1207276200-75-2-1-0" ) );
            nvps.add( new BasicNameValuePair( "p4", "1-30--1-2-_60--2-Y-RED,XL__2-30-5-3-A-" ) );

            try
            {
                post.setEntity( new UrlEncodedFormEntity( nvps, HTTP.UTF_8 ) );

                System.out.println("before post: " + i);
                HttpResponse response = httpClient.execute( post );
                System.out.println("after post: " + i);
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
            finally
            {

            }
        }

        long stop = System.currentTimeMillis();
        System.out.println( this.id + "  finished: " + (stop - start) + " ms" );        
    }
}

With my driver being:

public class ThreadTest
{
    private ExecutorService executor = Executors.newCachedThreadPool(); 

    protected void spawnThreads( int numberOfThreads, int iterations )
    {
        for( int i = 1; i <= numberOfThreads; i++ )
        {
            HttpThread thread = new HttpThread( "thread-" + i, 100 );
            executor.execute( thread );

            System.out.println( "Launching: thread-" + i + " for: " + iterations + " iterations" );
        }
    }


    public static void main(String[] args)
    {
        ThreadTest t= new ThreadTest();
        t.spawnThreads( 1, 100 );
    }

}

My output is then:

Launching: thread-1 for: 100 iterations
before post: 0
after post: 0
before post: 1
after post: 1
before post: 2
after post: 2
before post: 3
after post: 3
before post: 4
after post: 4
before post: 5
after post: 5
before post: 6
after post: 6
before post: 7
after post: 7
before post: 8
after post: 8
before post: 9
after post: 9
before post: 10
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:29:19+00:00Added an answer on June 10, 2026 at 8:29 pm

    You need to add the following code to the try block of your run method to free up the internal resources:

    Entity entity = response.getEntity();
    if(entity != null) entity.getContent().close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Microsoft SQL Server 2005, is there any way to see when a table
I'm using a connection created by ThreadSafeClientConnManager (Apache httpcomponents 4.1.1). The response is chunked
Using android 2.3.3, I have a background Service which has a socket connection. There's
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using SQL Server 2008 R2 we are looking for a way to select the
It seems that nginx does not support chunked requests well. But I'm trying to
Using jQuery, one can easily find out whether a particular element is visible using
I am using HttpClient 4.02 to create a connection via proxy (using the CONNECT
When using the Apache HttpComponents HttpClient library (4.0.2) I'm having a problem where the
I'm fetching a web page using the Apache httpcomponents Java library . After connecting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.