Example usage for org.apache.http.conn ClientConnectionManager closeExpiredConnections

List of usage examples for org.apache.http.conn ClientConnectionManager closeExpiredConnections

Introduction

In this page you can find the example usage for org.apache.http.conn ClientConnectionManager closeExpiredConnections.

Prototype

void closeExpiredConnections();

Source Link

Document

Closes all expired connections in the pool.

Usage

From source file:org.eclipse.mylyn.internal.commons.repositories.http.core.IdleConnectionMonitorThread.java

@Override
public void run() {
    try {/*  ww  w.j  a va  2  s  .  c  o  m*/
        while (!shutdown) {
            for (ClientConnectionManager connectionManager : connectionManagers) {
                connectionManager.closeExpiredConnections();
                if (timeout > 0) {
                    connectionManager.closeIdleConnections(timeout, TimeUnit.MILLISECONDS);
                }
            }

            synchronized (this) {
                wait(pollingInterval);
            }
        }
    } catch (InterruptedException e) {
        // shutdown
    }
}

From source file:com.nhn.android.archetype.base.AABaseApplication.java

private void closeHttpClient(HttpClient client) {
    ClientConnectionManager mgr = client.getConnectionManager();
    mgr.closeExpiredConnections();
    mgr.closeIdleConnections(0, TimeUnit.MILLISECONDS);
}

From source file:processing.core.PRequest.java

/**
 * Closes the connection and releases the resources associated with this
 * request to the server.//from   w ww .  j a  v  a2s.  com
 * 
 * @thisref request
 * @thisreftext any variable of the type PRequest
 * @return None
 */
public void close() {
    synchronized (this) {
        state = STATE_CLOSED;
    }
    if (is != null) {
        try {
            is.close();
        } catch (IOException ioe) {
        }
        is = null;
    }
    if (client != null) {
        try {
            ClientConnectionManager mgr = client.getConnectionManager();
            mgr.closeExpiredConnections();
            mgr.closeIdleConnections(1, TimeUnit.SECONDS);
            mgr.shutdown();
        } catch (Exception ioe) {
        }
        client = null;
    }
}

From source file:com.almende.reaal.apachehttp.ApacheHttpClient.java

/**
 * Instantiates a new apache http client.
 *
 *//*  w ww .j  a v a2s.c om*/
private ApacheHttpClient() {

    // Allow self-signed SSL certificates:
    final TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
    final X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
    final SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();

    SSLSocketFactory sslSf;
    try {
        sslSf = new SSLSocketFactory(trustStrategy, hostnameVerifier);
        final Scheme https = new Scheme("https", 443, sslSf);
        schemeRegistry.register(https);
    } catch (Exception e) {
        LOG.warning("Couldn't init SSL socket, https not supported!");
    }

    // Work with PoolingClientConnectionManager
    final ClientConnectionManager connection = new PoolingClientConnectionManager(schemeRegistry);

    // Provide eviction thread to clear out stale threads.
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        wait(5000);
                        connection.closeExpiredConnections();
                        connection.closeIdleConnections(30, TimeUnit.SECONDS);
                    }
                }
            } catch (final InterruptedException ex) {
            }
        }
    }).start();

    // generate httpclient
    httpClient = new DefaultHttpClient(connection);

    final HttpParams params = httpClient.getParams();

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
    params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
    params.setParameter(CoreConnectionPNames.TCP_NODELAY, true);
    httpClient.setParams(params);
}

From source file:com.almende.eve.transport.http.ApacheHttpClient.java

/**
 * Instantiates a new apache http client.
 *
 *///w  w w . j  av a 2  s .c  o m
private ApacheHttpClient() {

    // Allow self-signed SSL certificates:
    final TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
    final X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
    final SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();

    SSLSocketFactory sslSf;
    try {
        sslSf = new SSLSocketFactory(trustStrategy, hostnameVerifier);
        final Scheme https = new Scheme("https", 443, sslSf);
        schemeRegistry.register(https);
    } catch (Exception e) {
        LOG.warning("Couldn't init SSL socket, https not supported!");
    }

    // Work with PoolingClientConnectionManager
    final ClientConnectionManager connection = new PoolingClientConnectionManager(schemeRegistry);

    // Provide eviction thread to clear out stale threads.
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        wait(5000);
                        connection.closeExpiredConnections();
                        connection.closeIdleConnections(30, TimeUnit.SECONDS);
                    }
                }
            } catch (final InterruptedException ex) {
            }
        }
    }).start();

    // generate httpclient
    httpClient = new DefaultHttpClient(connection);

    // Set cookie policy and persistent cookieStore
    try {
        httpClient.setCookieStore(new MyCookieStore());
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "Failed to initialize persistent cookieStore!", e);
    }
    final HttpParams params = httpClient.getParams();

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
    params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
    params.setParameter(CoreConnectionPNames.TCP_NODELAY, true);
    httpClient.setParams(params);
}

From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java

@Test
@SuppressWarnings("deprecation")
public void testConnectionManagerAndProxy() throws Exception {

    HttpParams params = new BasicHttpParams();

    Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(http);// www. jav  a  2  s . co m

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, sr);

    DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);

    // ? HTTP?
    String proxyHost = "proxy.wdf.sap.corp";
    int proxyPort = 8080;
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
            new UsernamePasswordCredentials("", ""));
    HttpHost proxy = new HttpHost(proxyHost, proxyPort);
    httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);

    // ? HTTP?
    HttpRoutePlanner routePlanner = new HttpRoutePlanner() {

        public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context)
                throws HttpException {
            return new HttpRoute(target, null, new HttpHost("proxy.wdf.sap.corp", 8080), true);
        }
    };

    // httpClient.setRoutePlanner(routePlanner);

    String[] urisToGet = { "http://119.29.234.42/", //
            "http://119.29.234.42/solr", //
            "http://119.29.234.42/jenkins", //
            "http://119.29.234.42/jenkins/manage", //
            "http://119.29.234.42/jenkins/credential-store", //
    };

    // ?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);
    }

    // 
    for (int j = 0; j < threads.length; j++) {
        threads[j].start();
    }
    // ?
    for (int j = 0; j < threads.length; j++) {
        threads[j].join();
    }

    cm.closeIdleConnections(40, TimeUnit.SECONDS);
    cm.closeExpiredConnections();

}

From source file:org.mule.modules.httpcomponents.HttpUtil.java

public void disconnect() {
    try {//ww  w.ja  v  a 2  s  . com
        final ClientConnectionManager connectionManager = httpClient.getConnectionManager();
        connectionManager.shutdown();
        connectionManager.closeExpiredConnections();
    } catch (final IllegalStateException e) {
        // if the connection was already closed, avoid throwing exceptions
    }
}