Search in sources :

Example 16 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RetryChain method execute.

@Override
public void execute(Chain chain) throws Exception {
    String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getRetries().getPath();
    chain.prefix(prefix, chain1 -> {
        chain1.get("events", ctx -> Promise.<RetryEventsEndpointResponse>async(d -> {
            List<RetryEventDTO> eventsList = eventConsumerRegistry.getAllEventConsumer().flatMap(CircularEventConsumer::getBufferedEvents).sorted(Comparator.comparing(RetryEvent::getCreationTime)).map(RetryEventDTO::createRetryEventDTO).toJavaList();
            d.success(new RetryEventsEndpointResponse(eventsList));
        }).then(r -> ctx.render(Jackson.json(r))));
        chain1.get("stream/events", ctx -> {
            Seq<Flowable<RetryEvent>> eventStreams = retryRegistry.getAllRetries().map(retry -> toFlowable(retry.getEventPublisher()));
            Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(Flowable.merge(eventStreams), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
        chain1.get("events/:name", ctx -> {
            String retryName = ctx.getPathTokens().get("name");
            Promise.<RetryEventsEndpointResponse>async(d -> {
                List<RetryEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(retryName).getBufferedEvents().sorted(Comparator.comparing(RetryEvent::getCreationTime)).map(RetryEventDTO::createRetryEventDTO).toJavaList();
                d.success(new RetryEventsEndpointResponse(eventsList));
            }).then(r -> ctx.render(Jackson.json(r)));
        });
        chain1.get("stream/events/:name", ctx -> {
            String rateLimiterName = ctx.getPathTokens().get("name");
            Retry retry = retryRegistry.getAllRetries().find(rL -> rL.getName().equals(rateLimiterName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", rateLimiterName)));
            Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(toFlowable(retry.getEventPublisher()), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
        chain1.get("events/:name/:type", ctx -> {
            String retryName = ctx.getPathTokens().get("name");
            String eventType = ctx.getPathTokens().get("type");
            Promise.<RetryEventsEndpointResponse>async(d -> {
                List<RetryEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(retryName).getBufferedEvents().sorted(Comparator.comparing(RetryEvent::getCreationTime)).filter(event -> event.getEventType() == RetryEvent.Type.valueOf(eventType.toUpperCase())).map(RetryEventDTO::createRetryEventDTO).toJavaList();
                d.success(new RetryEventsEndpointResponse(eventsList));
            }).then(r -> ctx.render(Jackson.json(r)));
        });
        chain1.get("stream/events/:name/:type", ctx -> {
            String retryName = ctx.getPathTokens().get("name");
            String eventType = ctx.getPathTokens().get("type");
            Retry retry = retryRegistry.getAllRetries().find(rL -> rL.getName().equals(retryName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", retryName)));
            Flowable<RetryEvent> eventStream = toFlowable(retry.getEventPublisher()).filter(event -> event.getEventType() == RetryEvent.Type.valueOf(eventType.toUpperCase()));
            Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStream, e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
    });
}
Also used : Function(ratpack.func.Function) Retry(io.github.resilience4j.retry.Retry) Promise(ratpack.exec.Promise) RetryEvent(io.github.resilience4j.retry.event.RetryEvent) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) Jackson(ratpack.jackson.Jackson) ServerSentEvents(ratpack.sse.ServerSentEvents) Chain(ratpack.handling.Chain) Inject(javax.inject.Inject) List(java.util.List) Flowable(io.reactivex.Flowable) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) Action(ratpack.func.Action) RxJava2Adapter.toFlowable(io.github.resilience4j.adapter.RxJava2Adapter.toFlowable) Seq(io.vavr.collection.Seq) RetryRegistry(io.github.resilience4j.retry.RetryRegistry) Comparator(java.util.Comparator) EventConsumerRegistry(io.github.resilience4j.consumer.EventConsumerRegistry) RetryEvent(io.github.resilience4j.retry.event.RetryEvent) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) ServerSentEvents(ratpack.sse.ServerSentEvents) List(java.util.List) Retry(io.github.resilience4j.retry.Retry) Flowable(io.reactivex.Flowable) RxJava2Adapter.toFlowable(io.github.resilience4j.adapter.RxJava2Adapter.toFlowable)

Example 17 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RetryTransformerTest method shouldNotRetryFromPredicateUsingObservable.

@Test
public void shouldNotRetryFromPredicateUsingObservable() {
    // Given
    RetryConfig config = RetryConfig.custom().retryOnException(t -> t instanceof IOException).maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // When
    Observable.fromCallable(helloWorldService::returnHelloWorld).compose(RetryTransformer.of(retry)).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    // Then
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) IOException(java.io.IOException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 18 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RetryTransformerTest method shouldReturnOnErrorUsingFlowable.

@Test
public void shouldReturnOnErrorUsingFlowable() {
    // 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 19 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RetryTransformerTest method shouldReturnOnCompleteUsingSingle.

@Test
public void shouldReturnOnCompleteUsingSingle() {
    // Given
    RetryConfig config = RetryConfig.ofDefaults();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world").willThrow(new WebServiceException("BAM!")).willThrow(new WebServiceException("BAM!")).willReturn("Hello world");
    // When
    Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertValueCount(1).assertValues("Hello world").assertComplete();
    Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertValueCount(1).assertValues("Hello world").assertComplete();
    // Then
    BDDMockito.then(helloWorldService).should(Mockito.times(4)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    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 20 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RetryTransformerTest method shouldNotRetryFromPredicateUsingFlowable.

@Test
public void shouldNotRetryFromPredicateUsingFlowable() {
    // Given
    RetryConfig config = RetryConfig.custom().retryOnException(t -> t instanceof IOException).maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // When
    Flowable.fromCallable(helloWorldService::returnHelloWorld).compose(RetryTransformer.of(retry)).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    // Then
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) IOException(java.io.IOException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

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