结合两个单声道和返回值

Combining two mono and returning value

我正在使用 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。

您有两个调用可以 return 您得到一个空结果。尝试使用 defaultIfEmptyswitchIfEmpty 到 return 默认值。在您的两次调用 return 实际值之前,它不会进入 flatMap