Example usage for org.apache.commons.httpclient.util IdleConnectionTimeoutThread IdleConnectionTimeoutThread

List of usage examples for org.apache.commons.httpclient.util IdleConnectionTimeoutThread IdleConnectionTimeoutThread

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.util IdleConnectionTimeoutThread IdleConnectionTimeoutThread.

Prototype

public IdleConnectionTimeoutThread() 

Source Link

Usage

From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java

/**
 * ?/*ww  w  .  j a va 2  s .c o  m*/
 */
private HttpProtocolHandler() {
    // HTTP
    connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost);
    connectionManager.getParams().setMaxTotalConnections(defaultMaxTotalConn);

    IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread();
    ict.addConnectionManager(connectionManager);
    ict.setConnectionTimeout(defaultIdleConnTimeout);

    ict.start();
}

From source file:com.ning.http.client.providers.apache.ApacheAsyncHttpProvider.java

public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler) throws IOException {
    if (isClose.get()) {
        throw new IOException("Closed");
    }//from  ww  w .j  a v  a2 s . com

    if (ResumableAsyncHandler.class.isAssignableFrom(handler.getClass())) {
        request = ResumableAsyncHandler.class.cast(handler).adjustRequestRange(request);
    }

    if (config.getMaxTotalConnections() > -1 && (maxConnections.get() + 1) > config.getMaxTotalConnections()) {
        throw new IOException(String.format("Too many connections %s", config.getMaxTotalConnections()));
    }

    if (idleConnectionTimeoutThread != null) {
        idleConnectionTimeoutThread.shutdown();
        idleConnectionTimeoutThread = null;
    }

    int requestTimeout = requestTimeout(config, request.getPerRequestConfig());
    if (config.getIdleConnectionTimeoutInMs() > 0 && requestTimeout != -1
            && requestTimeout < config.getIdleConnectionTimeoutInMs()) {
        idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
        idleConnectionTimeoutThread.setConnectionTimeout(config.getIdleConnectionTimeoutInMs());
        idleConnectionTimeoutThread.addConnectionManager(connectionManager);
        idleConnectionTimeoutThread.start();
    }

    HttpClient httpClient = new HttpClient(params, connectionManager);

    Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
    if (realm != null) {
        httpClient.getParams().setAuthenticationPreemptive(realm.getUsePreemptiveAuth());
        Credentials defaultcreds = new UsernamePasswordCredentials(realm.getPrincipal(), realm.getPassword());
        httpClient.getState().setCredentials(new AuthScope(null, -1, AuthScope.ANY_REALM), defaultcreds);
    }

    HttpMethodBase method = createMethod(httpClient, request);
    ApacheResponseFuture f = new ApacheResponseFuture<T>(handler, requestTimeout, request, method);
    f.touch();

    f.setInnerFuture(
            config.executorService().submit(new ApacheClientRunnable(request, handler, method, f, httpClient)));
    maxConnections.incrementAndGet();
    return f;
}

From source file:com.ning.http.client.providers.apache.TestableApacheAsyncHttpProvider.java

