Example usage for org.apache.commons.httpclient HttpConnectionManager closeIdleConnections

List of usage examples for org.apache.commons.httpclient HttpConnectionManager closeIdleConnections

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpConnectionManager closeIdleConnections.

Prototype

public abstract void closeIdleConnections(long paramLong);

Source Link

Usage

From source file:com.legstar.test.cixs.AbstractHttpClientTester.java

/**
 * Setup the static http connection pool.
 * /*from  w w w .  j  a  v a2 s .  com*/
 * @return an http client instance
 */
protected static HttpClient createHttpClient() {

    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();

    connectionManagerParams.setMaxTotalConnections(MAX_TOTAL_CONNECTIONS);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(DEFAULT_MAX_CONNECTIONS_PER_HOST);
    connectionManagerParams.setTcpNoDelay(TCP_NO_DELAY);
    connectionManagerParams.setSoTimeout(SOCKET_TIMEOUT);
    connectionManagerParams.setConnectionTimeout(CONNECT_TIMEOUT);

    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);
    connectionManager.closeIdleConnections(IDLE_CONNECTION_TIMEOUT);
    return new HttpClient(connectionManager);
}

From source file:com.jaspersoft.ireport.jasperserver.ws.http.IdleConnectionMonitorThread.java

@Override
public void run() {
    try {/* w ww .j a  v  a2 s. c  om*/
        while (!shutdown) {
            synchronized (this) {
                wait(5000);
                for (HttpConnectionManager m : connMgr) {
                    // Close expired connections
                    // m.closeExpiredConnections();
                    // Optionally, close connections
                    // that have been idle longer than 30 sec
                    m.closeIdleConnections(30000);
                }
            }
        }
    } catch (InterruptedException ex) {
        // terminate
    }
}

From source file:it.geosolutions.geoserver.rest.HTTPUtils.java

public static boolean httpPing(String url, String username, String pw) {

    GetMethod httpMethod = null;//w  ww  .  ja v a 2 s.c  om
    HttpClient client = new HttpClient();
    HttpConnectionManager connectionManager = client.getHttpConnectionManager();
    try {
        setAuth(client, url, username, pw);
        httpMethod = new GetMethod(url);
        connectionManager.getParams().setConnectionTimeout(2000);
        int status = client.executeMethod(httpMethod);
        if (status != HttpStatus.SC_OK) {
            LOGGER.warn("PING failed at '" + url + "': (" + status + ") " + httpMethod.getStatusText());
            return false;
        } else {
            return true;
        }
    } catch (ConnectException e) {
        return false;
    } catch (IOException e) {
        LOGGER.error(e.getLocalizedMessage(), e);
        return false;
    } finally {
        if (httpMethod != null)
            httpMethod.releaseConnection();
        connectionManager.closeIdleConnections(0);
    }
}

From source file:it.geosolutions.geoserver.rest.HTTPUtils.java

/**
 * Used to query for REST resources.//from   w  ww. j  a  va 2 s  .c  om
 * 
 * @param url The URL of the REST resource to query about.
 * @param username
 * @param pw
 * @return true on 200, false on 404.
 * @throws RuntimeException on unhandled status or exceptions.
 */
public static boolean exists(String url, String username, String pw) {

    GetMethod httpMethod = null;
    HttpClient client = new HttpClient();
    HttpConnectionManager connectionManager = client.getHttpConnectionManager();
    try {
        setAuth(client, url, username, pw);
        httpMethod = new GetMethod(url);
        connectionManager.getParams().setConnectionTimeout(2000);
        int status = client.executeMethod(httpMethod);
        switch (status) {
        case HttpStatus.SC_OK:
            return true;
        case HttpStatus.SC_NOT_FOUND:
            return false;
        default:
            throw new RuntimeException("Unhandled response status at '" + url + "': (" + status + ") "
                    + httpMethod.getStatusText());
        }
    } catch (ConnectException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (httpMethod != null)
            httpMethod.releaseConnection();
        connectionManager.closeIdleConnections(0);
    }
}

From source file:it.geosolutions.geoserver.rest.HTTPUtils.java

