I'm learning Flux and Mono and am hitting a wall when trying to check if a key doesn't exist in redis. I need to filter my flux and want to remove the element if the key exists. I don't find a way to do this in the docs, all I can filter is to keep if the key exists. I need the opposite.
client.request(MWS_EU, Orders, ordersRequest, ListOrdersResponse.class)
.flatMapIterable(listOrdersResponse -> listOrdersResponse.getOrders())
.filterWhen(order -> isNewOrder(order.getOrderId()))
.flatMap(...)
and the check for redis:
private Mono<Boolean> isNewOrder(String orderId) {
return redisOrders.opsForValue().get(orderId).hasElement();
}
What is a clean way to filter my Flux to keep elements only if the key does not exist?
I don't want to use block()
.
Mono is more relatable to the Optional class in Java since it contains 0 or 1 value, and Flux is more relatable to List since it can have N number of values.
You can access your subscription plan in your Flux:: user account Subscriptions Section, where you can administer and cancel your subscription plan. When cancelling a subscription plan, this will be effectuated at the end of the commitment period.
3 Answers 3. Instead of take(1) , you could use next() . This will transform the Flux into a valued Mono by taking the first emitted item, or an empty Mono if the Flux is empty itself.
There are three ways that we can use onErrorResume to handle errors: Compute a dynamic fallback value. Execute an alternative path with a fallback method. Catch, wrap and re-throw an error, e.g., as a custom business exception.
You can change your method to the following:
private Mono<Boolean> isNewOrder(String orderId) {
return redisOrders.opsForValue().get(orderId).hasElement().map(b -> !b);
}
By using the map()
method you can just invert the value returned by hasElement()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With