Example usage for org.apache.http.conn.params ConnManagerPNames TIMEOUT

List of usage examples for org.apache.http.conn.params ConnManagerPNames TIMEOUT

Introduction

In this page you can find the example usage for org.apache.http.conn.params ConnManagerPNames TIMEOUT.

Prototype

String TIMEOUT

To view the source code for org.apache.http.conn.params ConnManagerPNames TIMEOUT.

Click Source Link

Document

Defines the timeout in milliseconds used when retrieving an instance of org.apache.http.conn.ManagedClientConnection from the org.apache.http.conn.ClientConnectionManager .

Usage

From source file:Main.java

public static HttpClient getHttpClient() {

    if (httpClient != null) {
        return httpClient;
    }//from   ww w .  jav  a  2 s.  c o  m

    synchronized (HTTP_CLIENT) {
        HttpParams defaultParams = new BasicHttpParams();
        defaultParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
        defaultParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 6000);
        defaultParams.setLongParameter(ConnManagerPNames.TIMEOUT, 6000);

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

        ClientConnectionManager connMgr = new ThreadSafeClientConnManager(defaultParams, scheme);

        httpClient = new DefaultHttpClient(connMgr, defaultParams);
    }

    return httpClient;
}

From source file:org.graphity.core.util.jena.HttpQuery.java

private InputStream execGet() throws QueryExceptionHTTP {
    URL target = null;/*from   w w  w.ja  v a2 s  .  c  o  m*/
    String qs = getQueryString();

    ARQ.getHttpRequestLogger().trace(qs);

    try {
        if (count() == 0)
            target = new URL(serviceURL);
        else
            target = new URL(serviceURL + (serviceParams ? "&" : "?") + qs);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("GET " + target.toExternalForm());

    try {
        try {
            this.client = new SystemDefaultHttpClient();

            // Always apply a 10 second timeout to obtaining a connection lease from HTTP Client
            // This prevents a potential lock up
            this.client.getParams().setLongParameter(ConnManagerPNames.TIMEOUT, TimeUnit.SECONDS.toMillis(10));

            // If user has specified time outs apply them now
            if (this.connectTimeout > 0)
                this.client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                        this.connectTimeout);
            if (this.readTimeout > 0)
                this.client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, this.readTimeout);

            // Enable compression support appropriately
            HttpContext context = new BasicHttpContext();
            if (allowGZip || allowDeflate) {
                // Apply auth early as the decompressing client we're about
                // to add will block this being applied later
                HttpOp.applyAuthentication((AbstractHttpClient) client, serviceURL, context, authenticator);
                client = new DecompressingHttpClient(client);
            }

            // Get the actual response stream
            TypedInputStream stream = HttpOp.execHttpGet(target.toString(), contentTypeResult, client, context,
                    this.authenticator);
            if (stream == null)
                throw new QueryExceptionHTTP(404);
            return execCommon(stream);
        } catch (HttpException httpEx) {
            // Back-off and try POST if something complain about long URIs
            if (httpEx.getResponseCode() == 414)
                return execPost();
            throw httpEx;
        }
    } catch (HttpException httpEx) {
        // Unwrap and re-wrap the HTTP exception
        responseCode = httpEx.getResponseCode();
        throw new QueryExceptionHTTP(responseCode, "Error making the query, see cause for details",
                httpEx.getCause());
    }
}

From source file:com.hp.hpl.jena.sparql.engine.http.HttpQuery.java

