Sahil Paudel
Sahil Paudel

Reputation: 474

Combining two mono and returning value

I am using 2 Mono and combining them to perform a certain tasks but it is not going inside the flatMap block.

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()));
                    }
                });
    }

No println inside the flatMap is getting printed.

Upvotes: 0

Views: 746

Answers (1)

Vova Bilyachat
Vova Bilyachat

Reputation: 19514

You have two calls which can return you an empty result. Try to use defaultIfEmpty or switchIfEmpty to return default values. Until both of your calls return actual value it wont get into flatMap

Upvotes: 2

Related Questions