/**
 * Performs an HTTP GET on the given URL. <BR>
 * Basic auth is used if both username and pw are not null.
 * //from  w  w  w  .  ja va 2s  .  com
 * @param url The URL where to connect to.
 * @param username Basic auth credential. No basic auth if null.
 * @param pw Basic auth credential. No basic auth if null.
 * @return The HTTP response as a String if the HTTP response code was 200
 *         (OK).
 * @throws MalformedURLException
 */
public static String get(String url, String username, String pw) {

    GetMethod httpMethod = null;
    HttpClient client = new HttpClient();
    HttpConnectionManager connectionManager = client.getHttpConnectionManager();
    try {
        setAuth(client, url, username, pw);
        httpMethod = new GetMethod(url);
        connectionManager.getParams().setConnectionTimeout(5000);
        int status = client.executeMethod(httpMethod);
        if (status == HttpStatus.SC_OK) {
            InputStream is = httpMethod.getResponseBodyAsStream();
            String response = IOUtils.toString(is);
            IOUtils.closeQuietly(is);
            if (response.trim().length() == 0) { // sometime gs rest fails
                LOGGER.warn("ResponseBody is empty");
                return null;
            } else {
                return response;
            }
        } else {
            LOGGER.info("(" + status + ") " + HttpStatus.getStatusText(status) + " -- " + url);
        }
    } catch (ConnectException e) {
        LOGGER.info("Couldn't connect to [" + url + "]");
    } catch (IOException e) {
        LOGGER.info("Error talking to [" + url + "]", e);
    } finally {
        if (httpMethod != null)
            httpMethod.releaseConnection();
        connectionManager.closeIdleConnections(0);
    }

    return null;
}

From source file:it.geosolutions.geoserver.rest.HTTPUtils.java

public static boolean delete(String url, final String user, final String pw) {

    DeleteMethod httpMethod = null;//from  ww  w  .j a  va  2 s  .  c  o  m
    HttpClient client = new HttpClient();
    HttpConnectionManager connectionManager = client.getHttpConnectionManager();
    try {
        setAuth(client, url, user, pw);
        httpMethod = new DeleteMethod(url);
        connectionManager.getParams().setConnectionTimeout(5000);
        int status = client.executeMethod(httpMethod);
        String response = "";
        if (status == HttpStatus.SC_OK) {
            InputStream is = httpMethod.getResponseBodyAsStream();
            response = IOUtils.toString(is);
            IOUtils.closeQuietly(is);
            if (response.trim().equals("")) {
                if (LOGGER.isTraceEnabled())
                    LOGGER.trace(
                            "ResponseBody is empty (this may be not an error since we just performed a DELETE call)");
                return true;
            }
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("(" + status + ") " + httpMethod.getStatusText() + " -- " + url);
            return true;
        } else {
            LOGGER.info("(" + status + ") " + httpMethod.getStatusText() + " -- " + url);
            LOGGER.info("Response: '" + response + "'");
        }
    } catch (ConnectException e) {
        LOGGER.info("Couldn't connect to [" + url + "]");
    } catch (IOException e) {
        LOGGER.info("Error talking to [" + url + "]", e);
    } finally {
        if (httpMethod != null)
            httpMethod.releaseConnection();
        connectionManager.closeIdleConnections(0);
    }

    return false;
}

From source file:it.geosolutions.geoserver.rest.HTTPUtils.java

/**
 * Send an HTTP request (PUT or POST) to a server. <BR>
 * Basic auth is used if both username and pw are not null.
 * <P>//from   ww  w .  ja v a 2  s .  co m
 * Only
 * <UL>
 * <LI>200: OK</LI>
 * <LI>201: ACCEPTED</LI>
 * <LI>202: CREATED</LI>
 * </UL>
 * are accepted as successful codes; in these cases the response string will
 * be returned.
 * 
 * @return the HTTP response or <TT>null</TT> on errors.
 */