private InputStream execGet() throws QueryExceptionHTTP {
    URL target = null;/*w w w. java 2 s.c om*/
    String qs = getQueryString();

    ARQ.getHttpRequestLogger().trace(qs);

    try {
        if (count() == 0)
            target = new URL(serviceURL);
        else
            target = new URL(serviceURL + (serviceParams ? "&" : "?") + qs);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("GET " + target.toExternalForm());

    try {
        try {
            // Select the appropriate HttpClient to use
            this.selectClient();

            // Always apply a 10 second timeout to obtaining a connection lease from HTTP Client
            // This prevents a potential lock up
            this.client.getParams().setLongParameter(ConnManagerPNames.TIMEOUT, TimeUnit.SECONDS.toMillis(10));

            // If user has specified time outs apply them now
            if (this.connectTimeout > 0)
                this.client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                        this.connectTimeout);
            if (this.readTimeout > 0)
                this.client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, this.readTimeout);

            // Enable compression support appropriately
            HttpContext context = new BasicHttpContext();
            if (allowGZip || allowDeflate) {
                // Apply auth early as the decompressing client we're about
                // to add will block this being applied later
                HttpOp.applyAuthentication((AbstractHttpClient) client, serviceURL, context, authenticator);
                client = new DecompressingHttpClient(client);
            }

            // Get the actual response stream
            TypedInputStream stream = HttpOp.execHttpGet(target.toString(), contentTypeResult, client, context,
                    this.authenticator);
            if (stream == null)
                throw new QueryExceptionHTTP(404);
            return execCommon(stream);
        } catch (HttpException httpEx) {
            // Back-off and try POST if something complain about long URIs
            if (httpEx.getResponseCode() == 414)
                return execPost();
            throw httpEx;
        }
    } catch (HttpException httpEx) {
        throw rewrap(httpEx);
    }
}

From source file:com.google.apphosting.vmruntime.VmApiProxyDelegate.java

/**
 * Create an HTTP post request suitable for sending to the API server.
 *
 * @param environment The current VMApiProxyEnvironment
 * @param packageName The API call package
 * @param methodName The API call method
 * @param requestData The POST payload./*from  www.j a  v a  2  s .  c  om*/
 * @param timeoutMs The timeout for this request
 * @return an HttpPost object to send to the API.
 */
// 
static HttpPost createRequest(VmApiProxyEnvironment environment, String packageName, String methodName,
        byte[] requestData, int timeoutMs) {
    // Wrap the payload in a RemoteApi Request.
    RemoteApiPb.Request remoteRequest = new RemoteApiPb.Request();
    remoteRequest.setServiceName(packageName);
    remoteRequest.setMethod(methodName);
    remoteRequest.setRequestId(environment.getTicket());
    remoteRequest.setRequestAsBytes(requestData);

    HttpPost request = new HttpPost("http://" + environment.getServer() + REQUEST_ENDPOINT);
    request.setHeader(RPC_STUB_ID_HEADER, REQUEST_STUB_ID);
    request.setHeader(RPC_METHOD_HEADER, REQUEST_STUB_METHOD);

    // Set TCP connection timeouts.
    HttpParams params = new BasicHttpParams();
    params.setLongParameter(ConnManagerPNames.TIMEOUT, timeoutMs + ADDITIONAL_HTTP_TIMEOUT_BUFFER_MS);
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            timeoutMs + ADDITIONAL_HTTP_TIMEOUT_BUFFER_MS);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeoutMs + ADDITIONAL_HTTP_TIMEOUT_BUFFER_MS);

    // Performance tweaks.
    params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, Boolean.TRUE);
    params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, Boolean.FALSE);
    request.setParams(params);

    // The request deadline can be overwritten by the environment, read deadline if available.
    Double deadline = (Double) (environment.getAttributes().get(API_DEADLINE_KEY));
    if (deadline == null) {
        request.setHeader(RPC_DEADLINE_HEADER,
                Double.toString(TimeUnit.SECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS)));
    } else {
        request.setHeader(RPC_DEADLINE_HEADER, Double.toString(deadline));
    }

    // If the incoming request has a dapper trace header: set it on outgoing API calls
    // so they are tied to the original request.
    Object dapperHeader = environment.getAttributes()
            .get(VmApiProxyEnvironment.AttributeMapping.DAPPER_ID.attributeKey);
    if (dapperHeader instanceof String) {
        request.setHeader(VmApiProxyEnvironment.AttributeMapping.DAPPER_ID.headerKey, (String) dapperHeader);
    }

    // If the incoming request has a Cloud trace header: set it on outgoing API calls
    // so they are tied to the original request.
    // TODO(user): For now, this uses the incoming span id - use the one from the active span.
    Object traceHeader = environment.getAttributes()
            .get(VmApiProxyEnvironment.AttributeMapping.CLOUD_TRACE_CONTEXT.attributeKey);
    if (traceHeader instanceof String) {
        request.setHeader(VmApiProxyEnvironment.AttributeMapping.CLOUD_TRACE_CONTEXT.headerKey,
                (String) traceHeader);
    }

    ByteArrayEntity postPayload = new ByteArrayEntity(remoteRequest.toByteArray(),
            ContentType.APPLICATION_OCTET_STREAM);
    postPayload.setChunked(false);
    request.setEntity(postPayload);

    return request;
}

