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

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

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

Bulkhead.decorateCheckedFunction介绍

[英]Returns a function which is decorated by a bulkhead.
[中]返回由隔板修饰的函数。

代码示例

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

public DecorateCheckedFunction<T, R> withBulkhead(Bulkhead bulkhead) {
  function = Bulkhead.decorateCheckedFunction(bulkhead, function);
  return this;
}

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

@Test
public void shouldDecorateCheckedFunctionAndReturnWithException() throws Throwable {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  BDDMockito.given(helloWorldService.returnHelloWorldWithNameWithException("Tom")).willThrow(new RuntimeException("BAM!"));
  // When
  CheckedFunction1<String, String> function  = Bulkhead.decorateCheckedFunction(bulkhead, helloWorldService::returnHelloWorldWithNameWithException);
  Try<String> result = Try.of(() -> function.apply("Tom"));
  // Then
  assertThat(result.isFailure()).isTrue();
  assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}

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

@Test
public void shouldDecorateCheckedFunctionAndReturnWithSuccess() throws Throwable {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  BDDMockito.given(helloWorldService.returnHelloWorldWithNameWithException("Tom")).willReturn("Hello world Tom");
  // When
  String result = Bulkhead.decorateCheckedFunction(bulkhead, helloWorldService::returnHelloWorldWithNameWithException)
              .apply("Tom");
  // Then
  assertThat(result).isEqualTo("Hello world Tom");
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithNameWithException("Tom");
}

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

@Test
public void shouldChainDecoratedFunctions() throws ExecutionException, InterruptedException {
  // tag::shouldChainDecoratedFunctions[]
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  Bulkhead anotherBulkhead = Bulkhead.of("testAnother", config);
  // When I create a Supplier and a Function which are decorated by different Bulkheads
  CheckedFunction0<String> decoratedSupplier
    = Bulkhead.decorateCheckedSupplier(bulkhead, () -> "Hello");
  CheckedFunction1<String, String> decoratedFunction
    = Bulkhead.decorateCheckedFunction(anotherBulkhead, (input) -> input + " world");
  // and I chain a function with map
  Try<String> result = Try.of(decoratedSupplier)
              .mapTry(decoratedFunction::apply);
  // Then
  assertThat(result.isSuccess()).isTrue();
  assertThat(result.get()).isEqualTo("Hello world");
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  assertThat(anotherBulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  // end::shouldChainDecoratedFunctions[]
}

相关文章