Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert normal CrudRepository<T,ID> to ReactiveCrudRepository<T,ID>

I am using spring-data-jpa and spring webflux. When i am extending my UserRepository with ReactiveCrudRepository. i am getting below error:

org.springframework.dao.InvalidDataAccessApiUsageException: Reactive Repositories are not supported by DynamoDB. Offending repository is com.poc.crud.repository.EmployeeRepository!

If i extend with CrudRepository and send response with Mono.just(data-from-db) and Flux.just(data-from-db) then every thing is fine.

My question is , How can i create a custom generic ReactiveCrudRepository<T,ID> so that all crud method will return Mono and Flux objects.

like image 930
P3arl Avatar asked Oct 15 '22 09:10

P3arl


1 Answers

If i extend with CrudRepository and send response with Mono.just(data-from-db) and Flux.just(data-from-db) then everything is fine.

Woah there - no, it's not. You might think it's all fine, but this is introducing blocking webcalls into a reactive chain, which will slow things down horrendously as soon as you have a few calls running in parallel. Worse than that, you've created a method that "looks" reactive, but isn't - commonly known as an "imposter reactive method".

Put simply, it's impossible to take blocking code and make it reactive - wrapping it in Mono.just() doesn't work. The library has to be built asynchronously from the ground up.

The bad news is that, as far as I'm aware, there's currently (Nov 2019) no ReactiveCrudRepository repository support for DynamoDB, so if you have to use this, you're a bit stuck.

The good news is that Amazon does now provide an asynchronous client for DynamoDB, and you can easily wrap a CompleteableFuture in a Mono by calling Mono.fromFuture() - so you could use that to stay reactive and still have DynamoDB support.

like image 110
Michael Berry Avatar answered Oct 19 '22 02:10

Michael Berry