Example usage for org.apache.http.params HttpConnectionParams setLinger

List of usage examples for org.apache.http.params HttpConnectionParams setLinger

Introduction

In this page you can find the example usage for org.apache.http.params HttpConnectionParams setLinger.

Prototype

public static void setLinger(HttpParams httpParams, int i) 

Source Link

Usage

From source file:cn.keke.travelmix.HttpClientHelper.java

public static HttpClient getNewHttpClient() {
    try {/* www .  ja v a  2  s.c om*/
        SSLSocketFactory sf = new EasySSLSocketFactory();

        // TODO test, if SyncBasicHttpParams is needed
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpProtocolParams.setUseExpectContinue(params, false);
        HttpProtocolParams.setHttpElementCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSocketBufferSize(params, 8192);
        HttpConnectionParams.setLinger(params, 1);
        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        HttpConnectionParams.setSoReuseaddr(params, true);
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpClientParams.setCookiePolicy(params, CookiePolicy.IGNORE_COOKIES);
        HttpClientParams.setAuthenticating(params, false);
        HttpClientParams.setRedirecting(params, false);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        registry.register(new Scheme("https", 443, sf));

        ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(registry, 20, TimeUnit.MINUTES);
        ccm.setMaxTotal(100);
        ccm.setDefaultMaxPerRoute(20);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        LOG.warn("Failed to create custom http client. Default http client is created", e);
        return new DefaultHttpClient();
    }
}

From source file:org.frameworkset.spi.remote.http.Client.java

public static HttpParams buildHttpParams() {

    int so_timeout = conparams.getInt("http.socket.timeout", 30);//??

    int CONNECTION_TIMEOUT = conparams.getInt("http.connection.timeout", 30);
    int CONNECTION_Manager_TIMEOUT = conparams.getInt("http.conn-manager.timeout", 2);
    int httpsoLinger = conparams.getInt("http.soLinger", -1);
    HttpParams params = new BasicHttpParams();

    HttpConnectionParams.setSoTimeout(params, so_timeout * 1000);
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT * 1000);
    HttpConnectionParams.setLinger(params, httpsoLinger);
    ConnManagerParams.setTimeout(params, CONNECTION_Manager_TIMEOUT * 1000);
    //      params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, 
    //            "org.frameworkset.spi.remote.http.BBossClientConnectionManagerFactory");
    return params;
}

From source file:com.autonomy.aci.client.transport.impl.HttpClientFactory.java

/**
 * Creates an instance of <tt>DefaultHttpClient</tt> with a <tt>ThreadSafeClientConnManager</tt>.
 * @return an implementation of the <tt>HttpClient</tt> interface.
 *//*from   w w w .ja v  a2s.  c  om*/
public HttpClient createInstance() {
    LOGGER.debug("Creating a new instance of DefaultHttpClient with configuration -> {}", toString());

    // Create the connection manager which will be default create the necessary schema registry stuff...
    final PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setMaxTotal(maxTotalConnections);
    connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);

    // Set the HTTP connection parameters (These are in the HttpCore JavaDocs, NOT the HttpClient ones)...
    final HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
    HttpConnectionParams.setLinger(params, linger);
    HttpConnectionParams.setSocketBufferSize(params, socketBufferSize);
    HttpConnectionParams.setSoKeepalive(params, soKeepAlive);
    HttpConnectionParams.setSoReuseaddr(params, soReuseAddr);
    HttpConnectionParams.setSoTimeout(params, soTimeout);
    HttpConnectionParams.setStaleCheckingEnabled(params, staleCheckingEnabled);
    HttpConnectionParams.setTcpNoDelay(params, tcpNoDelay);

    // Create the HttpClient and configure the compression interceptors if required...
    final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, params);

    if (useCompression) {
        httpClient.addRequestInterceptor(new RequestAcceptEncoding());
        httpClient.addResponseInterceptor(new DeflateContentEncoding());
    }

    return httpClient;
}

From source file:com.sayar.requests.impl.HttpClientRequestHandler.java

