Reactor编程中有时候我们需要对empty Mono<T>做一些特定业务逻辑。下面看一段非reactor编程的代码:

public void oldCheck(Token token) {
  if (token == null) {
    // business logic
    return;
  }
  if (token.isExpired) {
    // business logic
    return;
  }
  // business logic
  return;
}

如果让你改成reactor你也许会改成这样:

public Mono<Void> badCheck(Mono<Token> tokenMono) {
  return tokenMono
      .flatMap(token -> {
        if (token == null) {
          // CAUTION: You will never be in here
          // business logic
          return Mono.empty();
        }
        if (token.isExpired) {
          // business logic
          return Mono.empty();
        }
        // business logic
        return Mono.empty();
      });
}

上面的示例代码里的注释已经写了if (token == null) {}的这个条件是永远成立的,这是因为当Mono<Token>是empty时,它是不会触发flatMap的。诸如flatMap的绝大部分Operator都依赖于PublisherMonoFlux都是Pubisher)推送数据(详情请看javadoc),如果Publisher本身无数据可推送,那么就不会触发Operator。换句话说flatMap内部是不可能得到null的。

那么怎么做才可以?你可以使用Java 8的Optional来作为中间值:

public Mono<Void> goodCheck(Mono<Token> tokenMono) {
  return tokenMono
      // Transform Mono<Token> to Mono<Optional<Token>>.
      // If Mono<Token> is empty, flatMap will not be triggered,
      // then we will get a empty Mono<Optional<Token>>
      .flatMap(token -> Mono.just(Optional.of(token)))
      // If Mono<Optional<Token>> is empty, provide an empty Optional<Token>,
      // then we will get a non-empty Mono<Optional<Token>> anyway
      .defaultIfEmpty(Optional.empty())
      // Since Mono<Optional<Token>> is not empty, flatMap will always be triggered.
      .flatMap(tokenOptional -> {
        if (!tokenOptional.isPresent()) {
          // business logic
          return Mono.empty();
        }
        Token token = tokenOptional.get();
        if (token.isExpired) {
          // business logic
          return Mono.empty();
        }
        // business logic
        return Mono.empty();
      });
}

除了defaultIfEmpty之外,Reactor还提供了switchIfEmptyrepeatWhenEmpty来处理empty Mono/Flux


chanjarster
4.2k 声望244 粉丝