io.github.resilience4j.bulkhead.Bulkhead.decorateCompletionStage()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(4.2k)|赞(0)|评价(0)|浏览(192)

本文整理了Java中io.github.resilience4j.bulkhead.Bulkhead.decorateCompletionStage()方法的一些代码示例,展示了Bulkhead.decorateCompletionStage()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bulkhead.decorateCompletionStage()方法的具体详情如下:
包路径:io.github.resilience4j.bulkhead.Bulkhead
类名称:Bulkhead
方法名:decorateCompletionStage

Bulkhead.decorateCompletionStage介绍

[英]Returns a supplier which is decorated by a bulkhead.
[中]返回由隔板装饰的供应商。

代码示例

代码示例来源:origin: resilience4j/resilience4j

public DecorateCompletionStage<T> withBulkhead(Bulkhead bulkhead) {
  stageSupplier = Bulkhead.decorateCompletionStage(bulkhead, stageSupplier);
  return this;
}

代码示例来源:origin: resilience4j/resilience4j

/**
 * Decorates and executes the decorated CompletionStage.
 *
 * @param supplier the original CompletionStage
 * @param <T> the type of results supplied by this supplier
 * @return the decorated CompletionStage.
 */
default <T> CompletionStage<T> executeCompletionStage(Supplier<CompletionStage<T>> supplier){
  return decorateCompletionStage(this, supplier).get();
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void shouldDecorateCompletionStageAndReturnWithExceptionAtAsyncStage() throws ExecutionException, InterruptedException {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM! At async stage"));
  // When
  Supplier<CompletionStage<String>> completionStageSupplier =
      () -> CompletableFuture.supplyAsync(helloWorldService::returnHelloWorld);
  Supplier<CompletionStage<String>> decoratedCompletionStageSupplier =
      Bulkhead.decorateCompletionStage(bulkhead, completionStageSupplier);
  CompletionStage<String> decoratedCompletionStage = decoratedCompletionStageSupplier.get();
  // Then the helloWorldService should be invoked 1 time
  assertThatThrownBy(decoratedCompletionStage.toCompletableFuture()::get)
      .isInstanceOf(ExecutionException.class).hasCause(new RuntimeException("BAM! At async stage"));
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void shouldDecorateCompletionStageAndReturnWithSuccess() throws ExecutionException, InterruptedException {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello");
  // When
  Supplier<CompletionStage<String>> completionStageSupplier =
      () -> CompletableFuture.supplyAsync(helloWorldService::returnHelloWorld);
  Supplier<CompletionStage<String>> decoratedCompletionStageSupplier =
      Bulkhead.decorateCompletionStage(bulkhead, completionStageSupplier);
  CompletionStage<String> decoratedCompletionStage = decoratedCompletionStageSupplier
      .get()
      .thenApply(value -> value + " world");
  // Then
  assertThat(decoratedCompletionStage.toCompletableFuture().get()).isEqualTo("Hello world");
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void shouldDecorateCompletionStageAndReturnWithExceptionAtSyncStage() throws ExecutionException, InterruptedException {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  // When
  Supplier<CompletionStage<String>> completionStageSupplier = () -> {
    throw new WebServiceException("BAM! At sync stage");
  };
  Supplier<CompletionStage<String>> decoratedCompletionStageSupplier =
      Bulkhead.decorateCompletionStage(bulkhead, completionStageSupplier);
  // NOTE: Try.of does not detect a completion stage that has been completed with failure !
  Try<CompletionStage<String>> result = Try.of(decoratedCompletionStageSupplier::get);
  // Then the helloWorldService should be invoked 0 times
  BDDMockito.then(helloWorldService).should(times(0)).returnHelloWorld();
  assertThat(result.isSuccess()).isTrue();
  result.get()
     .exceptionally(
       error -> {
         // NOTE: Try.of does not detect a completion stage that has been completed with failure !
         assertThat(error).isInstanceOf(WebServiceException.class);
         return null;
       }
     );
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}

相关文章