public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler) throws IOException {
    if (isClose.get()) {
        throw new IOException("Closed");
    }//from  w  ww.ja  va  2 s . c  o  m

    if (ResumableAsyncHandler.class.isAssignableFrom(handler.getClass())) {
        request = ResumableAsyncHandler.class.cast(handler).adjustRequestRange(request);
    }

    if (config.getMaxTotalConnections() > -1 && (maxConnections.get() + 1) > config.getMaxTotalConnections()) {
        throw new IOException(String.format("Too many connections %s", config.getMaxTotalConnections()));
    }

    if (idleConnectionTimeoutThread != null) {
        idleConnectionTimeoutThread.shutdown();
        idleConnectionTimeoutThread = null;
    }

    int requestTimeout = requestTimeout(config, request.getPerRequestConfig());
    if (config.getIdleConnectionTimeoutInMs() > 0 && requestTimeout != -1
            && requestTimeout < config.getIdleConnectionTimeoutInMs()) {
        idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
        idleConnectionTimeoutThread.setConnectionTimeout(config.getIdleConnectionTimeoutInMs());
        idleConnectionTimeoutThread.addConnectionManager(connectionManager);
        idleConnectionTimeoutThread.start();
    }

    HttpClient httpClient = testClient != null ? testClient : new HttpClient(params, connectionManager);

    Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
    if (realm != null) {
        httpClient.getParams().setAuthenticationPreemptive(realm.getUsePreemptiveAuth());
        Credentials defaultCredentials = new UsernamePasswordCredentials(realm.getPrincipal(),
                realm.getPassword());
        httpClient.getState().setCredentials(new AuthScope(null, -1, AuthScope.ANY_REALM), defaultCredentials);
    }

    HttpMethodBase method = createMethod(httpClient, request);
    ApacheResponseFuture<T> f = new ApacheResponseFuture<>(handler, requestTimeout, request, method);
    f.touch();

    f.setInnerFuture(config.executorService()
            .submit(new ApacheClientRunnable<>(request, handler, method, f, httpClient)));
    maxConnections.incrementAndGet();
    return f;
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Create and start a new HttpConnectionManager, close idle connections every minute.
 */// w w w  . j  a  va 2  s . c  o m
public static void start() {
    synchronized (LOCK) {
        if (httpConnectionManagerThread == null) {
            httpConnectionManagerThread = new IdleConnectionTimeoutThread();
            httpConnectionManagerThread.setName(IdleConnectionTimeoutThread.class.getSimpleName());
            httpConnectionManagerThread.setConnectionTimeout(ONE_MINUTE);
            httpConnectionManagerThread.setTimeoutInterval(ONE_MINUTE);
            httpConnectionManagerThread.start();
        }
    }
}

From source file:nl.nn.adapterframework.http.HttpConnectionManager.java

private void initIdleConnectionsHandler(long connectionTimeout, long timeoutInterval, String ownerName) {
    AppConstants ac = AppConstants.getInstance();
    try {/*  www. java2  s .  co m*/
        if (connectionTimeout == 0) {
            connectionTimeout = ac.getInt(CONNECTION_TIMEOUT_KEY, DEFAULT_CONNECTION_TIMEOUT);
        }
    } catch (Exception e) {
        log.warn("could not parse connection timeout from property [" + CONNECTION_TIMEOUT_KEY + "]", e);
    }
    try {
        if (timeoutInterval == 0) {
            timeoutInterval = ac.getInt(TIMEOUT_INTERVAL_KEY, DEFAULT_TIMEOUT_INTERVAL);
        }
    } catch (Exception e) {
        log.warn("could not parse timeout interval from property [" + TIMEOUT_INTERVAL_KEY + "]", e);
    }
    if (connectionTimeout > 0 && timeoutInterval > 0) {
        log.debug("starting idleConnectionTimeoutThread, timeout [" + connectionTimeout + "] s interval ["
                + timeoutInterval + "] s");
        idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
        idleConnectionTimeoutThread.setConnectionTimeout(connectionTimeout * 1000);
        idleConnectionTimeoutThread.setTimeoutInterval(timeoutInterval * 1000);
        idleConnectionTimeoutThread.addConnectionManager(this);
        idleConnectionTimeoutThread.setName(
                "IdleConnectionManager-" + idleConnectionTimeoutThread.getName() + "-for-" + ownerName);
        idleConnectionTimeoutThread.start();
        log.info("started idleConnectionTimeoutThread [" + idleConnectionTimeoutThread.getName()
                + "], timeout [" + connectionTimeout + "] s interval [" + timeoutInterval + "] s");
    }
}

From source file:org.activebpel.rt.axis.bpel.handlers.AeHTTPSender.java

/** @deprecated */
protected void initialize() {
    if (sConnectionManager == null) {
        synchronized (AeHTTPSender.class) {
            if (sConnectionManager == null) {
                // initialize the connection manager w/ its properties
                MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
                sClientProperties = CommonsHTTPClientPropertiesFactory.create();

                // max connections per host, defaults to constant above
                int maxConnectionsPerHost = getIntegerOption(
                        DefaultCommonsHTTPClientProperties.MAXIMUM_CONNECTIONS_PER_HOST_PROPERTY_KEY,
                        DEFAULT_MAX_CONNECTIONS_PER_HOST);

                cm.setMaxConnectionsPerHost(maxConnectionsPerHost);

                // max connections, defaults to constant above
                int maxConnections = getIntegerOption(
                        DefaultCommonsHTTPClientProperties.MAXIMUM_TOTAL_CONNECTIONS_PROPERTY_KEY,
                        DEFAULT_MAX_CONNECTIONS);
                // as per apache-commons problem report #36882 (max connections per host setting does not work)
                // cm.setMaxTotalConnections(maxConnections);
                cm.getParams().setMaxTotalConnections(maxConnections);

                // save the connection manager for future calls
                sConnectionManager = cm;

                sConnectionTimeoutThread = new IdleConnectionTimeoutThread();
                sConnectionTimeoutThread.addConnectionManager(sConnectionManager);

                int sweepInterval = getIntegerOption(KEY_IDLE_SWEEP_INTERVAL,
                        DEFAULT_IDLE_CONNECTION_SWEEP_INTERVAL);
                sConnectionTimeoutThread.setTimeoutInterval(sweepInterval);

                int idleTimeout = getIntegerOption(KEY_IDLE_TIMEOUT, DEFAULT_IDLE_CONNECTION_TIMEOUT);
                sConnectionTimeoutThread.setConnectionTimeout(idleTimeout);
                sConnectionTimeoutThread.start();
            }/*from   w w  w .  j  ava 2 s  .  c  o m*/
        }
    }
    connectionManager = sConnectionManager;
    clientProperties = sClientProperties;
}

From source file:org.apache.ode.axis2.ODEServer.java

private void initHttpConnectionManager() throws ServletException {
    httpConnectionManager = new MultiThreadedHttpConnectionManager();
    // settings may be overridden from ode-axis2.properties using the same properties as HttpClient
    // /!\ If the size of the conn pool is smaller than the size of the thread pool, the thread pool might get starved.
    int max_per_host = Integer.parseInt(_odeConfig.getProperty(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS,
            "" + _odeConfig.getPoolMaxSize()));
    int max_total = Integer.parseInt(_odeConfig.getProperty(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
            "" + _odeConfig.getPoolMaxSize()));
    if (__log.isDebugEnabled()) {
        __log.debug(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS + "=" + max_per_host);
        __log.debug(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS + "=" + max_total);
    }/*w  w w  .ja  v a  2 s. com*/
    if (max_per_host < 1 || max_total < 1) {
        String errmsg = HttpConnectionManagerParams.MAX_HOST_CONNECTIONS + " and "
                + HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS + " must be positive integers!";
        __log.error(errmsg);
        throw new ServletException(errmsg);
    }
    httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(max_per_host);
    httpConnectionManager.getParams().setMaxTotalConnections(max_total);

    // Register the connection manager to a idle check thread
    idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
    idleConnectionTimeoutThread.setName("Http_Idle_Connection_Timeout_Thread");
    long idleConnectionTimeout = Long
            .parseLong(_odeConfig.getProperty("http.idle.connection.timeout", "30000"));
    long idleConnectionCheckInterval = Long
            .parseLong(_odeConfig.getProperty("http.idle.connection.check.interval", "30000"));

    if (__log.isDebugEnabled()) {
        __log.debug("http.idle.connection.timeout=" + idleConnectionTimeout);
        __log.debug("http.idle.connection.check.interval=" + idleConnectionCheckInterval);
    }
    idleConnectionTimeoutThread.setConnectionTimeout(idleConnectionTimeout);
    idleConnectionTimeoutThread.setTimeoutInterval(idleConnectionCheckInterval);

    idleConnectionTimeoutThread.addConnectionManager(httpConnectionManager);
    idleConnectionTimeoutThread.start();
}

From source file:org.asynchttpclient.providers.apache.ApacheAsyncHttpProvider.java

public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler) throws IOException {
    if (isClose.get()) {
        throw new IOException("Closed");
    }/*  w w w .j av  a2  s. c  om*/

    if (ResumableAsyncHandler.class.isAssignableFrom(handler.getClass())) {
        request = ResumableAsyncHandler.class.cast(handler).adjustRequestRange(request);
    }

    if (config.getMaxTotalConnections() > -1 && (maxConnections.get() + 1) > config.getMaxTotalConnections()) {
        throw new IOException(String.format("Too many connections %s", config.getMaxTotalConnections()));
    }

    if (idleConnectionTimeoutThread != null) {
        idleConnectionTimeoutThread.shutdown();
        idleConnectionTimeoutThread = null;
    }

    int requestTimeout = AsyncHttpProviderUtils.requestTimeout(config, request);
    if (config.getIdleConnectionTimeoutInMs() > 0 && requestTimeout != -1
            && requestTimeout < config.getIdleConnectionTimeoutInMs()) {
        idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
        idleConnectionTimeoutThread.setConnectionTimeout(config.getIdleConnectionTimeoutInMs());
        idleConnectionTimeoutThread.addConnectionManager(connectionManager);
        idleConnectionTimeoutThread.start();
    }

    HttpClient httpClient = new HttpClient(params, connectionManager);

    Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
    if (realm != null) {
        httpClient.getParams().setAuthenticationPreemptive(realm.getUsePreemptiveAuth());
        Credentials defaultcreds = new UsernamePasswordCredentials(realm.getPrincipal(), realm.getPassword());
        httpClient.getState().setCredentials(new AuthScope(null, -1, AuthScope.ANY_REALM), defaultcreds);
    }

    HttpMethodBase method = createMethod(httpClient, request);
    ApacheResponseFuture<T> f = new ApacheResponseFuture<T>(handler, requestTimeout, request, method);
    f.touch();

    f.setInnerFuture(config.executorService()
            .submit(new ApacheClientRunnable<T>(request, handler, method, f, httpClient)));
    maxConnections.incrementAndGet();
    return f;
}

