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

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

Introduction

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

Prototype

void closeIdleConnections(long idletime, TimeUnit tunit);

Source Link

Document

Closes idle connections in the pool.

Usage

From source file:org.apache.camel.component.box.internal.BoxClientHelper.java

public static void closeIdleConnections(CachedBoxClient cachedBoxClient) {
    @SuppressWarnings("deprecation")
    final ClientConnectionManager connectionManager = cachedBoxClient.getClientConnectionManager();
    if (connectionManager != null) {
        // close all idle connections
        connectionManager.closeIdleConnections(1, TimeUnit.MILLISECONDS);
    }//w ww .  ja  v a 2  s  .  c o  m
}

From source file:com.aliyun.oss.common.comm.IdleConnectionReaper.java

@SuppressWarnings("unchecked")
@Override/*from w w w.j  ava  2 s  . c o m*/
public void run() {
    while (true) {
        if (shuttingDown) {
            getLogger().debug("Shutting down reaper thread.");
            return;
        }
        try {
            Thread.sleep(REAP_INTERVAL_MILLISECONDS);

            List<ClientConnectionManager> connectionManagers = null;
            synchronized (IdleConnectionReaper.class) {
                connectionManagers = (List<ClientConnectionManager>) IdleConnectionReaper.connectionManagers
                        .clone();
            }
            for (ClientConnectionManager connectionManager : connectionManagers) {
                try {
                    connectionManager.closeIdleConnections(60, TimeUnit.SECONDS);
                } catch (Exception ex) {
                    getLogger().warn("Unable to close idle connections", ex);
                }
            }
        } catch (Throwable t) {
            getLogger().debug("Reaper thread: ", t);
        }
    }
}

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

private void closeHttpClient(HttpClient client) {
    ClientConnectionManager mgr = client.getConnectionManager();
    mgr.closeExpiredConnections();/* w  w  w.jav a  2 s . co m*/
    mgr.closeIdleConnections(0, TimeUnit.MILLISECONDS);
}

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

@Override
public void run() {
    try {//from  w w w. j a va  2s  .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:processing.core.PRequest.java

/**
 * Closes the connection and releases the resources associated with this
 * request to the server./*ww  w.  ja va 2 s  .  c  om*/
 * 
 * @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.amazon.s3.http.IdleConnectionReaper.java

@SuppressWarnings("unchecked")
@Override//from  w w  w .  j ava  2s  .c  om
public void run() {
    while (true) {
        try {
            Thread.sleep(PERIOD_MILLISECONDS);

            // Copy the list of managed ConnectionManagers to avoid possible
            // ConcurrentModificationExceptions if registerConnectionManager or
            // removeConnectionManager are called while we're iterating (rather
            // than block/lock while this loop executes).
            List<ClientConnectionManager> connectionManagers = null;
            synchronized (IdleConnectionReaper.class) {
                connectionManagers = (List<ClientConnectionManager>) IdleConnectionReaper.connectionManagers
                        .clone();
            }
            for (ClientConnectionManager connectionManager : connectionManagers) {
                // When we release connections, the connection manager leaves them
                // open so they can be reused.  We want to close out any idle
                // connections so that they don't sit around in CLOSE_WAIT.
                try {
                    connectionManager.closeIdleConnections(60, TimeUnit.SECONDS);
                } catch (Throwable t) {
                    Log.w(TAG, "Unable to close idle connections", t);
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        }
    }
}

From source file:cn.ctyun.amazonaws.http.IdleConnectionReaper.java

@SuppressWarnings("unchecked")
@Override/* ww  w  .  j a v  a 2s.c  o  m*/
public void run() {
    while (true) {
        try {
            Thread.sleep(PERIOD_MILLISECONDS);

            // Copy the list of managed ConnectionManagers to avoid possible
            // ConcurrentModificationExceptions if registerConnectionManager or
            // removeConnectionManager are called while we're iterating (rather
            // than block/lock while this loop executes).
            List<ClientConnectionManager> connectionManagers = null;
            synchronized (IdleConnectionReaper.class) {
                connectionManagers = (List<ClientConnectionManager>) IdleConnectionReaper.connectionManagers
                        .clone();
            }
            for (ClientConnectionManager connectionManager : connectionManagers) {
                // When we release connections, the connection manager leaves them
                // open so they can be reused.  We want to close out any idle
                // connections so that they don't sit around in CLOSE_WAIT.
                try {
                    connectionManager.closeIdleConnections(60, TimeUnit.SECONDS);
                } catch (Throwable t) {
                    log.warn("Unable to close idle connections", t);
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        }
    }
}

From source file:com.amazonaws.http.IdleConnectionReaper.java

@SuppressWarnings("unchecked")
@Override/*  w ww.jav  a  2  s .  c  om*/
public void run() {
    while (true) {
        if (shuttingDown) {
            log.debug("Shutting down reaper thread.");
            return;
        }
        try {
            Thread.sleep(PERIOD_MILLISECONDS);

            // Copy the list of managed ConnectionManagers to avoid possible
            // ConcurrentModificationExceptions if registerConnectionManager or
            // removeConnectionManager are called while we're iterating (rather
            // than block/lock while this loop executes).
            List<ClientConnectionManager> connectionManagers = null;
            synchronized (IdleConnectionReaper.class) {
                connectionManagers = (List<ClientConnectionManager>) IdleConnectionReaper.connectionManagers
                        .clone();
            }
            for (ClientConnectionManager connectionManager : connectionManagers) {
                // When we release connections, the connection manager leaves them
                // open so they can be reused.  We want to close out any idle
                // connections so that they don't sit around in CLOSE_WAIT.
                try {
                    connectionManager.closeIdleConnections(60, TimeUnit.SECONDS);
                } catch (Exception t) {
                    log.warn("Unable to close idle connections", t);
                }
            }
        } catch (Throwable t) {
            log.debug("Reaper thread: ", t);
        }
    }
}

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

/**
 * Instantiates a new apache http client.
 *
 *///from  w  w w .  j a va 2s  .  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);

    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.
 *
 *//*  ww  w  . j  av a 2 s . 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);

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