Java Code Examples for io.github.resilience4j.circuitbreaker.CircuitBreaker#executeSupplier()

The following examples show how to use io.github.resilience4j.circuitbreaker.CircuitBreaker#executeSupplier() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageServiceSupport.java    From front50 with Apache License 2.0 6 votes vote down vote up
public T findById(String id) throws NotFoundException {
  CircuitBreaker breaker =
      circuitBreakerRegistry.circuitBreaker(
          getClass().getSimpleName() + "-findById",
          CircuitBreakerConfig.custom()
              .ignoreException(e -> e instanceof NotFoundException)
              .build());

  Supplier<T> recoverableSupplier =
      SupplierUtils.recover(
          () -> service.loadObject(objectType, buildObjectKey(id)),
          e ->
              allItemsCache.get().stream()
                  .filter(item -> item.getId().equalsIgnoreCase(id))
                  .findFirst()
                  .orElseThrow(
                      () ->
                          new NotFoundException(
                              String.format(
                                  "No item found in cache with id of %s", id.toLowerCase()))));

  return breaker.executeSupplier(recoverableSupplier);
}
 
Example 2
Source File: CircuitBreakerMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRemoveAllMetrics() {
    CircuitBreaker circuitBreaker = givenMetricRegistry("testPrefix", metricRegistry);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");

    String value = circuitBreaker.executeSupplier(helloWorldService::returnHelloWorld);

    assertThat(value).isEqualTo("Hello world");
    then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(10);

    circuitBreakerRegistry.remove(circuitBreaker.getName());
    assertThat(metricRegistry.getMetrics()).hasSize(0);
}
 
Example 3
Source File: AbstractCircuitBreakerMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRegisterMetrics() {
    CircuitBreaker circuitBreaker = givenMetricRegistry(metricRegistry);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");

    String value = circuitBreaker.executeSupplier(helloWorldService::returnHelloWorld);

    assertThat(value).isEqualTo("Hello world");
    then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(10);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.state").getValue())
        .isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.buffered")
        .getValue()).isEqualTo(1);
    assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.successful")
        .getValue()).isEqualTo(1);
    assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.failed")
        .getValue()).isEqualTo(0);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.slow").getValue())
        .isEqualTo(0);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.slow_successful")
            .getValue()).isEqualTo(0);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.slow_failed")
            .getValue()).isEqualTo(0);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.not_permitted")
            .getValue()).isEqualTo(0L);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.failure_rate")
            .getValue()).isEqualTo(-1f);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.slow_call_rate")
            .getValue()).isEqualTo(-1f);
}
 
Example 4
Source File: AbstractCircuitBreakerMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseCustomPrefix() throws Throwable {
    CircuitBreaker circuitBreaker = givenMetricRegistry("testPrefix", metricRegistry);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");

    String value = circuitBreaker.executeSupplier(helloWorldService::returnHelloWorld);

    assertThat(value).isEqualTo("Hello world");
    then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(10);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.state").getValue())
        .isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.buffered").getValue())
        .isEqualTo(1);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.successful").getValue())
        .isEqualTo(1);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.failed").getValue())
        .isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.slow").getValue())
        .isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.slow_successful").getValue())
        .isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.slow_failed").getValue())
        .isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.not_permitted").getValue())
        .isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.failure_rate").getValue())
        .isEqualTo(-1f);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName.slow_call_rate").getValue())
        .isEqualTo(-1f);
}