From source file:org.bibsonomy.recommender.tags.WebserviceTagRecommender.java

/**
 * inits the recommender//  w w w.  j  a v  a  2  s . c om
 */
public WebserviceTagRecommender() {
    // Create an instance of HttpClient.
    connectionManager = new IdleClosingConnectionManager();// MultiThreadedHttpConnectionManager();
    client = new HttpClient(connectionManager);

    // set default timeouts
    final HttpConnectionManagerParams connectionParams = connectionManager.getParams();
    connectionParams.setSoTimeout(SOCKET_TIMEOUT_MS);
    connectionParams.setConnectionTimeout(HTTP_CONNECTION_TIMEOUT_MS);
    connectionManager.setParams(connectionParams);
    log.debug("MAXCONNECTIONS: " + connectionParams.getMaxTotalConnections());
    log.debug("MAXCONNECTIONSPERHOST: " + connectionParams.getDefaultMaxConnectionsPerHost());

    // handle idle connections
    connectionManager.closeIdleConnections(IDLE_TIMEOUT_MS);
    idleConnectionHandler = new IdleConnectionTimeoutThread();
    idleConnectionHandler.addConnectionManager(connectionManager);
    idleConnectionHandler.start();

    this.renderer = new RendererFactory(new UrlRenderer("/api/")).getRenderer(RenderingFormat.XML);
}

From source file:org.eclipse.mylyn.commons.net.WebUtil.java

/**
 * @since 3.1//  w w w  .  jav  a  2s . c  om
 */
public synchronized static void addConnectionManager(HttpConnectionManager connectionManager) {
    if (idleConnectionTimeoutThread == null) {
        idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
        idleConnectionTimeoutThread.setTimeoutInterval(CONNECTION_TIMEOUT_INTERVAL);
        idleConnectionTimeoutThread.setConnectionTimeout(CONNNECT_TIMEOUT);
        idleConnectionTimeoutThread.start();
    }
    idleConnectionTimeoutThread.addConnectionManager(connectionManager);
}