Pio*_*łek 6 java kotlin reactor-netty spring-webflux
我有点陷入一个琐碎的任务:每当我使用反应式 Spring WebClient 查询外部 API 或查询反应式 MongoDBRepository 时,我想记录有多少实体通过了我的 Flux,例如。记录诸如“在数据库中找到 n 条记录”之类的消息。例如:
return repository.findAll()
.doOnComplete { log.info("Found total n records!") } // how to get the n?
.filter { it.age > 10 }
.distinct { it.name }
Run Code Online (Sandbox Code Playgroud)
TLDR:完成后如何获取通量大小(也许还有它的内容)?
您可以使用ConnectableFlux。在你的例子中:
var all = repository.findAll()
.filter { it.age > 10 }
.distinct { it.name }
.publish()
.autoConnect(2)
all.count()
.subscribe {c -> log.info("Found total {} records!", c)}
return all;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16054 次 |
| 最近记录: |