首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring reactive:链接库结果

Spring reactive:链接库结果
EN

Stack Overflow用户
提问于 2020-03-10 00:00:43
回答 2查看 189关注 0票数 1
代码语言:javascript
复制
Repository repo
Repository otherRepo

foreach entity : repo.FindAll() {
    entityFind = otherRepo.FindById(entity.Prop)
    if (entityFind != null) {
        return entityFind 
    }
}

我如何使用spring reactive来做到这一点呢?

我可以使用blockFirst()在otherRepo中进行搜索,但它会中断反应链

我也尝试过使用句柄()来控制流程,但当我找到一个项目时,我无法中断流程

有什么想法吗?谢谢

EN

回答 2

Stack Overflow用户

发布于 2020-03-10 00:45:43

如果您有类似这样的repos,对于repo1的每条记录,如果您需要从repo2中查找一条记录,您可能可以使用spring data JPQL连接表&使用您的自定义方法,因为您当前的方法可能会对性能产生影响。

因为你似乎只对第一张记录感兴趣,只是为了给你一个想法,我们可以实现这样的事情。

代码语言:javascript
复制
return Flux.fromIterable(repo.findAll()) //assuming it returns a list
           .map(entity -> otherRepo.findById(entity.property)) // for each entity we query the other repo
           .filter(Objects::nonNull) // replace it with Optional::isPresent if it is optional
           .next();   //converts the flux to mono with the first record
票数 1
EN

Stack Overflow用户

发布于 2020-03-11 21:16:49

来自vins的答案是假设非反应式存储库,所以这里是完全反应式的:

代码语言:javascript
复制
return repo.findAll() //assuming reactive repository, which returns Flux<Entity>
    .flatMap(entity -> otherRepo.findById(entity.property)) //findById returns an empty Mono if id not found, which basically gets ignored by flatMap
    .next(); //first record is turned into a Mono, and the Flux is cancelled

注意,正如您所说的,这可能会导致向Cassandra发出不必要的请求(然后被next()取消)。这是由于flatMap允许多个并发请求(默认情况下为256个)。您可以减少flatMapparallelism (通过提供第二个参数int),也可以使用concatMap串行执行findById查询。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60604247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档