I’ve been developed a spring web application based on webflux(which is based on project-reactor) over the past a few months. Even I thought that I’am accustomed to the reactor operators, I found something weird, specifically in ReactiveMongoRepository
.
Here is a code which I’m confused with.
// interface UserRepository : ReactiveMongoRepository<User, String>
userRepository.findByNickName("foo") // returns Mono<User>
.switchIfEmpty { Mono.error(UserNotFoundException()) } // error if mono was empty
.retry(RetrySpec.fixedDelay(...)) // retry to find user
As you see, I wanted to re-try to execute the query again, if user was not found on the first trial. However, it doesn’t work as expected. the userRepository.findByNickName
returns same result as Mono.empty
, and also didn’t execute any query for re-trial.
Although I resolved this by using Mono.defer { userRepository.findByNickName() }
, but still can’t understand why the query not executed.
As I know, the cold publisher creates the data on subscriber’s subscription, and the hot publisher can create the data before the subscription(Like Mono.just("foo")
) So I thought that userRepository.findByNickName
returns hot publisher, but on the other side, it doesn’t make sense because the query should be executed after a subscription happens. (Maybe I might be confused the hot and cold publisher, not the repository one)
I’v read some articles explaining the difference between hot and cold publisher, tried to reproduce this by simple test code, digged into the source code of SimpleReactiveMongoRepository
, but couldn’t find why.
Can anybody explain me the reason why is this happening ?