use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue36 method retryListener_WithExceptions_ShouldBeCalled.
@Test
public void retryListener_WithExceptions_ShouldBeCalled() throws Exception {
RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
AtomicInteger listenerCallbacks = new AtomicInteger();
try {
Failsafe.<Boolean>with(policy).onRetry((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> {
throw new RuntimeException();
});
} catch (RuntimeException e) {
}
assertEquals(listenerCallbacks.get(), 3);
}
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 ddf by codice.
the class TemporaryFileBackedOutputStream method reset.
/**
* Reset fileBackedOutputStream and retry if it fails.
*/
@SuppressWarnings("unchecked")
private void reset() {
RetryPolicy retryPolicy = new RetryPolicy().retryOn(IOException.class).withBackoff(INITIAL_RETRY_SLEEP, MAX_DELAY, INITIAL_RETRY_SLEEP_UNIT).withMaxRetries(MAX_RETRY_ATTEMPTS);
Failsafe.with(retryPolicy).onFailedAttempt(throwable -> LOGGER.debug("failed to delete temporary file, will retry", throwable)).onFailure(throwable -> LOGGER.debug("failed to delete temporary file", throwable)).run(fileBackedOutputStream::reset);
}
Aggregations