java - 结合两个单声道和返回值
问题描述
我正在使用 2 Mono 并将它们组合起来以执行某些任务,但它没有进入 flatMap 块内。
public Mono<Invocable> getJSCompiledInstance() {
return Mono.fromFuture(jsScriptCache.get(SCRIPT_KEY).toCompletableFuture());
}
private AsyncLoadingCache<String, Invocable> jsScriptCache;
private AsyncLoadingCache<Key, String> ruleSetVersionsCache;
private AsyncLoadingCache<String, String> currentlyLiveSetVersionCache;
public Mono<Object> evaluateVersion(Key key, String facts, boolean showTestResults) {
System.out.println("Inside Evaluate By Version");
return getJSCompiledInstance().zipWith(Mono.fromFuture(ruleSetVersionsCache.get(key).toCompletableFuture()))
.flatMap(tuple -> {
String ruleSetVersion = tuple.getT2();
Invocable inv = tuple.getT1();
if (ruleSetVersion.equals("null")) {
return Mono.error(new RuntimeException("Unable to fetch currently live rule set version for" +
"for rule set id " + key.getRuleSetId()));
}
try {
JSONObject ruleSetVersionObject = new JSONObject(ruleSetVersion);
String ruleSetVersionId = ruleSetVersionObject.get("id").toString();
Object result = inv.invokeFunction("evaluate", key.getRuleSetId(), ruleSetVersion, facts, ruleSetVersionId, showTestResults);
System.out.println("===================");
System.out.println(result);
System.out.println("===================");
return Mono.just(result);
} catch (Exception e) {
e.printStackTrace();
return Mono.error(new RuntimeException(e.getMessage()));
}
});
}
flatMap 内没有 println 被打印出来。
解决方案
您有两个调用可以返回一个空结果。尝试使用defaultIfEmpty
或switchIfEmpty
返回默认值。直到您的两个调用都返回实际值,它才会进入flatMap
推荐阅读
- javascript - 通过执行 javascript 在 python 中使用 selenium webdriver 隐藏元素
- java - 通过 XML Parser on Fly 将 ZonedDateTime 字符串映射到 LocalDateTime
- java - 如何使用 Mysql 数据库获取当前登录的用户 ID 并将该 ID 作为数据上传到 Android
- html - 父 div 中的 Twitter 引导中心 div
- functional-programming - 带有 IF 的 Lisp Lamba 函数
- php - 如何显示每个组的所有行
- java - 试图设置一个抽象的监听器
- ios - UITableViewAlertForLayoutOutsideViewHierarchy当UITableView没有窗口iOS13
- node.js - 在nodejs中将字符串转换为ISO日期时间
- keyboard - 删除对 WIN+L 键盘快捷键的访问