How to wrap synchronous method that returns `void` into Mono<Void>?

3.5k views Asked by At

I'm trying to adapt the postgresql repository deleteById method which return void to return Mono<Void>

the repository is a service that I autowired, I use it like this

repository.deleteById(id) with String id as the argument

1

There are 1 answers

5
sawim On

If you want to use reactive stack, then you should use spring-data-r2dbc, which provides ReactiveCrudRepository and method Mono<Void> deleteById(ID id).

If for some reason you need to use a synchronous method, then you could wrap deleteById call using Mono.fromRunnable

Mono.fromRunnable(() -> repository.deleteById(someId))

However, this should be avoided, because then you get no benefits of using reactive stack.