Java:异步 MongoTemplate / MongoOperation

Java: Async MongoTemplate / MongoOperation

有没有办法将一些东西异步插入 mongodb?

我知道 mongodb 在大多数情况下相当快,但我想也许我可以通过 return 命令一发出就节省几毫秒。

它适用于连接到服务器以发送 mongodb 插入内容的命令的那些用例。您希望在发出命令后 return 返回客户端,而不必等待 mongodb.

的响应

我阅读了文档:http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/

好像只有异步读取,没有异步插入

Spring Data MongoDB documentation 仅显示了在查询方法上使用 @Async 注释的示例,但可以在每个方法上使用它。

引用此文档:

Repository queries can be executed asynchronously using Spring’s asynchronous method execution capability. This means the method will return immediately upon invocation and the actual query execution will occur in a task that has been submitted to a Spring TaskExecutor.

方法的异步调用不是Spring数据问题而是Spring核心问题所以你可以参考Spring framework documentation.

简单地说,你只需要在你想要的方法上添加@Async注释,并在Spring配置中配置一个合适的任务执行器。示例 XML 配置为:

<task:annotation-driven executor="myExecutor" />
<task:executor id="myExecutor" pool-size="5"/>