Home > database >  how to render an object with two flux fields without blocking?
how to render an object with two flux fields without blocking?

Time:11-18

I want to render an object composed of two mono or flux elements (below a code snippet):

 Mono<List<NodeDTO>> nodeDTOFlux = this.webClient
            .get()
            .uri(NODES_WITH_LIMIT   limit)
            .retrieve()
            .onStatus(HttpStatus::isError,
                    response -> response.bodyToMono(String.class).flatMap(
                            msg -> Mono.error(new ApiCallException(msg, response.statusCode())))
            )
            .bodyToFlux(new ParameterizedTypeReference<Node>() {
            }).map(node -> nodeMapper.toNodeDTO(node))
            .collectList();



    Mono<List<EdgeDTO>> edgeDTOFlux = this.webClient
            .get()
            .uri(EDGES_WITH_LIMIT   limit)
            .retrieve()
            .onStatus(HttpStatus::isError,
                    response -> response.bodyToMono(String.class).flatMap(
                            msg -> Mono.error(new ApiCallException(msg, response.statusCode())))
            )
            .bodyToFlux(new ParameterizedTypeReference<Edge>() {
            }).map(edge -> edgeMapper.toEdgeDTO(edge))
            .collectList();

I tried with zip() method but it's not what I aim to do I tried to return an object like this

    GraphDataDTO graphDataDTO = new GraphDataDTO();
    graphDataDTO.setEdgeDTOS(edgeDTOFlux);
    graphDataDTO.setNodeDTOS(nodeDTOFlux);

I have a result in my console but the object returned { "nodeDTOS": { "scanAvailable": true }, "edgeDTOS": { "scanAvailable": true } } the return is done before getting all the flux.. is there any solution without blocking ! thanks in advance.

CodePudding user response:

This should work:

return Mono.zip(nodeDTOFlux, edgeDTOFlux)
      .map(tuple2 -> GraphDataDTO.builder().nodeDTO(tuple2.getT1()).edgeDTO(tuple2.getT2()).build())

It creates a Tuple of NodeDTO and EdgeDTO and maps it into GraphDataDTO.

  • Related