Reputation: 19938
Is there a way to insert something asynchronously into mongodb?
I know that mongodb is fairly quick in most cases, but thought maybe I can save some milliseconds by returning as soon as the command is given.
It is for those use cases where you connect to the server to send mongodb a command to insert something. You want to return back to the client once the command is issued and not necessarily wait for a response from mongodb.
I read the documentation: http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/
It only seems to read asynchronously, not insert asychronously.
Upvotes: 3
Views: 5482
Reputation: 137084
The Spring Data MongoDB documentation only shows example of using the @Async
annotation on query methods, but it is possible to use it on every method.
Quoting this documentation:
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.
Asynchronous invocation of methods is not a Spring Data concern but a Spring Core concern so you can refer to Spring framework documentation.
Simply said, you just need to add the @Async
annotation on the method you want and configure a proper task executor in Spring configuration. A sample XML configuration would be:
<task:annotation-driven executor="myExecutor" />
<task:executor id="myExecutor" pool-size="5"/>
Upvotes: 2