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

x33g5p2x  于2022-01-19 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(669)

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

Flux.switchIfEmpty介绍

[英]Switch to an alternative Publisher if this sequence is completed without any data.
[中]如果此序列在没有任何数据的情况下完成,请切换到其他发布服务器。

代码示例

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

public Flux<String> processOrFallback(Mono<String> source, Publisher<String> fallback) {
  return source
      .flatMapMany(phrase -> Flux.fromArray(phrase.split("\\s+")))
      .switchIfEmpty(fallback);
}

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

/**
 * Fetch the list of ids matching a given condition.
 *
 * @param targetType must not be {@literal null}.
 * @param condition must not be {@literal null}.
 * @return empty {@link List} if none found.
 */
protected Flux<Object> getIds(Class<?> targetType, Mono<Predicate> condition) {
  return condition.flatMapMany(it -> getIds(targetType, it))
      .switchIfEmpty(Flux.defer(() -> getIds(targetType, (Predicate) null)));
}

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

@Test(expected = NullPointerException.class)
public void otherNull() {
  Flux.never()
    .switchIfEmpty(null);
}

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

flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParam, ex)));
    if (isBodyRequired) {
      flux = flux.switchIfEmpty(Flux.error(() -> handleMissingBody(bodyParam)));
});
if (isBodyRequired) {
  body = body.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));

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

@Test
public void empty() {
  AssertSubscriber<Integer> ts = AssertSubscriber.create();
  Flux.<Integer>empty()
    .switchIfEmpty(Flux.just(10))
    .subscribe(ts);
  ts.assertValues(10)
   .assertComplete()
   .assertNoError();
}

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

@Test
public void nonEmpty() {
  AssertSubscriber<Integer> ts = AssertSubscriber.create();
  Flux.just(1, 2, 3, 4, 5)
    .switchIfEmpty(Flux.just(10))
    .subscribe(ts);
  ts.assertValues(1, 2, 3, 4, 5)
   .assertComplete()
   .assertNoError();
}

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

@Test
public void emptyBackpressured() {
  AssertSubscriber<Integer> ts = AssertSubscriber.create(0);
  Flux.<Integer>empty()
    .switchIfEmpty(Flux.just(10))
    .subscribe(ts);
  ts.assertNoValues()
   .assertNoError()
   .assertNotComplete();
  ts.request(2);
  ts.assertValues(10)
   .assertComplete()
   .assertNoError();
}

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

@Test
public void nonEmptyBackpressured() {
  AssertSubscriber<Integer> ts = AssertSubscriber.create(0);
  Flux.just(1, 2, 3, 4, 5)
    .switchIfEmpty(Flux.just(10))
    .subscribe(ts);
  ts.assertNoValues()
   .assertNoError()
   .assertNotComplete();
  ts.request(2);
  ts.assertValues(1, 2)
   .assertNotComplete()
   .assertNoError();
  ts.request(10);
  ts.assertValues(1, 2, 3, 4, 5)
   .assertComplete()
   .assertNoError();
}

代码示例来源:origin: com.aol.cyclops/cyclops-reactor

/**
 * @param alternate
 * @return
 * @see reactor.core.publisher.Flux#switchIfEmpty(org.reactivestreams.Publisher)
 */
public final Flux<T> switchIfEmpty(Publisher<? extends T> alternate) {
  return boxed.switchIfEmpty(alternate);
}
/**

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Flux<Resource<?>> getDomains(CloudFoundryClient cloudFoundryClient, String organizationId, String domain) {
  return requestPrivateDomains(cloudFoundryClient, organizationId, domain)
    .map((Function<PrivateDomainResource, Resource<?>>) in -> in)
    .switchIfEmpty(requestSharedDomains(cloudFoundryClient, domain));
}

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Mono<Void> validateOrganization(CloudFoundryClient cloudFoundryClient, String organizationName) {
  if (organizationName != null) {
    return requestListOrganizations(cloudFoundryClient, organizationName)
      .switchIfEmpty(ExceptionUtils.illegalArgument("Organization %s not found", organizationName))
      .then();
  } else {
    return Mono.empty();
  }
}

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Mono<Void> validateService(CloudFoundryClient cloudFoundryClient, String serviceName) {
  if (serviceName != null) {
    return requestListServices(cloudFoundryClient, serviceName)
      .switchIfEmpty(ExceptionUtils.illegalArgument("Service %s not found", serviceName))
      .then();
  } else {
    return Mono.empty();
  }
}

代码示例来源:origin: hantsy/spring-reactive-sample

@Bean
public ReactiveRedisMessageListenerContainer redisMessageListenerContainer(PostRepository posts, ReactiveRedisConnectionFactory connectionFactory) {
  ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(connectionFactory);
  ObjectMapper objectMapper = new ObjectMapper();
  container.receive(ChannelTopic.of("posts"))
    .map(p->p.getMessage())
    .map(m -> {
      try {
        Post post= objectMapper.readValue(m, Post.class);
        post.setId(UUID.randomUUID().toString());
        return post;
      } catch (IOException e) {
        return null;
      }
    })
    .switchIfEmpty(Mono.error(new IllegalArgumentException()))
    .flatMap(p-> posts.save(p))
    .subscribe(c-> log.info(" count:" + c), null , () -> log.info("saving post."));
  return container;
}

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Mono<List<ServiceBrokerResource>> listServiceBrokers(CloudFoundryClient cloudFoundryClient) {
  return requestListServiceBrokers(cloudFoundryClient)
    .switchIfEmpty(ExceptionUtils.illegalArgument("No Service Brokers found"))
    .collectList();
}

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Mono<String> getUserId(UaaClient uaaClient, String username) {
  return PaginationUtils
    .requestUaaResources(startIndex -> uaaClient.users()
      .list(ListUsersRequest.builder()
        .filter(String.format("userName eq \"%s\"", username))
        .startIndex(startIndex)
        .build()))
    .switchIfEmpty(ExceptionUtils.illegalArgument("User %s does not exist", username))
    .single()
    .map(User::getId);
}

代码示例来源:origin: org.mule.runtime/mule-core

@Override
 public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
  return from(publisher)
    .flatMapMany(event -> processWithChildContext(event,
                           p -> from(p).flatMapMany(e -> template.routeEventAsync(e))
                             .switchIfEmpty(fromCallable(() -> emptyEvent(templateEvent))),
                           Optional.empty(), messagingExceptionHandler));
 }
}

代码示例来源:origin: org.mule.runtime/mule-core

.switchIfEmpty(defer(() -> {
 if (count.get() == 0) {
  logger

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

ex -> Flux.error(handleReadError(bodyParam, ex)));
if (isBodyRequired) {
  flux = flux.switchIfEmpty(
      Flux.error(() -> handleMissingBody(bodyParam)));

代码示例来源:origin: org.springframework.cloud/spring-cloud-function-web

ex -> Flux.error(handleReadError(bodyParam, ex)));
if (isBodyRequired) {
  flux = flux.switchIfEmpty(
      Flux.error(() -> handleMissingBody(bodyParam)));

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

.handle((i, o) -> i.receive().asString())
.log()
.switchIfEmpty(Mono.error(new Exception()));

相关文章

Flux类方法