演示应用程序的设置如下:
我有一个服务,它根据加密货币集合中的货币返回Flux
of List<CryptoCurrencyRateDTO>
。我为每种货币生成一个随机的汇率,并将它们流到网络客户端。
这项服务是:
@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();
}
}
CryptoCurrencyRateRepository
如下:
@Repository
public interface CryptoCurrencyRateRepository extends ReactiveMongoRepository<CryptoCurrencyRate, String> {
Mono<CryptoCurrencyRate> findTopBySymbolOrderByTimestamp(String symbol);
}
但是,在调用.flatMap(rateRepository::findTopBySymbolOrderByTimestamp)
之后,我只得到一个包含1项的Flux
,而我想我会得到一个Flux
,其中包含来自currencyRepository.findAll().map(CryptoCurrency::getSymbol)
调用的每个符号的最高速率,因为我的加密货币集合包含3种货币。
当我查看日志记录时,我看到对findTopBySymbolOrderByTimestamp
的调用执行了3次
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
发布于 2018-11-22 00:10:28
看来我的期望是正确的。唯一出错的是,并不是所有的物体都因为缺少身份证而被保存在蒙戈。
发布于 2018-11-21 09:44:36
我无法重现你的问题。我就是这样模仿的
public static void main(String[] args) {
Flux<String> stringFlux = Flux.fromStream(Stream.of("a", "b", "c"));
System.out.println(realtimeRates(stringFlux).blockFirst());
}
static Flux<List<String>> realtimeRates(Flux<String> list) {
Flux<String> symbols = list.map(Scratch::getSymbol);
Flux<String> topRates = symbols.flatMap(Scratch::findTopBySymbolOrderByTimestamp);
Flux<String> zip = topRates.zipWith(
Flux.<Long>generate(sink -> sink.next(Instant.now().toEpochMilli())),
(rate, timestamp) -> rate + timestamp.toString());
Mono<List<String>> listMono = zip.collectList();
Mono<List<String>> delayElement = listMono.delayElement(Duration.ofSeconds(5));
Flux<List<String>> repeat = delayElement.repeat();
return repeat;
}
static Mono<String> findTopBySymbolOrderByTimestamp(String symbol) {
return Mono.just("other-" + symbol);
}
static String getSymbol(String rate) {
return rate.toLowerCase();
}
如您所见,您将得到类似于[other-a1542821666133, other-b1542821666133, other-c1542821666133]
的内容。
你是如何检查平面图结果的?请注意,如果使用blockFirst()
或blockLast()
方法执行此操作,则只会得到一个元素,因为它是一个Flux<String>
(请检查上面代码中的topRates
变量)。
https://stackoverflow.com/questions/53340911
复制相似问题