reactor.core.publisher.Mono.zip()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(1371)

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

Mono.zip介绍

[英]Aggregate given monos into a new Mono that will be fulfilled when all of the given Monos have produced an item, aggregating their values according to the provided combinator function. An error or empty completion of any source will cause other sources to be cancelled and the resulting Mono to immediately error or complete, respectively.
[中]将给定的Mono聚合成一个新的Mono,当所有给定的Mono都生成了一个项时,将根据提供的组合函数聚合它们的值。任何源的错误或空完成都会导致其他源被取消,并导致Mono立即出错或完成。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private Mono<Void> invokeModelAttributeMethods(BindingContext bindingContext,
    List<InvocableHandlerMethod> modelMethods, ServerWebExchange exchange) {
  List<Mono<HandlerResult>> resultList = new ArrayList<>();
  modelMethods.forEach(invocable -> resultList.add(invocable.invoke(exchange, bindingContext)));
  return Mono
      .zip(resultList, objectArray ->
          Arrays.stream(objectArray)
              .map(object -> handleResult(((HandlerResult) object), bindingContext))
              .collect(Collectors.toList()))
      .flatMap(Mono::when);
}

代码示例来源:origin: spring-projects/spring-framework

return Mono.zip(valueMonos,
    values -> {
      for (int i=0; i < values.length; i++) {

代码示例来源:origin: spring-projects/spring-framework

return Mono.zip(argMonos, values ->
    Stream.of(values).map(o -> o != NO_ARG_VALUE ? o : null).toArray());

代码示例来源:origin: reactor/reactor-core

@Test
public void oneSourcePublisherCombined() {
  assertThat(Mono.zip(args -> (int) args[0], Mono.just(1))
          .block()).isEqualTo(1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void allNonEmpty2() {
  assertThat(Mono.zip(args -> (int) args[0] + (int) args[1],
      Mono.just(1),
      Mono.just(2))
          .block()).isEqualTo(3);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void allNonEmptyIterable() {
  assertThat(Mono.zip(Arrays.asList(Mono.just(1), Mono.just(2)),
      args -> (int) args[0] + (int) args[1])
          .block()).isEqualTo(3);
}

代码示例来源:origin: reactor/reactor-core

@Test(timeout = 5000)
public void castCheck() {
  Mono<String[]> mono = Mono.zip(a -> Arrays.copyOf(a, a.length, String[].class),
      Mono.just("hello"),
      Mono.just("world"));
  mono.subscribe(System.out::println);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void allEmpty() {
  Assert.assertNull(Mono.zip(Mono.empty(), Mono.empty())
             .block());
}

代码示例来源:origin: reactor/reactor-core

@Test
public void noSourcePublisherCombined() {
  assertThat(Mono.zip(args -> (int) args[0] + (int) args[1])
          .block()).isNull();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Combine query params and form data for multipart form data from the body
 * of the request into a {@code Map<String, Object>} of values to use for
 * data binding purposes.
 * @param exchange the current exchange
 * @return a {@code Mono} with the values to bind
 * @see org.springframework.http.server.reactive.ServerHttpRequest#getQueryParams()
 * @see ServerWebExchange#getFormData()
 * @see ServerWebExchange#getMultipartData()
 */
public static Mono<Map<String, Object>> extractValuesToBind(ServerWebExchange exchange) {
  MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
  Mono<MultiValueMap<String, String>> formData = exchange.getFormData();
  Mono<MultiValueMap<String, Part>> multipartData = exchange.getMultipartData();
  return Mono.zip(Mono.just(queryParams), formData, multipartData)
      .map(tuple -> {
        Map<String, Object> result = new TreeMap<>();
        tuple.getT1().forEach((key, values) -> addBindValue(result, key, values));
        tuple.getT2().forEach((key, values) -> addBindValue(result, key, values));
        tuple.getT3().forEach((key, values) -> addBindValue(result, key, values));
        return result;
      });
}

代码示例来源:origin: reactor/reactor-core

@Test
public void emptySources() {
  AtomicBoolean cancelled = new AtomicBoolean();
  Mono<String> empty1 = Mono.empty();
  Mono<String> empty2 = Mono.empty();
  Mono<String> empty3 = Mono.<String>empty().delaySubscription(Duration.ofMillis(500))
      .doOnCancel(() -> cancelled.set(true));
  Duration d = StepVerifier.create(Mono.zip(empty1, empty2, empty3))
        .verifyComplete();
  assertThat(cancelled).isTrue();
  assertThat(d).isLessThan(Duration.ofMillis(500));
}

代码示例来源:origin: spring-projects/spring-security

Mono<Request> createDefaultedRequest(String clientRegistrationId,
    Authentication authentication, ServerWebExchange exchange) {
  Mono<Authentication> defaultedAuthentication = Mono.justOrEmpty(authentication)
      .switchIfEmpty(currentAuthentication());
  Mono<String> defaultedRegistrationId = Mono.justOrEmpty(clientRegistrationId)
      .switchIfEmpty(Mono.justOrEmpty(this.defaultClientRegistrationId))
      .switchIfEmpty(clientRegistrationId(defaultedAuthentication));
  Mono<Optional<ServerWebExchange>> defaultedExchange = Mono.justOrEmpty(exchange)
      .switchIfEmpty(currentServerWebExchange()).map(Optional::of)
      .defaultIfEmpty(Optional.empty());
  return Mono.zip(defaultedRegistrationId, defaultedAuthentication, defaultedExchange)
      .map(t3 -> new Request(t3.getT1(), t3.getT2(), t3.getT3().orElse(null)));
}

代码示例来源:origin: reactor/reactor-core

@Test//(timeout = 5000)
public void all2NonEmpty() {
  Assert.assertEquals(Tuples.of(0L, 0L),
      Mono.zip(Mono.delay(Duration.ofMillis(150)), Mono.delay(Duration.ofMillis(250)))
        .block());
}

代码示例来源:origin: reactor/reactor-core

@Test
public void whenIterableDoesntCombineErrors() {
  Exception boom1 = new NullPointerException("boom1");
  Exception boom2 = new IllegalArgumentException("boom2");
  StepVerifier.create(Mono.zip(
      Arrays.asList(Mono.just("foo"), Mono.<String>error(boom1), Mono.<String>error(boom2)),
      Tuples.fn3()))
        .verifyErrorMatches(e -> e == boom1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void whenMonoJust7() {
  StepVerifier.create(Mono.zip(Mono.just(1),
      Mono.just(2),
      Mono.just(3),
      Mono.just(4),
      Mono.just(5),
      Mono.just(6),
      Mono.just(7)))
        .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3 && v.getT4() == 4 && v.getT5() == 5 && v.getT6() == 6 && v.getT7() == 7).isTrue())
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void whenMonoError() {
  MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create();
  StepVerifier.create(Mono.zip(Mono.<Integer>error(new Exception("test1")),
      Mono.<Integer>error(new Exception("test2")))
              .subscribeWith(mp))
        .then(() -> assertThat(mp.isError()).isTrue())
        .then(() -> assertThat(mp.isSuccess()).isFalse())
        .then(() -> assertThat(mp.isTerminated()).isTrue())
        .verifyErrorSatisfies(e -> assertThat(e).hasMessage("test1"));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void whenMonoJust() {
  MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create();
  StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2))
              .subscribeWith(mp))
        .then(() -> assertThat(mp.isError()).isFalse())
        .then(() -> assertThat(mp.isSuccess()).isTrue())
        .then(() -> assertThat(mp.isTerminated()).isTrue())
        .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue())
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void whenMonoJust3() {
  MonoProcessor<Tuple3<Integer, Integer, Integer>> mp = MonoProcessor.create();
  StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2), Mono.just(3))
              .subscribeWith(mp))
        .then(() -> assertThat(mp.isError()).isFalse())
        .then(() -> assertThat(mp.isSuccess()).isTrue())
        .then(() -> assertThat(mp.isTerminated()).isTrue())
        .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3).isTrue())
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void someEmpty() {
  StepVerifier.withVirtualTime(() ->
      Mono.zip(Mono.delay(Duration.ofMillis(150)).then(), Mono.delay(Duration
          .ofMillis(250))))
        .thenAwait(Duration.ofMillis(150))
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void whenMonoCallable() {
  MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create();
  StepVerifier.create(Mono.zip(Mono.fromCallable(() -> 1),
      Mono.fromCallable(() -> 2))
              .subscribeWith(mp))
        .then(() -> assertThat(mp.isError()).isFalse())
        .then(() -> assertThat(mp.isSuccess()).isTrue())
        .then(() -> assertThat(mp.isTerminated()).isTrue())
        .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue())
        .verifyComplete();
}

相关文章

微信公众号

最新文章

更多

Mono类方法