use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class RetryTransformerTest method shouldReturnOnCompleteUsingObservable.
@Test
public void shouldReturnOnCompleteUsingObservable() {
// Given
RetryConfig config = RetryConfig.ofDefaults();
Retry retry = Retry.of("testName", config);
RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// When
Observable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
Observable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
// Then
BDDMockito.then(helloWorldService).should(Mockito.times(6)).returnHelloWorld();
Retry.Metrics metrics = retry.getMetrics();
assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class RetryTransformerTest method shouldReturnOnCompleteUsingFlowable.
@Test
public void shouldReturnOnCompleteUsingFlowable() {
// Given
RetryConfig config = RetryConfig.ofDefaults();
Retry retry = Retry.of("testName", config);
RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// When
Flowable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
Flowable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
// Then
BDDMockito.then(helloWorldService).should(Mockito.times(6)).returnHelloWorld();
Retry.Metrics metrics = retry.getMetrics();
assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
use of io.github.resilience4j.retry.Retry in project java-chassis by ServiceComb.
the class InvokerUtils method decorateReactiveRetry.
private static void decorateReactiveRetry(Invocation invocation, DecorateCompletionStage<Response> dcs, GovernanceRequest request) {
// governance implementations.
RetryHandler retryHandler = BeanUtils.getBean(RetryHandler.class);
Retry retry = retryHandler.getActuator(request);
if (retry != null) {
dcs.withRetry(retry, getOrCreateRetryPool());
}
if (isCompatibleRetryEnabled(invocation)) {
// compatible implementation for retry in load balance module in old versions.
retry = getOrCreateCompatibleRetry(invocation);
dcs.withRetry(retry, getOrCreateRetryPool());
}
}
use of io.github.resilience4j.retry.Retry in project java-chassis by ServiceComb.
the class InvokerUtils method decorateSyncRetry.
private static Response decorateSyncRetry(Invocation invocation, GovernanceRequest request) {
try {
// governance implementations.
RetryHandler retryHandler = BeanUtils.getBean(RetryHandler.class);
Retry retry = retryHandler.getActuator(request);
if (retry != null) {
CheckedFunction0<Response> supplier = Retry.decorateCheckedSupplier(retry, () -> innerSyncInvokeImpl(invocation));
return Try.of(supplier).get();
}
if (isCompatibleRetryEnabled(invocation)) {
// compatible implementation for retry in load balance module in old versions.
retry = getOrCreateCompatibleRetry(invocation);
CheckedFunction0<Response> supplier = Retry.decorateCheckedSupplier(retry, () -> innerSyncInvokeImpl(invocation));
return Try.of(supplier).get();
}
// retry not enabled
return innerSyncInvokeImpl(invocation);
} catch (Throwable e) {
String message = String.format("invoke failed, operation %s, trace id %s", invocation.getMicroserviceQualifiedName(), invocation.getTraceId());
LOGGER.error(message, e);
Response response = Response.createConsumerFail(e, message);
invocation.onFinish(response);
return response;
}
}
use of io.github.resilience4j.retry.Retry in project java-chassis by ServiceComb.
the class RetryHandler method getRetry.
private Retry getRetry(RetryPolicy retryPolicy) {
LOGGER.info("applying new policy: {}", retryPolicy.toString());
RetryConfig config = RetryConfig.custom().maxAttempts(retryPolicy.getMaxAttempts() + 1).retryOnResult(response -> retryExtension.isRetry(retryPolicy.getRetryOnResponseStatus(), response)).retryOnException(e -> retryExtension.isRetry(e)).intervalFunction(getIntervalFunction(retryPolicy)).build();
RetryRegistry registry = RetryRegistry.of(config);
return registry.retry(retryPolicy.getName());
}
Aggregations