【问题标题】:How to check Flux<Object> is empty or not?如何检查 Flux<Object> 是否为空?
【发布时间】:2021-03-15 05:23:37
【问题描述】:

我有一个 api 供 kubernetes 调用并检测服务是否可用。在该 api 中,首先调用一个接口获取其他服务的主机,该接口返回一个 Flux,如果结果为空 api 返回 SERVICE_UNAVAILABLE 其他返回好的。我当前的代码如下:

@GetMapping(value = "/gateway/readiness")
public Mono<Long> readiness(ServerHttpResponse response) {
    Flux<Map<String, List<String>>> hosts = hostProvider.getHosts();
    List<String> hostProviders = new ArrayList<>();
    
    // the below code has a warning: Calling subscribe in a non-blocking scope
    hosts.subscribe(new Consumer<Map<String, List<String>>>() {
        @Override
        public void accept(Map<String, List<String>> stringListMap) {
            hostProviders.addAll(stringListMap.keySet());
        }
    });
    if (hostProviders.isEmpty()) {
        response.setStatusCode(HttpStatus.SERVICE_UNAVAILABLE);
    }
    return routeLocator.getRoutes().count();
}

有优雅这样做吗?

【问题讨论】:

  • 你不应该写一个map,然后改用defaultIfEmpty吗?

标签: java spring spring-boot spring-webflux reactor


【解决方案1】:

请尝试以下方法:

@GetMapping(value = "/gateway/readiness")
public Mono<ServerResponse> readiness() {
    return hostProvider.getHosts()
            .map(Map::keySet)
            .flatMap(set -> Flux.fromStream(set.stream()))
            .collectList()
            .flatMap(hostProviders -> 
               // response whatever you want to the client
               ServerResponse.ok().bodyValue(routeLocator.getRoutes().count())
            )
            // if no value was propagated from the above then send 503 response code
            .switchIfEmpty(ServerResponse.status(HttpStatus.SERVICE_UNAVAILABLE).build());
}

【讨论】:

    【解决方案2】:

    你应该像这样重写你的代码:

    @GetMapping(value = "/gateway/readiness")
    public Mono<ResponseEntity<Long>> readiness() {
        Flux<Map<String, List<String>>> hosts = Flux.empty();
        return hosts
                .flatMapIterable(Map::keySet)
                .distinct()
                .collectList()
                .map(ignored -> ResponseEntity.ok(1L))
                .defaultIfEmpty(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build());
    }
    

    【讨论】:

    • 我用这个方法得到一个异常:java.lang.NullPointerException: null
    猜你喜欢
    • 1970-01-01
    • 2023-02-07
    • 2017-11-15
    • 2020-05-29
    • 2018-04-02
    • 2016-04-05
    • 2018-01-08
    • 2020-09-25
    • 2012-01-15
    相关资源
    最近更新 更多