我有点纠结于一个琐碎的任务:每当我用reactive spring WebClient或query reactive MongoDBRepository查询外部API时,我都想记录有多少实体通过了我的flux,例如。以记录类似“在数据库中找到n条记录。”的消息。例如:
return repository.findAll()
.doOnComplete { log.info("Found total n records!") } // how to get the n?
.filter { it.age > 10 }
.distinct { it.name }TLDR:如何在完成时获取流量大小(也许还有它的内容)?
发布于 2019-07-16 00:46:32
您可以使用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;发布于 2018-08-02 05:10:42
通过调用count()。当观察到onComplete时,它应该发出一个单声道。
发布于 2021-02-23 17:27:03
这是我所做的,
AtomicInteger i = new AtomicInteger();
Flux<UserDetails> stringFlux =
Flux.using(() -> stringStream, Flux::fromStream,
Stream::close)
.doOnNext(s -> i.getAndIncrement())
.log()
.map(UserDetails::createUserDetails);
stringFlux
.subscribe(updateUserDetailsService::updateUserDetails);
log.info("number of records: {}", i);https://stackoverflow.com/questions/51642123
复制相似问题