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

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

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

Mono.blockOptional介绍

[英]Subscribe to this Mono and block indefinitely until a next signal is received or the Mono completes empty. Returns an Optional, which can be used to replace the empty case with an Exception via Optional#orElseThrow(Supplier). In case the Mono itself errors, the original exception is thrown (wrapped in a RuntimeException if it was a checked exception).

Note that each blockOptional() will trigger a new subscription: in other words, the result might miss signal from hot publishers.
[中]订阅此Mono并无限期阻塞,直到收到下一个信号或Mono完成清空。返回可选的,可通过可选的#orElseThrow(供应商)用异常替换空案例。如果Mono本身出错,则会抛出原始异常(如果它是选中的异常,则包装在RuntimeException中)。
请注意,每个blockOptional()都将触发一个新订阅:换句话说,结果可能会错过热门发布商的信号。

代码示例

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

@Test
public void timeoutOptionalValued() {
  Optional<String> result = Mono.just("foo")
                 .blockOptional(Duration.ofMillis(500));
  assertThat(result).contains("foo");
}

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

@Test
public void timeoutOptionalEmpty() {
  Optional<String> result = Mono.<String>empty()
                 .blockOptional(Duration.ofMillis(500));
  assertThat(result).isEmpty();
}

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

@Test
public void optionalEmpty() {
  Optional<String> result = Mono.<String>empty().blockOptional();
  assertThat(result).isEmpty();
}

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

@Test
public void optionalValued() {
  Optional<String> result = Mono.just("foo").blockOptional();
  assertThat(result).contains("foo");
}

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

@Test
public void monoBlockOptionalDoesntCancel() {
  AtomicLong cancelCount = new AtomicLong();
  Mono.just("data")
    .doOnCancel(cancelCount::incrementAndGet)
    .blockOptional();
  assertThat(cancelCount.get()).isEqualTo(0);
}

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

@Test
public void optionalEmptyDelayed() {
  Optional<String> result = Mono.<String>empty()
      .delayElement(Duration.ofMillis(500))
      .blockOptional();
  assertThat(result).isEmpty();
}

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

@Test
public void optionalValuedDelayed() {
  Optional<String> result = Mono.just("foo")
                 .delayElement(Duration.ofMillis(500))
                 .blockOptional();
  assertThat(result).contains("foo");
}

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

private <T extends WebFilter> Optional<T> getWebFilter(SecurityWebFilterChain filterChain, Class<T> filterClass) {
  return (Optional<T>) filterChain.getWebFilters()
      .filter(Objects::nonNull)
      .filter(filter -> filter.getClass().isAssignableFrom(filterClass))
      .singleOrEmpty()
      .blockOptional();
}

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

@Test
public void timeoutOptionalError() {
  Mono<String> source = Mono.error(new IllegalStateException("boom"));
  assertThatExceptionOfType(IllegalStateException.class)
      .isThrownBy(() -> source.blockOptional(Duration.ofMillis(500)))
      .withMessage("boom");
}

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

@Test
public void timeoutOptionalTimingOut() {
  Mono<Long> source = Mono.delay(Duration.ofSeconds(1));
  assertThatExceptionOfType(IllegalStateException.class)
      .isThrownBy(() -> source.blockOptional(Duration.ofMillis(500)))
      .withMessage("Timeout on blocking read for 500 MILLISECONDS");
}

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

