0
votes

I'm dealing with the Cloudfoundry Java client for the following use case:

  • I perform a request that returns a Mono<Void>
  • On success of this Mono, I want to perform an optional operation that returns a Mono<String>
  • For deciding when to perform the second operation, I'm using filter, but it doesn't seem to work

So, it looks like this:

Mono<Void> service = createService();
Mono<String> serviceKey =  service.filter( x -> someBoolean)
   .map( x -> someKey)
   .flatMap(key ->  {
       Mono<String> key = serviceKey(key);
       return key;
     });

serviceKey.blockOptional() //returns Empty

My expectation would be that, when service succeeds and the filter operation is succesfull, the second call serviceKey would happen. However, I saw with the debugger that the code inside flatMap nevers get executed. The javadoc for Mono#filter states:

If this Mono is valued, test the result and replay it if predicate returns true. Otherwise complete without value.

Not sure how to understand that... Question is, how can I chain successful operations when the first one returns a Mono<Void>? I just want to perform the second one when the first is succesful, and return an empty Mono when the filter predicates fails.

1

1 Answers

1
votes

Mono<Void> means "Will either complete without value or error" because you can't instantiate Void type.

What you need is then operator, it ignores the previous result and "switches" the flow to the provided Mono.
There is also thenMany in case you need to "switch" it to Flux.