Search in sources :

Example 21 with Retry

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);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 22 with Retry

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);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 23 with Retry

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());
    }
}
Also used : RetryHandler(org.apache.servicecomb.governance.handler.RetryHandler) Retry(io.github.resilience4j.retry.Retry)

Example 24 with Retry

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;
    }
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Exceptions.toConsumerResponse(org.apache.servicecomb.core.exception.Exceptions.toConsumerResponse) RetryHandler(org.apache.servicecomb.governance.handler.RetryHandler) Retry(io.github.resilience4j.retry.Retry)

Example 25 with Retry

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());
}
Also used : Logger(org.slf4j.Logger) Retry(io.github.resilience4j.retry.Retry) RetryConfig(io.github.resilience4j.retry.RetryConfig) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) IntervalFunction(io.github.resilience4j.core.IntervalFunction) RetryExtension(org.apache.servicecomb.governance.handler.ext.RetryExtension) GovernanceUtils(org.apache.servicecomb.governance.utils.GovernanceUtils) Component(org.springframework.stereotype.Component) GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) Duration(java.time.Duration) RetryProperties(org.apache.servicecomb.governance.properties.RetryProperties) RetryRegistry(io.github.resilience4j.retry.RetryRegistry) RetryPolicy(org.apache.servicecomb.governance.policy.RetryPolicy) RetryConfig(io.github.resilience4j.retry.RetryConfig) RetryRegistry(io.github.resilience4j.retry.RetryRegistry)

Aggregations

Retry (io.github.resilience4j.retry.Retry)39 Test (org.junit.Test)34 WebServiceException (javax.xml.ws.WebServiceException)29 RetryConfig (io.github.resilience4j.retry.RetryConfig)17 CheckedRunnable (io.vavr.CheckedRunnable)9 RetryRegistry (io.github.resilience4j.retry.RetryRegistry)6 IOException (java.io.IOException)4 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)2 HelloWorldService (io.github.resilience4j.test.HelloWorldService)2 API (io.vavr.API)2 Predicates (io.vavr.Predicates)2 Try (io.vavr.control.Try)2 Duration (java.time.Duration)2 RetryHandler (org.apache.servicecomb.governance.handler.RetryHandler)2 Before (org.junit.Before)2 BDDMockito (org.mockito.BDDMockito)2 Mockito (org.mockito.Mockito)2 RxJava2Adapter.toFlowable (io.github.resilience4j.adapter.RxJava2Adapter.toFlowable)1 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)1 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)1