hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Larsen" <clarsen...@gmail.com>
Subject PoolingClientConnectionManager Using Non-standard HTTP Port Question
Date Mon, 04 Mar 2013 19:50:59 GMT
Hello, I'm trying to setup a pool of HTTP clients for interacting with an
Elastic Search server using their HTTP API. I'll have many threads making
simultaneous requests for indexing or searching data so I figured the
connection manager was the way to go. Working off the tutorial at
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html I
setup a pool and some threads all pulling data from the same host to verify
that setDefaultMaxPerRoute() worked correctly (code pasted at the end). It
looked god and seemed to spread the load nicely returning:

 

Starting thread [0]

Starting thread [1]

Starting thread [2]

Starting thread [3]

[0] HTTP Status [200]

[3] HTTP Status [200]

[1] HTTP Status [200]

[2] HTTP Status [200]

[0] HTTP Status [200]

[1] HTTP Status [200]

[3] HTTP Status [200]

[2] HTTP Status [200]

[1] HTTP Status [200]

[0] HTTP Status [200]

[3] HTTP Status [200]

[2] HTTP Status [200]

[3] HTTP Status [200]

[0] HTTP Status [200]

[1] HTTP Status [200]

[2] HTTP Status [200]

[3] HTTP Status [200]

[1] HTTP Status [200]

[0] HTTP Status [200]

[2] HTTP Status [200]

[1] HTTP Status [200]

 

But by default Elastic Search runs on port 9200. When I tried the same code
but with the non-standard port, it looks like pooling doesn't function
properly and only one thread handled requests:

 

Starting thread [1]

Starting thread [0]

Starting thread [2]

Starting thread [3]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

[0] HTTP Status [200]

 

So am I doing something goofy or does the pool not handle non-standard ports
correctly? I'm using 4.2.3. Thanks!

 

 

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.conn.scheme.PlainSocketFactory;

import org.apache.http.conn.scheme.Scheme;

import org.apache.http.conn.scheme.SchemeRegistry;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.impl.conn.PoolingClientConnectionManager;

import org.apache.http.protocol.BasicHttpContext;

import org.apache.http.protocol.HttpContext;

import org.apache.http.util.EntityUtils;

 

public class HttpCTest {

 

  public void run(){

    SchemeRegistry schemeRegistry = new SchemeRegistry();

    schemeRegistry.register(

            new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

//              new Scheme("http", 9200,
PlainSocketFactory.getSocketFactory()));

 

    PoolingClientConnectionManager cm = new
PoolingClientConnectionManager(schemeRegistry);

    cm.setDefaultMaxPerRoute(10);

    cm.setMaxTotal(20);

    HttpClient httpClient = new DefaultHttpClient(cm);

 

    // URIs to perform GETs on

    String[] urisToGet = {

        "http://www.google.com/",

        "http://www.google.com/",

        "http://www.google.com/",

        "http://www.google.com/"

    };

    

//      String[] urisToGet = {

//        "http://elasticsearchhost:9200/",

//        "http://elasticsearchhost:9200/",

//        "http://elasticsearchhost:9200/",

//        "http://elasticsearchhost:9200/"

//      };

 

    // create a thread for each URI

    GetThread[] threads = new GetThread[urisToGet.length];

    for (int i = 0; i < threads.length; i++) {

        HttpGet httpget = new HttpGet(urisToGet[i]);

        threads[i] = new GetThread(httpClient, httpget, i);

    }

 

    // start the threads

    for (int j = 0; j < threads.length; j++) {

        threads[j].start();

    }

 

    // join the threads

    for (int j = 0; j < threads.length; j++) {

        try {

          threads[j].join();

        } catch (InterruptedException e) {

          // TODO Auto-generated catch block

          e.printStackTrace();

        }

    }

  }

  

  static class GetThread extends Thread {

    private final HttpClient httpClient;

    private final HttpContext context;

    private final HttpGet httpget;

    private final int thread_id;

    

    public GetThread(HttpClient httpClient, HttpGet httpget, int thread_id)
{

        this.httpClient = httpClient;

        this.context = new BasicHttpContext();

        this.httpget = httpget;

        this.thread_id = thread_id;

    }

    

    @Override

    public void run() {

        try {

          System.out.println("Starting thread [" + this.thread_id + "]");

          for (int i=0; i<100; i++){

            HttpResponse response = this.httpClient.execute(this.httpget,
this.context);

            HttpEntity entity = response.getEntity();

            if (entity != null) {

                // do something useful with the entity

              System.out.println(String.format("[%d] HTTP Status [%d]", 

                  this.thread_id,
response.getStatusLine().getStatusCode()));

            }

            // ensure the connection gets released to the manager

            EntityUtils.consume(entity);

          }

        } catch (Exception ex) {

            this.httpget.abort();

        }

     }

    }

}


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message