简体   繁体   中英

How to emit from Flux onComplete

I'm trying to implement something similar to Akka Streams statefulMapConcat... Basically I have a Flux of scores something like this:

Score(LocalDate date, Integer score)

I want to take these in and emit one aggregate per day:

ScoreAggregate(LocalDate date, Integer scoreCount, Integer totalScore)

So I've got an aggregator that keeps some internal state that I set up before processing, and I want to flatmap over that aggregator which returns a Mono. The aggregator will only emit a Mono with a value if the date changes so you only get one per day.

ScoreAggregator aggregator = ...

Flux<Score> scoreFlux = ...

scoreFlux.flatMap(aggregator::addScore)

So my question is... how do I emit a final element when the scoreFlux completes? The aggregator will have some data for the final day that hasn't been emitted yet and I need to get that sent.

Echoing the comment as an answer just so this doesn't show as unanswered:

So my question is... how do I emit a final element when the scoreFlux completes?

You can simply use concatWith() to concatenate the publisher you want after your original flux completes. If you only want that to be evaluated when the original publisher completes, make sure you wrap it up in Mono.defer() , which will prevent the pre-emptive execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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