简体   繁体   English

MongoDb async/insertData、sync/getData:使用 ReactiveMongoTemplate

[英]MongoDb async/insertData, sync/getData: using ReactiveMongoTemplate

I have a situation where I need to insert in async and get data (obviously it should be in sync) currenlty am using ReactiveMongoTemplate我有一种情况需要插入异步并获取数据(显然它应该是同步的)当前我正在使用 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()我试图用 toIterable() / toStream() 来阻止.find

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?我是否应该只使用常规的 mongo 模板来获取详细信息,以便结果同步,在同一个项目中使用 ReactiveMongoTemplate 和 MongoTemplate 是一种好习惯吗?

Kindly help, Thanks请帮助,谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM