MongoDB 的 Spring 数据中的非阻塞查询?
Non-blocking Queries in Spring Data for MongoDB?
我想通过 Spring 数据访问 MongoDB 使用 MongoDB's Async Client API.
执行非阻塞数据库查询
到目前为止,我只看到 return 一个
的可能性
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 自行实现数据库查询。但希望我只是遗漏了一些东西并且那里有一个直截了当的答案。 ;)
从 Spring Data Kay M1 finally a reactive API has been introduced that allows non-blocking data access using the ReactiveCrudRepository 界面开始。
有个不错的blog post of the Spring team about it.
现在在 Spring Data documentation 中也提到了它,但请记住,如果您已经通过使用 @Async 功能开始您的项目,不要将它与新的反应式 api 混合使用,正如文档明确指出的那样:"Asynchronous query execution is different from reactive query execution and should not be mixed."
我想通过 Spring 数据访问 MongoDB 使用 MongoDB's Async Client API.
执行非阻塞数据库查询到目前为止,我只看到 return 一个
的可能性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 自行实现数据库查询。但希望我只是遗漏了一些东西并且那里有一个直截了当的答案。 ;)
从 Spring Data Kay M1 finally a reactive API has been introduced that allows non-blocking data access using the ReactiveCrudRepository 界面开始。
有个不错的blog post of the Spring team about it.
现在在 Spring Data documentation 中也提到了它,但请记住,如果您已经通过使用 @Async 功能开始您的项目,不要将它与新的反应式 api 混合使用,正如文档明确指出的那样:"Asynchronous query execution is different from reactive query execution and should not be mixed."