MongoDB的Spring数据中的java非阻塞查询?
我想通过使用MongoDB's Async Client API访问MongoDB的Spring数据执行非阻塞数据库查询
到目前为止,我只看到了返回
java.util.concurrent.Future
java.util.concurrent.CompletableFuture
org.springframework.util.concurrent.ListenableFuture
并用@Async
注释查询方法,例如
public interface UserRepo extends Repository<User, Long> { @Async ListenableFuture<User> findByName(String name); }
但是documentation明确指出实际的[...] query execution will occur in a task that has been submitted to a Spring TaskExecutor
。所以它不是真正的无阻塞,而是使用一个扩展性不好的线程池来解耦我的线程
因此,我的问题是:
如何使用MongoDB异步驱动程序的NIO功能在非阻塞模式下执行查询
到目前为止,我看到的唯一解决方法是摆脱Spring数据,并使用Mongo Async Driver API实现自己的数据库查询。但我希望我只是错过了一些东西,而且有一个非常正面的答案
# 1 楼答案
从Spring Data Kay M1开始,最后引入了一个反应式API,它允许使用ReactiveCrudRepository接口进行非阻塞数据访问
有一个好的blog post of the Spring team about it
现在在Spring Data documentation中也提到了它,但请记住,如果您已经开始使用@Async功能,不将其与新的反应式api混合,那么正如文档明确指出的那样:“异步查询执行与反应式查询执行不同,不应混合。”