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

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

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

Bulkhead.decorateSupplier介绍

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

代码示例

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

public DecorateSupplier<T> withBulkhead(Bulkhead bulkhead) {
  supplier = Bulkhead.decorateSupplier(bulkhead, supplier);
  return this;
}

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

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

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

@Test
public void shouldInvokeAsyncApply() throws ExecutionException, InterruptedException {
  // tag::shouldInvokeAsyncApply[]
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  // When
  Supplier<String> decoratedSupplier = Bulkhead.decorateSupplier(bulkhead, () -> "This can be any method which returns: 'Hello");
  CompletableFuture<String> future = CompletableFuture.supplyAsync(decoratedSupplier)
                            .thenApply(value -> value + " world'");
  String result = future.get();
  // Then
  assertThat(result).isEqualTo("This can be any method which returns: 'Hello world'");
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  // end::shouldInvokeAsyncApply[]
}

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

@Test
public void shouldDecorateSupplierAndReturnWithSuccess() {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
  // When
  Supplier<String> supplier = Bulkhead.decorateSupplier(bulkhead, helloWorldService::returnHelloWorld);
  // Then
  assertThat(supplier.get()).isEqualTo("Hello world");
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}

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

@Test
public void shouldDecorateSupplierAndReturnWithException() {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  // When
  Supplier<String> supplier = Bulkhead.decorateSupplier(bulkhead, helloWorldService::returnHelloWorld);
  Try<String> result = Try.of(supplier::get);
  //Then
  assertThat(result.isFailure()).isTrue();
  assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}

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

@Test
public void shouldConsumeOnCallFinishedEventWhenExecutionIsFinished() throws Exception {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  // When
  bulkhead.getEventPublisher()
      .onCallFinished(event ->
          logger.info(event.getEventType().toString()));
  Try.ofSupplier(Bulkhead.decorateSupplier(bulkhead,helloWorldService::returnHelloWorld));
  // Then
  then(logger).should(times(1)).info("CALL_FINISHED");
}

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

@Test
public void shouldConsumeOnCallRejectedEvent() {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  // When
  bulkhead.getEventPublisher()
      .onCallRejected(event ->
          logger.info(event.getEventType().toString()));
  bulkhead.isCallPermitted();
  Try.ofSupplier(Bulkhead.decorateSupplier(bulkhead,helloWorldService::returnHelloWorld));
  // Then
  then(logger).should(times(1)).info("CALL_REJECTED");
}

相关文章