简体   繁体   中英

Non-blocking Queries in Spring Data for MongoDB?

I want to execute non-blocking database queries through Spring Data accessing a MongoDB using MongoDB's Async Client API .

So far I've only seen the possibility to return a

  • java.util.concurrent.Future
  • java.util.concurrent.CompletableFuture
  • org.springframework.util.concurrent.ListenableFuture

and annotate the query method with @Async , eg

public interface UserRepo extends Repository<User, Long> {

  @Async
  ListenableFuture<User> findByName(String name);

}

but the documentation clearly states that the actual [...] query execution will occur in a task that has been submitted to a Spring TaskExecutor . So it's not really non-blocking but just decoupling my thread using a thread pool which doesn't scale very well.

Therefore my question:

How to execute the queries in non-blocking mode using the MongoDB Async Driver's NIO Features?

The only workaround I see so far is to get rid of Spring Data and implement the database queries by my own using the Mongo Async Driver API . But hopefully I'm just missing something and there is a straigth-forward answer out there. ;)

So it's not really non-blocking but just decoupling my thread using a thread pool which doesn't scale very well.

Non-Blocking/Asynchronous mean delegating a task to a different thread and not using the calling thread to complete the task. Spring Data mongodb implementations stands for its claims.

But you can't say that it will not scale as you expect because you can configure the Spring TaskExecutor to cater to your scaling SLAs.

Remember Spring data mongodb is an abstraction layer used to simply the development life cycle and is good for most of the development use cases and provides good non-blocking/asynchronous executions also. But if you think you need more then you have to stick with MongoDB's own Aync Client API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM