Java Code Examples for org.apache.http.impl.client.DefaultHttpClient.setHttpRequestRetryHandler()

The following are Jave code examples for showing how to use setHttpRequestRetryHandler() of the org.apache.http.impl.client.DefaultHttpClient class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: aos-MediaLib   File: NetworkResourceLoader.java   View Source Code Vote up 5 votes
/**
 * Create a thread-safe client. This client does not do redirecting, to allow us to capture
 * correct "error" codes.
 *
 * @return HttpClient
 */
public final DefaultHttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);  
    HttpProtocolParams.setUseExpectContinue(params, true);  
    // Turn off stale checking. Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    // Default connection and socket timeout of 30 seconds. Tweak to taste.
    HttpConnectionParams.setConnectionTimeout(params, 10*1000);
    HttpConnectionParams.setSoTimeout(params, 20*1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    
    ConnManagerParams.setTimeout(params, 5 * 1000);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(50));
    ConnManagerParams.setMaxTotalConnections(params, 200);
    
    // Sets up the http part of the service.
    final SchemeRegistry supportedSchemes = new SchemeRegistry();

    // Register the "http" protocol scheme, it is required
    // by the default operator to look up socket factories.
    final SocketFactory sf = PlainSocketFactory.getSocketFactory();
    supportedSchemes.register(new Scheme("http", sf, 80));
    supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));  
    final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(params,
            supportedSchemes);
    
    DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
    
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));
    
    return httpClient;
}
 
Example 2
Project: rainbow   File: HttpFactory.java   View Source Code Vote up 4 votes
public HttpClient getHttpClient()
{
    DefaultHttpClient client = new DefaultHttpClient(cm);
    Integer socketTimeout = 10000;
    Integer connectionTimeout = 10000;
    final int retryTime = 3;
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
    client.getParams().setParameter(CoreConnectionPNames.TCP_NODELAY, false);
    client.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 1024 * 1024);
    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler()
    {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context)
        {
            if (executionCount >= retryTime)
            {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof InterruptedIOException)
            {
                // Timeout
                return false;
            }
            if (exception instanceof UnknownHostException)
            {
                // Unknown host
                return false;
            }
            if (exception instanceof ConnectException)
            {
                // Connection refused
                return false;
            }
            if (exception instanceof SSLException)
            {
                // SSL handshake exception
                return false;
            }
            HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent)
            {
                // Retry if the request is considered idempotent
                return true;
            }
            return false;
        }

    };
    client.setHttpRequestRetryHandler(myRetryHandler);
    return client;
}