private static String send(final EntityEnclosingMethod httpMethod, String url, RequestEntity requestEntity,
        String username, String pw) {
    HttpClient client = new HttpClient();
    HttpConnectionManager connectionManager = client.getHttpConnectionManager();
    try {
        setAuth(client, url, username, pw);
        connectionManager.getParams().setConnectionTimeout(5000);
        if (requestEntity != null)
            httpMethod.setRequestEntity(requestEntity);
        int status = client.executeMethod(httpMethod);

        switch (status) {
        case HttpURLConnection.HTTP_OK:
        case HttpURLConnection.HTTP_CREATED:
        case HttpURLConnection.HTTP_ACCEPTED:
            String response = IOUtils.toString(httpMethod.getResponseBodyAsStream());
            // LOGGER.info("================= POST " + url);
            if (LOGGER.isInfoEnabled())
                LOGGER.info("HTTP " + httpMethod.getStatusText() + ": " + response);
            return response;
        default:
            LOGGER.warn("Bad response: code[" + status + "]" + " msg[" + httpMethod.getStatusText() + "]"
                    + " url[" + url + "]" + " method[" + httpMethod.getClass().getSimpleName() + "]: "
                    + IOUtils.toString(httpMethod.getResponseBodyAsStream()));
            return null;
        }
    } catch (ConnectException e) {
        LOGGER.info("Couldn't connect to [" + url + "]");
        return null;
    } catch (IOException e) {
        LOGGER.error("Error talking to " + url + " : " + e.getLocalizedMessage());
        return null;
    } finally {
        if (httpMethod != null)
            httpMethod.releaseConnection();
        connectionManager.closeIdleConnections(0);
    }
}

From source file:org.eclipse.ecf.internal.provider.filetransfer.httpclient.ConnectionManagerHelper.java

private static void initParameters(HttpClient httpClient, HttpConnectionManager cm, boolean cmIsShared,
        Map options) {//from   w  ww .  j  a  v a 2 s  .c om

    if (cmIsShared) {
        long closeIdlePeriod = getLongProperty(ConnectionOptions.PROP_POOL_CLOSE_IDLE_PERIOD,
                ConnectionOptions.POOL_CLOSE_IDLE_PERIOD_DEFAULT);
        if (closeIdlePeriod > 0) {
            Trace.trace(Activator.PLUGIN_ID,
                    "Closing connections which were idle at least " + closeIdlePeriod + " milliseconds."); //$NON-NLS-1$ //$NON-NLS-2$
            cm.closeIdleConnections(closeIdlePeriod);
        }
    }

    // HttpClient parameters can be traced independently
    httpClient.setHttpConnectionManager(cm);
    int readTimeout = getSocketReadTimeout(options);
    cm.getParams().setSoTimeout(readTimeout);
    int connectTimeout = getConnectTimeout(options);
    cm.getParams().setConnectionTimeout(connectTimeout);

    if (cmIsShared) {
        HttpConnectionManagerParams cmParams = cm.getParams();
        int maxHostConnections = getIntegerProperty(ConnectionOptions.PROP_MAX_CONNECTIONS_PER_HOST,
                ConnectionOptions.MAX_CONNECTIONS_PER_HOST_DEFAULT);
        int maxTotalConnections = getIntegerProperty(ConnectionOptions.PROP_MAX_TOTAL_CONNECTIONS,
                ConnectionOptions.MAX_TOTAL_CONNECTIONS_DEFAULT);

        cmParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
        cmParams.setMaxTotalConnections(maxTotalConnections);
        long connectionManagerTimeout = getLongProperty(ConnectionOptions.PROP_POOL_CONNECTION_TIMEOUT,
                ConnectionOptions.POOL_CONNECTION_TIMEOUT_DEFAULT);
        httpClient.getParams().setConnectionManagerTimeout(connectionManagerTimeout);
    }
}

From source file:org.switchyard.component.test.mixins.http.HTTPMixIn.java

@Override
public void uninitialize() {
    if (_httpClient != null) {
        final HttpConnectionManager connectionManager = _httpClient.getHttpConnectionManager();
        if (connectionManager instanceof MultiThreadedHttpConnectionManager) {
            final MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = (MultiThreadedHttpConnectionManager) connectionManager;
            multiThreadedHttpConnectionManager.shutdown();
        }//from  ww  w  . ja  v a 2 s. c o  m
        connectionManager.closeIdleConnections(0);
    }
}