Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negation of filterWhen and hasElement

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().

like image 295
baao Avatar asked Jul 20 '18 08:07

baao


People also ask

What is the difference between mono and flux?

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.

How do I cancel flux?

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.

How do you convert flux to Mono?

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.

How do you use onErrorResume?

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.


1 Answers

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().

like image 167
Lino Avatar answered Oct 20 '22 07:10

Lino