Http client connections not reused

Hi,
I need to invoke same url freq, So I want to use connection pooling.
My code for invoke same url frequently as follows.
Its working, but connections not reused, every time new connection created(I invoke http url only).
Any suggestions ?

my java code

private static final int CONNECT_TIMEOUT_SECONDS = 10;

private static final int SOCKET_TIMEOUT_SECONDS = 30;

private static final int MAX_CONNS_PER_ROUTE = 500;

private static final int MAX_TOTAL_CONNS = 3000;

private static HttpProcessor httpproc;

private static HttpRequestExecutor httpexecutor;

private static BasicConnPool pool;

public static void init() throws Exception {
    httpproc = HttpProcessorBuilder.create().add(new RequestContent()).add(new RequestTargetHost()).add(new RequestConnControl()).add(new RequestUserAgent("ZMapsAppServer/1.1")).add(new RequestExpectContinue(true)).build(); 
    httpexecutor = new HttpRequestExecutor();

    BasicConnFactory bfact = new BasicConnFactory(CONNECT_TIMEOUT_SECONDS * 1000, SocketConfig.custom().setSoTimeout(SOCKET_TIMEOUT_SECONDS * 1000).build(), ConnectionConfig.DEFAULT);
    pool = new BasicConnPool(bfact);
    pool.closeIdle(1, TimeUnit.DAYS);
    pool.setDefaultMaxPerRoute(MAX_CONNS_PER_ROUTE);
    pool.setMaxTotal(MAX_TOTAL_CONNS);
}

public static String invokeURL(String url) throws Exception {
    String result = null;
    HttpHost host = null;
    try {
        URIBuilder urlbuild = new URIBuilder(url);
        host = new HttpHost(urlbuild.getHost(), urlbuild.getPort());
        Future<BasicPoolEntry> future = pool.lease(host, null);

        boolean reusable = false;
        BasicPoolEntry entry = future.get();
        try {
            HttpClientConnection conn = entry.getConnection();
            HttpCoreContext coreContext = HttpCoreContext.create();
            coreContext.setTargetHost(host);
            HttpGet httpget = new HttpGet(url);
            httpexecutor.preProcess(httpget, httpproc, coreContext);
            HttpResponse response = httpexecutor.execute(httpget, conn, coreContext);
            httpexecutor.postProcess(response, httpproc, coreContext);
            result = EntityUtils.toString(response.getEntity());
            reusable = DefaultConnectionReuseStrategy.INSTANCE.keepAlive(response, coreContext);
        } catch (IOException ex) {
            throw ex;
        } catch (HttpException ex) {
            throw ex;
        } finally {
            if (reusable) {
                LOGGER.info("Connection kept alive...");
            }
            pool.release(entry, reusable);
        }
    } catch (Exception ex) {
        LOGGER.warning("Request to " + host + " failed: " + ex.getMessage());
    }
    return result;
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.