From source file:org.graphity.core.util.jena.HttpQuery.java

private InputStream execPost() throws QueryExceptionHTTP {
    URL target = null;//from  w  w  w.  ja v a  2 s .  c o m
    try {
        target = new URL(serviceURL);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("POST " + target.toExternalForm());

    ARQ.getHttpRequestLogger().trace(target.toExternalForm());

    try {
        this.client = new SystemDefaultHttpClient();

        // Always apply a 10 second timeout to obtaining a connection lease from HTTP Client
        // This prevents a potential lock up
        this.client.getParams().setLongParameter(ConnManagerPNames.TIMEOUT, TimeUnit.SECONDS.toMillis(10));

        // If user has specified time outs apply them now
        if (this.connectTimeout > 0)
            this.client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                    this.connectTimeout);
        if (this.readTimeout > 0)
            this.client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, this.readTimeout);

        // Enable compression support appropriately
        HttpContext context = new BasicHttpContext();
        if (allowGZip || allowDeflate) {
            // Apply auth early as the decompressing client we're about
            // to add will block this being applied later
            HttpOp.applyAuthentication((AbstractHttpClient) client, serviceURL, context, authenticator);
            this.client = new DecompressingHttpClient(client);
        }

        // Get the actual response stream
        TypedInputStream stream = HttpOp.execHttpPostFormStream(serviceURL, this, contentTypeResult, client,
                context, authenticator);
        if (stream == null)
            throw new QueryExceptionHTTP(404);
        return execCommon(stream);
    } catch (HttpException httpEx) {
        // Unwrap and re-wrap the HTTP Exception
        responseCode = httpEx.getResponseCode();
        throw new QueryExceptionHTTP(responseCode, "Error making the query, see cause for details",
                httpEx.getCause());
    }
}

From source file:com.hp.hpl.jena.sparql.engine.http.HttpQuery.java

private InputStream execPost() throws QueryExceptionHTTP {
    URL target = null;/*from w w w  .  j a va  2  s .co m*/
    try {
        target = new URL(serviceURL);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("POST " + target.toExternalForm());

    ARQ.getHttpRequestLogger().trace(target.toExternalForm());

    try {
        // Select the appropriate HttpClient to use
        this.selectClient();

        // Always apply a 10 second timeout to obtaining a connection lease from HTTP Client
        // This prevents a potential lock up
        this.client.getParams().setLongParameter(ConnManagerPNames.TIMEOUT, TimeUnit.SECONDS.toMillis(10));

        // If user has specified time outs apply them now
        if (this.connectTimeout > 0)
            this.client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                    this.connectTimeout);
        if (this.readTimeout > 0)
            this.client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, this.readTimeout);

        // Enable compression support appropriately
        HttpContext context = new BasicHttpContext();
        if (allowGZip || allowDeflate) {
            // Apply auth early as the decompressing client we're about
            // to add will block this being applied later
            HttpOp.applyAuthentication((AbstractHttpClient) client, serviceURL, context, authenticator);
            this.client = new DecompressingHttpClient(client);
        }

        // Get the actual response stream
        TypedInputStream stream = HttpOp.execHttpPostFormStream(serviceURL, this, contentTypeResult, client,
                context, authenticator);
        if (stream == null)
            throw new QueryExceptionHTTP(404);
        return execCommon(stream);
    } catch (HttpException httpEx) {
        throw rewrap(httpEx);
    }
}