Example usage for org.apache.http.impl.client DefaultHttpRequestRetryHandler DefaultHttpRequestRetryHandler

List of usage examples for org.apache.http.impl.client DefaultHttpRequestRetryHandler DefaultHttpRequestRetryHandler

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpRequestRetryHandler DefaultHttpRequestRetryHandler.

Prototype

public DefaultHttpRequestRetryHandler() 

Source Link

Document

Create the request retry handler with a retry count of 3, requestSentRetryEnabled false and using the following list of non-retriable IOException classes:
  • InterruptedIOException
  • UnknownHostException
  • ConnectException
  • SSLException

Usage

From source file:com.microsoft.windowsazure.management.scheduler.SchedulerIntegrationTestBase.java

protected static void createSchedulerService(final String cloudServiceName, final String jobCollectionName)
        throws Exception {
    Configuration config = createConfiguration(cloudServiceName, jobCollectionName);
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());
    schedulerClient = SchedulerService.create(config);
    addClient((ServiceClient<?>) schedulerClient, new Callable<Void>() {
        @Override/*from ww  w. j  a v a2 s .c o  m*/
        public Void call() throws Exception {
            createSchedulerService(cloudServiceName, jobCollectionName);
            return null;
        }
    });
}

From source file:com.microsoft.windowsazure.management.mediaservices.MediaServiceManagementIntegrationTestBase.java

protected static void createStorageManagementClient() throws Exception {
    Configuration config = createConfiguration();
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());
    storageManagementClient = StorageManagementService.create(config);
    addClient((ServiceClient<?>) storageManagementClient, new Callable<Void>() {
        @Override//from  w  w  w  .  j  a  va2 s  .c  o  m
        public Void call() throws Exception {
            createStorageManagementClient();
            return null;
        }
    });
}

From source file:com.microsoft.windowsazure.management.scheduler.SchedulerIntegrationTestBase.java

protected static void createCloudServiceManagementService() throws Exception {
    Configuration config = createConfiguration();
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());
    cloudServiceManagementClient = CloudServiceManagementService.create(config);
    addClient((ServiceClient<?>) cloudServiceManagementClient, new Callable<Void>() {
        @Override//from   w  w w  .j a  v  a  2s. c om
        public Void call() throws Exception {
            createCloudServiceManagementService();
            return null;
        }
    });
}

From source file:sk.datalan.solr.impl.HttpClientUtil.java

public static HttpClientBuilder configureClient(final HttpClientConfiguration config) {
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    // max total connections
    if (config.isSetMaxConnections()) {
        clientBuilder.setMaxConnTotal(config.getMaxConnections());
    }/*from   w ww .ja v  a  2  s  .  c  o m*/

    // max connections per route
    if (config.isSetMaxConnectionsPerRoute()) {
        clientBuilder.setMaxConnPerRoute(config.getMaxConnectionsPerRoute());
    }

    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH)
            .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true);

    // connection timeout
    if (config.isSetConnectionTimeout()) {
        requestConfigBuilder.setConnectTimeout(config.getConnectionTimeout());
    }

    // soucket timeout
    if (config.isSetSocketTimeout()) {
        requestConfigBuilder.setSocketTimeout(config.getSocketTimeout());
    }

    // soucket timeout
    if (config.isSetFollowRedirects()) {
        requestConfigBuilder.setRedirectsEnabled(config.getFollowRedirects());
    }
    clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

    if (config.isSetUseRetry()) {
        if (config.getUseRetry()) {
            clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
        } else {
            clientBuilder.setRetryHandler(NO_RETRY);
        }
    }

    // basic authentication
    if (config.isSetBasicAuthUsername() && config.isSetBasicAuthPassword()) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(config.getBasicAuthUsername(), config.getBasicAuthPassword()));
    }

    if (config.isSetAllowCompression()) {
        clientBuilder.addInterceptorFirst(new UseCompressionRequestInterceptor());
        clientBuilder.addInterceptorFirst(new UseCompressionResponseInterceptor());
    }

    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    SSLContext sslcontext = SSLContexts.createSystemDefault();
    // Use custom hostname verifier to customize SSL hostname verification.
    X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier();

    // Create a registry of custom connection socket factories for supported
    // protocol schemes.
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslcontext, hostnameVerifier)).build();

    clientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry));

    return clientBuilder;
}

From source file:com.microsoft.windowsazure.management.sql.SqlManagementIntegrationTestBase.java

protected static void createService() throws Exception {
    // reinitialize configuration from known state
    Configuration config = createConfiguration();
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());

    sqlManagementClient = SqlManagementService.create(config);
    addClient((ServiceClient<?>) sqlManagementClient, new Callable<Void>() {
        @Override/*from  w  w  w.j  a  va 2s  .  c o m*/
        public Void call() throws Exception {
            createService();
            return null;
        }
    });
}