protected HttpClient getHttpClient(final RequestArguments args) {
    // The HttpClient is heavy and should be instantiated once per app.
    // It holds a thread pool for requests and is threadsafe.

    if (HttpClientRequestHandler.HttpClient == null) {
        // Reference:
        // http://na.isobar.com/2011/best-way-to-use-httpclient-in-android/
        final SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        // TODO: WE SHOULD BE THROWING AN EXCEMPTION IF CONTEXT IS NULL
        registry.register(new Scheme("https", this.getHttpsSocketFactory(args.getContext()), 443));

        final HttpParams basicparms = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(basicparms, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setHttpElementCharset(basicparms, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setVersion(basicparms, args.getHttpVersion());
        HttpProtocolParams.setUseExpectContinue(basicparms, true);
        HttpProtocolParams.setUserAgent(basicparms, args.getUserAgent());

        // TODO: Need sensible defaults.
        HttpConnectionParams.setLinger(basicparms, -1);
        // HttpConnectionParams.setSocketBufferSize(basicparms, ???);
        // HttpConnectionParams.setStaleCheckingEnabled(basicparms, false);
        HttpConnectionParams.setConnectionTimeout(basicparms, HttpClientRequestHandler.CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(basicparms, HttpClientRequestHandler.SO_TIMEOUT);
        // HttpConnectionParams.setTcpNoDelay(basicparms, true);

        final ClientConnectionManager cm = new ThreadSafeClientConnManager(basicparms, registry);

        HttpClientRequestHandler.HttpClient = new DefaultHttpClient(cm, basicparms);
    }//from  w  ww.  j a v a2 s  .  com
    return HttpClientRequestHandler.HttpClient;
}

From source file:com.archivas.clienttools.arcutils.impl.adapter.HCAPAdapter.java

/**
 * Setup default http client parameters including timeouts, number of simultaneous connections,
 * and a retry handler//w  w w. j a  va  2 s.c o m
 * 
 * @param sslExceptionCallback
 */
protected synchronized final void init(SSLCertificateCallback sslExceptionCallback)
        throws StorageAdapterException {

    if (httpClient != null) {
        LOG.log(Level.FINE, "Reinitializing...why?");
        return;
    }

    HttpParams params = new BasicHttpParams();

    // Right now we just set this up for normal load. This means when we go to low load it is
    // possible we will
    // be accessing more connections on HCP than what is set there. When we upgrade to the next
    // apache library
    // we can implement that functionality see: @updateHttpClient
    int maxConnectionsPerRoute = HCPMoverProperties.MAX_LOAD_MAXTHREADS.getAsInt();
    int maxConnections = HCPMoverProperties.MAX_LOAD_MAXTHREADS_PER_NODE.getAsInt();

    try {
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(maxConnectionsPerRoute);
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
    } catch (NumberFormatException e) {
        /* Already logged by helper */ }

    try {
        ConnManagerParams.setMaxTotalConnections(params, maxConnections);
    } catch (NumberFormatException e) {
        /* Already logged by helper */ }

    // We need a timeout so we don't just hang there forever
    int connectionTimeoutMillis = HCPMoverProperties.CONNECTION_TIMEOUT_MILLIS.getAsInt();
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);

    // And a socket timeout from properties
    int socketTimeoutMillis = HCPMoverProperties.SOCKET_TIMEOUT_MILLIS.getAsInt();
    HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);

    // This is the magic that makes windows reuse sockets.
    HttpConnectionParams.setLinger(params, 1);

    // 302's from HCP redirect -- prevent this
    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);

    try {
        SchemeRegistry schemeRegistry = getHcapProtocolSchemeRegistryForHttpClient(sslExceptionCallback);

        ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(params, schemeRegistry);

        // debug start
        // TestHttpThreadSafeClientConnManager connMgr = new
        // TestHttpThreadSafeClientConnManager(params, schemeRegistry);
        // debug end

        httpClient = new DefaultHttpClient(connMgr, params);
        LOG.log(Level.FINE, "Created new httpClient for profile: " + getProfile());

    } catch (Exception e) {
        LOG.log(Level.INFO, "Unable to initialize HCAPHttpAdapter!", e);
        throw new StorageAdapterLiteralException("Unable to initialize HCAPHttpAdapter!", e);
    }

}