0

I have a situation where I need to insert in async and get data (obviously it should be in sync) currenlty am using ReactiveMongoTemplate

//insert async
reactiveMongoTemplate.save(MyObject)
// get data in sync
reactiveMongoTemplate.find(Query.query(Criteria.where("myId")
                .in("myList")), MyObject.class);

with above code both are in async

I tried to block .find with toIterable() / toStream()

reactiveMongoTemplate.find(Query.query(Criteria.where("myId")
                .in("myList")), MyObject.class).toIterable().forEach(myRecord -> {
                    // process response
                });

but they are failing with below error

Iterating over a toIterable() / toStream() is blocking, which is not supported in thread reactor-http-nio-3

More code

webClient.post()
    .uri("serviceurl")
    .headers("headers")
    .body(Mono.just(body), JsonNode.class)
    .retrieve()
    .bodyToMono(MyObject.class)
    .flatMap(responseObject -> {
        Map<String, MySubResponse> repMap = responseObject.
                .stream()
                .collect(Collectors.toMap("key", "value"));
        reactiveMongoTemplate.find(Query.query(Criteria.where("myId")
                .in("myList")), MyObject.class).toIterable().forEach(val -> {
                        MySubResponse mySubResponse = repMap.get("val.key");
                        mySubResponse.setMyProperty(val.getProperty)
                    }
                });
        return repMap.values();
    });

any suggestion on how can I achieve this?

any other alternative?

are should I just use regular mongo template to get details, so results will be in sync, is it a good practise to use ReactiveMongoTemplate and MongoTemplate in a same project ?

Kindly help, Thanks

1 Answer 1

0

am able to make it work using below code

.collectList() // Collects all elements emitted by this Flux into a List 
.toFuture() // waits and returns CompletableFuture
.join(); // return's the data instead of CompletableFuture

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.