use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue55 method shouldOnlyFallbackOnFailure.
public void shouldOnlyFallbackOnFailure() throws Throwable {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
AtomicInteger counter = new AtomicInteger();
Failsafe.with(new RetryPolicy()).with(executor).withFallback(() -> counter.incrementAndGet()).get(() -> null);
Thread.sleep(100);
assertEquals(counter.get(), 0);
Failsafe.with(new RetryPolicy().withMaxRetries(1)).with(executor).withFallback(() -> counter.incrementAndGet()).run(() -> {
throw new RuntimeException();
});
Thread.sleep(100);
assertEquals(counter.get(), 1);
}
use of net.jodah.failsafe.RetryPolicy in project ddf by codice.
the class HttpSolrClientFactory method getHttpSolrClient.
/**
* Creates a new {@link HttpSolrClient} using the URL, core name and configuration file name
* provided.
*
* @param url Solr server URL. If {@code null}, defaults to the system's base URL
* followed by {@code /solr}.
* @param coreName name of the Solr core to create
* @param configFile configuration file name. If {@code null}, defaults to
* {@value #DEFAULT_SOLRCONFIG_XML}.
* @return {@code Future} used to retrieve the new {@link HttpSolrClient} instance
*/
public static Future<SolrClient> getHttpSolrClient(@Nullable String url, String coreName, @Nullable String configFile) {
String solrUrl = StringUtils.defaultIfBlank(url, SystemBaseUrl.constructUrl("/solr"));
String coreUrl = url + "/" + coreName;
if (System.getProperty("solr.data.dir") != null) {
ConfigurationStore.getInstance().setDataDirectoryPath(System.getProperty("solr.data.dir"));
}
RetryPolicy retryPolicy = new RetryPolicy().withBackoff(10, TimeUnit.MINUTES.toMillis(1), TimeUnit.MILLISECONDS);
return Failsafe.with(retryPolicy).with(executor).onRetry((c, failure, ctx) -> LOGGER.debug("Attempt {} failed to create HTTP Solr client ({}). Retrying again.", ctx.getExecutions(), coreName)).onFailedAttempt(failure -> LOGGER.debug("Attempt failed to create HTTP Solr client (" + coreName + ")", failure)).onSuccess(client -> LOGGER.debug("Successfully created HTTP Solr client ({})", coreName)).onFailure(failure -> LOGGER.warn("All attempts failed to create HTTP Solr client (" + coreName + ")", failure)).get(() -> createSolrHttpClient(solrUrl, coreName, configFile, coreUrl));
}
use of net.jodah.failsafe.RetryPolicy in project divolte-collector by divolte.
the class GoogleCloudStorageRetryConfiguration method createRetryPolicy.
public RetryPolicy createRetryPolicy() {
final RetryPolicy policyBeforeJitter = new RetryPolicy().withMaxRetries(maxAttempts - 1).withMaxDuration(totalTimeout.toNanos(), TimeUnit.NANOSECONDS).withBackoff(initialRetryDelay.toNanos(), maxRetryDelay.toNanos(), TimeUnit.NANOSECONDS, retryDelayMultiplier);
final RetryPolicy policyWithJitterFactor = jitterFactor.map(policyBeforeJitter::withJitter).orElse(policyBeforeJitter);
return jitterDelay.map(d -> policyWithJitterFactor.withJitter(d.toNanos(), TimeUnit.NANOSECONDS)).orElse(policyWithJitterFactor);
}
use of net.jodah.failsafe.RetryPolicy in project alliance by codice.
the class CatalogUpdateRetry method submitUpdateRequestWithRetry.
public void submitUpdateRequestWithRetry(CatalogFramework catalogFramework, UpdateRequest updateRequest, long initialSleepSeconds, long initialRetryWaitMilliseconds, long maxRetryMilliseconds, Consumer<Update> updateConsumer) {
if (sleep(TimeUnit.SECONDS.toMillis(initialSleepSeconds))) {
return;
}
RetryPolicy retryPolicy = new RetryPolicy().retryOn(IngestException.class, SourceUnavailableException.class).withBackoff(initialRetryWaitMilliseconds, maxRetryMilliseconds, TimeUnit.MILLISECONDS);
Failsafe.with(retryPolicy).onFailedAttempt(throwable -> LOGGER.debug("failed to update catalog, will retry: updateRequest={}", updateRequest)).onFailure(throwable -> LOGGER.debug("failed to update catalog: updateRequest={}", updateRequest)).run(() -> catalogFramework.update(updateRequest).getUpdatedMetacards().forEach(updateConsumer));
}
use of net.jodah.failsafe.RetryPolicy in project ddf by codice.
the class ContentProducerDataAccessObject method waitForAvailableSource.
private void waitForAvailableSource(CatalogFramework catalogFramework) throws SourceUnavailableException {
RetryPolicy retryPolicy = new RetryPolicy().withDelay(3, TimeUnit.SECONDS).withMaxDuration(3, TimeUnit.MINUTES).retryIf((Predicate<Set>) Set::isEmpty).retryIf((Set<SourceDescriptor> result) -> !result.stream().findFirst().get().isAvailable());
Failsafe.with(retryPolicy).get(() -> catalogFramework.getSourceInfo(new SourceInfoRequestLocal(false)).getSourceInfo());
}
Aggregations