From source file:com.microsoft.windowsazure.management.mediaservices.MediaServiceManagementIntegrationTestBase.java

protected static void createManagementClient() throws Exception {
    Configuration config = createConfiguration();
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());
    managementClient = ManagementService.create(config);
    addClient((ServiceClient<?>) managementClient, new Callable<Void>() {
        @Override//  w w w .  java  2s. co m
        public Void call() throws Exception {
            createManagementClient();
            return null;
        }
    });
}

From source file:com.microsoft.windowsazure.management.compute.ComputeManagementIntegrationTestBase.java

protected static void createManagementClient() throws Exception {
    Configuration config = createConfiguration();
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());

    managementClient = ManagementService.create(config);
    addClient((ServiceClient<?>) managementClient, new Callable<Void>() {
        @Override/*from  w w w  .  ja va  2s . co  m*/
        public Void call() throws Exception {
            createManagementClient();
            return null;
        }
    });
}

From source file:com.android.providers.downloads.OmaDownload.java

/**
* This method notifies the server for the status of the download operation.
* It sends a status report to a Web server if installNotify attribute is specified in the download descriptor.
@param  component   the component that contains attributes in the descriptor.
@param  handler     the handler used to send and process messages. A message
                indicates whether the media object is available to the user
                or not (READY or DISCARD).
*//*  ww  w. jav  a 2  s  .  c om*/
//protected static void installNotify (OmaDescription component, Handler handler) {
protected static int installNotify(OmaDescription component, Handler handler) {

    int ack = -1;
    int release = OmaStatusHandler.DISCARD;
    URL url = component.getInstallNotifyUrl();

    if (url != null) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(url.toString());

        try {
            HttpParams params = postRequest.getParams();
            HttpProtocolParams.setUseExpectContinue(params, false);
            postRequest.setEntity(
                    new StringEntity(OmaStatusHandler.statusCodeToString(component.getStatusCode()) + "\n\r"));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                // TODO Auto-generated method stub
                Log.i("@M_" + Constants.LOG_OMA_DL, "Retry the request...");
                return (executionCount <= OmaStatusHandler.MAXIMUM_RETRY);
            }
        });

        try {
            HttpResponse response = client.execute(postRequest);

            if (response.getStatusLine() != null) {
                ack = response.getStatusLine().getStatusCode();

                //200-series response code
                if (ack == HttpStatus.SC_OK || ack == HttpStatus.SC_ACCEPTED || ack == HttpStatus.SC_CREATED
                        || ack == HttpStatus.SC_MULTI_STATUS
                        || ack == HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION || ack == HttpStatus.SC_NO_CONTENT
                        || ack == HttpStatus.SC_PARTIAL_CONTENT || ack == HttpStatus.SC_RESET_CONTENT) {
                    if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
                        release = OmaStatusHandler.READY;
                    }
                }

                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        inputStream = entity.getContent();
                        if (inputStream != null) {
                            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

                            String s;
                            while ((s = br.readLine()) != null) {
                                Log.v("@M_" + Constants.LOG_OMA_DL, "Response: " + s);
                            }
                        }

                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
            }
        } catch (ConnectTimeoutException e) {
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
            postRequest.abort();
            //After time out period, the client releases the media object for use.
            if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
                release = OmaStatusHandler.READY;
            }
        } catch (NoHttpResponseException e) {
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
            postRequest.abort();
            //After time out period, the client releases the media object for use.
            if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
                release = OmaStatusHandler.READY;
            }
        } catch (IOException e) {
            Log.e("@M_" + Constants.LOG_OMA_DL, e.toString());
            postRequest.abort();
        }
        if (client != null) {
            client.getConnectionManager().shutdown();
        }
    } else {
        if (component.getStatusCode() == OmaStatusHandler.SUCCESS) {
            release = OmaStatusHandler.READY;
        }
    }

    if (handler != null) {
        Message mg = Message.obtain();
        mg.arg1 = release;
        handler.sendMessage(mg);
    }
    return release;
}

From source file:com.microsoft.azure.utility.compute.ComputeTestBase.java

protected static void createResourceManagementClient() throws Exception {
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());

    resourceManagementClient = ResourceManagementService.create(config);
    if (IS_MOCKED) {
        resourceManagementClient.setLongRunningOperationInitialTimeout(0);
        resourceManagementClient.setLongRunningOperationRetryTimeout(0);
    }/*from  w  ww  .j av a  2  s  .  co  m*/
    addClient((ServiceClient<?>) resourceManagementClient, new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            createResourceManagementClient();
            return null;
        }
    });
}