I have the following setup for a demo application:
- mongodb containing 2 collections: 1 with cryptocurrencies and 1 with the exchange rates of these cryptocurrencies
- spring webflux project to fetch realtime updates to these exchange rates using Server Sent events
I have a service which returns a Flux of List<CryptoCurrencyRateDTO> based on the currencies present in the cryptocurrency collection. I generate a random exchange rate for each of these currencies and stream them to the web client.
The service is this:
@Service
public class CryptoCurrencyRateService {
@Autowired private CryptoCurrencyRateRepository rateRepository;
@Autowired private CryptoCurrencyRepository currencyRepository;
// constructor
public Flux<List<CryptoCurrencyRateDTO>> realtimeRates() {
return currencyRepository.findAll()
.map(CryptoCurrency::getSymbol)
.flatMap(rateRepository::findTopBySymbolOrderByTimestamp)
.zipWith(
Flux.<Long>generate(sink -> sink.next(Instant.now().toEpochMilli())),
(rate, timestamp) -> new CryptoCurrencyRate(rate.getSymbol(), timestamp, randomRateBasedOnPrevious )
)
.flatMap(rateRepository::save)
.map(rateMapper::toDto)
.collectList()
.delayElement(Duration.ofSeconds(5))
.repeat();
}
}
The CryptoCurrencyRateRepository is as follows:
@Repository
public interface CryptoCurrencyRateRepository extends ReactiveMongoRepository<CryptoCurrencyRate, String> {
Mono<CryptoCurrencyRate> findTopBySymbolOrderByTimestamp(String symbol);
}
However after the call to .flatMap(rateRepository::findTopBySymbolOrderByTimestamp) I only get a Flux containing 1 item while I thought I would get a Flux containing the top rate for each symbol from the currencyRepository.findAll().map(CryptoCurrency::getSymbol) call because my cryptocurrency collection contains 3 currencies.
When I look in the logging I see that the call to findTopBySymbolOrderByTimestamp is executed 3 times
2018-11-16 16:04:33.626 DEBUG 3387 --- [ntLoopGroup-2-3] o.s.d.m.core.ReactiveMongoTemplate : find using query: { "symbol" : "BTC" } fields: Document{{}} for class: class nl.reactive.charts.server.domain.CryptoCurrencyRate in collection: cryptoCurrencyRate
2018-11-16 16:04:33.627 DEBUG 3387 --- [ntLoopGroup-2-3] o.s.d.m.core.ReactiveMongoTemplate : find using query: { "symbol" : "ETH" } fields: Document{{}} for class: class nl.reactive.charts.server.domain.CryptoCurrencyRate in collection: cryptoCurrencyRate
2018-11-16 16:04:33.629 DEBUG 3387 --- [ntLoopGroup-2-3] o.s.d.m.core.ReactiveMongoTemplate : find using query: { "symbol" : "XRP" } fields: Document{{}} for class: class nl.reactive.charts.server.domain.CryptoCurrencyRate in collection: cryptoCurrencyRate