I am new to java reactive and am spinning my wheels on what I would think is a pretty easy thing to do.
My intent is to evaluate the result of a method call that returns a Mono<Boolean> type value and then determine a course of action based on that result. In the below example, if fieldAExists is true, I want to run the code that performs the update at the latter part of the addFieldA method.
How is a Boolean value extracted from a Mono<Boolean> type value? Is it possible to extract the value out from the Mono<Boolean> field? I tried working with subscribe() but could not get it to return a value to me evaluate.
Can the two reactive statements be combined to form one statement? Any direction you can offer is appreciated.
public Mono<Boolean> addFieldA(Email email, String fieldA) {
Mono<Boolean> fieldAExists = checkFieldAExistence(email);
// if fieldAExists is true, call the below.
return reactiveMongoTemplate.updateMulti(query(where("customer.email.address").is(email.address())),
new Update().set("customer.publicId", fieldA), Account.class, accountCollection).map(result -> {
if (result.wasAcknowledged()) {
return true;
} else {
throw new IllegalArgumentException(
"Error adding fieldA value to customer with email address " + email.address());
}
});
}
public Mono<Boolean> checkFieldAExistence(Email email) {
return reactiveMongoTemplate
.findOne(query(where("customer.email.address").is(email.address()).and("customer.fieldA").ne(null)),
Account.class, accountCollection)
.map(found -> true).switchIfEmpty(Mono.just(false));
}