Example usage for org.apache.commons.httpclient CustomMultiThreadedHttpConnectionManager getConnectionsInPool

List of usage examples for org.apache.commons.httpclient CustomMultiThreadedHttpConnectionManager getConnectionsInPool

Introduction

In this page you can find the example usage for org.apache.commons.httpclient CustomMultiThreadedHttpConnectionManager getConnectionsInPool.

Prototype

public int getConnectionsInPool() 

Source Link

Document

Gets the total number of pooled connections.

Usage

From source file:org.sonatype.nexus.proxy.SimpleRemoteLeakTest.java

@Test
public void checkForConnectionPoolLeakWithGoodTransport() throws Exception {
    ProxyRepository repo1 = getRepositoryRegistry().getRepositoryWithFacet("repo1", ProxyRepository.class);
    ProxyRepository repo2 = getRepositoryRegistry().getRepositoryWithFacet("repo2", ProxyRepository.class);

    // this test is CommonsHttpclient3x dependent, it uses it's internals in assertions!
    // So run it only when that RRS is in use
    if (!(repo1.getRemoteStorage() instanceof CommonsHttpClientRemoteStorage)) {
        System.out.println("Test disabled, RRS implementation is not of class "
                + CommonsHttpClientRemoteStorage.class.getName() + " but "
                + repo1.getRemoteStorage().getClass().getName());

        return;/*  w  ww .j a  v a  2 s  . c  o m*/
    }

    // mangle one repos to have quasi different host, thus different HttpCommons HostConfig
    // but make it succeed! (127.0.0.1 is localhost, so will be able to connect)
    repo1.setRemoteUrl(getRepositoryRegistry().getRepositoryWithFacet("repo1", ProxyRepository.class)
            .getRemoteUrl().replace("localhost", "127.0.0.1"));
    repo1.commitChanges();

    ResourceStoreRequest req1 = new ResourceStoreRequest(
            "/repositories/repo1/activemq/activemq-core/1.2/activemq-core-1.2.jar", false);
    ResourceStoreRequest req2 = new ResourceStoreRequest(
            "/repositories/repo2/xstream/xstream/1.2.2/xstream-1.2.2.pom", false);

    for (int i = 0; i < 10; i++) {
        StorageItem item1 = getRootRouter().retrieveItem(req1);
        checkForFileAndMatchContents(item1);

        StorageItem item2 = getRootRouter().retrieveItem(req2);
        checkForFileAndMatchContents(item2);

        // to force refetch
        getRepositoryRegistry().getRepository(item1.getRepositoryId()).deleteItem(false,
                new ResourceStoreRequest(item1));

        getRepositoryRegistry().getRepository(item2.getRepositoryId()).deleteItem(false,
                new ResourceStoreRequest(item2));
    }

    // get the default context, since they used it
    RemoteStorageContext ctx1 = repo1.getRemoteStorageContext();

    CustomMultiThreadedHttpConnectionManager cm1 = (CustomMultiThreadedHttpConnectionManager) ((HttpClient) ctx1
            .getContextObject(CommonsHttpClientRemoteStorage.CTX_KEY_CLIENT)).getHttpConnectionManager();

    MatcherAssert.assertThat(cm1.getConnectionsInPool(), is(equalTo(1)));

    RemoteStorageContext ctx2 = repo2.getRemoteStorageContext();

    CustomMultiThreadedHttpConnectionManager cm2 = (CustomMultiThreadedHttpConnectionManager) ((HttpClient) ctx2
            .getContextObject(CommonsHttpClientRemoteStorage.CTX_KEY_CLIENT)).getHttpConnectionManager();

    MatcherAssert.assertThat(cm2.getConnectionsInPool(), is(equalTo(1)));
}

From source file:org.sonatype.nexus.proxy.SimpleRemoteLeakTest.java

@Test
public void checkForConnectionPoolLeakWithFailingTransport() throws Exception {
    // mangle one repos to have quasi different host, thus different HttpCommons HostConfig
    // but make it fail! (unknown host, so will not be able to connect)
    getRepositoryRegistry().getRepositoryWithFacet("repo1", ProxyRepository.class)
            .setRemoteUrl(getRepositoryRegistry().getRepositoryWithFacet("repo1", ProxyRepository.class)
                    .getRemoteUrl().replace("localhost", "1.1.1.1"));

    ProxyRepository repo1 = getRepositoryRegistry().getRepositoryWithFacet("repo1", ProxyRepository.class);
    ProxyRepository repo2 = getRepositoryRegistry().getRepositoryWithFacet("repo2", ProxyRepository.class);

    // this test is CommonsHttpclient3x dependent, it uses it's internals in assertions!
    // So run it only when that RRS is in use
    if (!(repo1.getRemoteStorage() instanceof CommonsHttpClientRemoteStorage)) {
        System.out.println("Test disabled, RRS implementation is not of class "
                + CommonsHttpClientRemoteStorage.class.getName() + " but "
                + repo1.getRemoteStorage().getClass().getName());

        return;// w  w  w.ja  v  a2  s .c  o  m
    }

    // loop until we have some "sensible" result (not unknown, since this is async op)
    // first unforced request will trigger the check, and wait until we have result
    RemoteStatus rs1 = RemoteStatus.UNKNOWN;
    RemoteStatus rs2 = RemoteStatus.UNKNOWN;

    while (RemoteStatus.UNKNOWN.equals(rs1) || RemoteStatus.UNKNOWN.equals(rs2)) {
        rs1 = repo1.getRemoteStatus(new ResourceStoreRequest(RepositoryItemUid.PATH_ROOT), false);
        rs2 = repo2.getRemoteStatus(new ResourceStoreRequest(RepositoryItemUid.PATH_ROOT), false);

        Thread.sleep(1000);
    }

    // get the default context, since they used it
    RemoteStorageContext ctx1 = repo1.getRemoteStorageContext();

    CustomMultiThreadedHttpConnectionManager cm1 = (CustomMultiThreadedHttpConnectionManager) ((HttpClient) ctx1
            .getContextObject(CommonsHttpClientRemoteStorage.CTX_KEY_CLIENT)).getHttpConnectionManager();

    MatcherAssert.assertThat(cm1.getConnectionsInPool(), is(equalTo(1)));

    RemoteStorageContext ctx2 = repo2.getRemoteStorageContext();

    CustomMultiThreadedHttpConnectionManager cm2 = (CustomMultiThreadedHttpConnectionManager) ((HttpClient) ctx2
            .getContextObject(CommonsHttpClientRemoteStorage.CTX_KEY_CLIENT)).getHttpConnectionManager();

    MatcherAssert.assertThat(cm2.getConnectionsInPool(), is(equalTo(1)));
}