我有一个接受参数并返回结果的methodA
。我正在编写一个响应式方法来批量调用该函数。但我不懂反应式语法。我的代码看起来像这样
List<GetResult> successfulResults =
Collections.synchronizedList(new ArrayList<>());
Map<String, Throwable> erroredResults = new ConcurrentHashMap<>();
Flux.fromIterable(docsToFetch).flatMap(key -> reactiveCollection.getAndTouch(key, Duration.ofMinutes(extendExpiryInMin))
.onErrorResume(e -> {
erroredResults.put(key, e);
return Mono.empty();
})
).doOnNext(successfulResults::add).last().block();
当前实现调用该方法,但在list中收集结果。在列表中收集结果对我的用例没有意义。我想把结果收集到key和result的hashmap中。
解决方案是
List<String> docsToFetch = Arrays.asList("airline_112", "airline_1191", "airline_1203");
Map<String, GetResult> successfulResults = new ConcurrentHashMap<>();
Map<String, Throwable> erroredResults = new ConcurrentHashMap<>();
Flux.fromIterable(docsToFetch).flatMap(key -> reactiveCollection.get(key).onErrorResume(e -> {
erroredResults.put(key, e);
return Mono.empty();
}).doOnNext(getResult -> successfulResults.put(key, getResult))).last().block();