@Test
public void monoBlockOptionalForbidden() {
  Function<String, Optional<String>> badMapper = v -> Mono.just(v).hide()
                              .blockOptional();
  Function<String, Optional<String>> badMapperTimeout = v -> Mono.just(v).hide()
                                  .blockOptional(Duration.ofMillis(100));
  Mono<Optional<String>> forbiddenSequence1 = Mono.just("data")
                          .publishOn(nonBlockingScheduler)
                          .map(badMapper);
  StepVerifier.create(forbiddenSequence1)
        .expectErrorSatisfies(e -> assertThat(e)
            .isInstanceOf(IllegalStateException.class)
            .hasMessageStartingWith("blockOptional() is blocking, which is not supported in thread nonBlockingScheduler-"))
        .verify();
  Mono<Optional<String>> forbiddenSequence2 = Mono.just("data")
                          .publishOn(nonBlockingScheduler)
                          .map(badMapperTimeout);
  StepVerifier.create(forbiddenSequence2)
        .expectErrorSatisfies(e -> assertThat(e)
            .isInstanceOf(IllegalStateException.class)
            .hasMessageStartingWith("blockOptional() is blocking, which is not supported in thread nonBlockingScheduler-"))
        .verify();
}

代码示例来源:origin: com.holon-platform.reactor/holon-reactor-spring

@Override
public <E> Optional<E> as(Class<E> entityType) {
  return asMono(entityType).blockOptional();
}

代码示例来源:origin: com.holon-platform.reactor/holon-reactor-spring

@Override
public Optional<T> getPayload() throws UnsupportedOperationException {
  return asMono().blockOptional();
}

代码示例来源:origin: com.holon-platform.reactor/holon-reactor-spring

@Override
public <E> Optional<E> as(ResponseType<E> entityType) {
  return asMono(entityType).blockOptional();
}

代码示例来源:origin: spring-cloud/spring-cloud-cloudfoundry

@Override
public List<String> getServices() {
  return this.cloudFoundryOperations
      .applications()
      .list()
      .map(ApplicationSummary::getName)
      .collectList()
      .blockOptional()
      .orElse(new ArrayList<>());
}

代码示例来源:origin: spring-cloud/spring-cloud-cloudfoundry

@Override
public List<ServiceInstance> getInstances(String serviceId) {
  return cloudFoundryService
      .getApplicationInstances(serviceId)
      .map(tuple -> {
        ApplicationDetail applicationDetail = tuple.getT1();
        InstanceDetail instanceDetail = tuple.getT2();
        String applicationId = applicationDetail.getId();
        String applicationIndex = instanceDetail.getIndex();
        String name = applicationDetail.getName();
        String url = applicationDetail.getUrls().size() > 0 ? applicationDetail.getUrls().get(0) : null;
        boolean secure = (url + "").toLowerCase().startsWith("https");
        HashMap<String, String> metadata = new HashMap<>();
        metadata.put("applicationId", applicationId);
        metadata.put("instanceId", applicationIndex);
        return (ServiceInstance) new DefaultServiceInstance(name, url, 80, secure, metadata);
      })
      .collectList()
      .blockOptional()
      .orElse(new ArrayList<>());
}

代码示例来源:origin: kptfh/feign-reactive

@Test
public void shouldReturnEmpty() {
  Optional<AllFeaturesApi.TestObject> returned = client.empty().blockOptional();
  assertThat(!returned.isPresent());
}

代码示例来源:origin: spring-cloud/spring-cloud-cloudfoundry

private List<CloudFoundryServer> cloudFoundryServers() {
  Assert.notNull(this.clientConfig, "clientConfig may not be null");
  RibbonProperties ribbon = RibbonProperties.from(clientConfig);
  Boolean secure = ribbon.getSecure();
  Integer securePort = ribbon.getSecurePort();
  Integer nonSecurePort = ribbon.getPort();
  final int port;
  if (secure != null && secure && securePort != null) {
    port = securePort;
  } else if (nonSecurePort != null) {
    port = nonSecurePort;
  } else {
    port = this.properties.getDefaultServerPort();
  }
  return cloudFoundryService
      .getApplicationInstances(this.serviceId)
      .map(tpl -> new CloudFoundryServer(tpl.getT1().getName(), tpl.getT1().getUrls().get(0), port))
      .collectList()
      .blockOptional()
      .orElse(new ArrayList<>());
}

相关文章

Mono类方法