I'm using the Spring Boot framework as my backend.
This is one of the calls I want to do asynchronously - it just saves a user into my mongoDB database:
@Async
public Future<Void> saveUser(String userid) {
User user = new User();
user.setUserId(userid);
return new AsyncResult<Void>(mongoTemplate.save(user));
}
The method gives me an errors as mongoTemplate.save(user)
returns a void
value and not a Void
object.
I tried to change the method by substituting in void
as follows but it does not work as Future<void>
and AsyncResult<void>
is not accepted:
@Async
public Future<void> saveUser(String userid) {
User user = new User();
user.setUserId(userid);
return new AsyncResult<void>(mongoTemplate.save(user));
}
Is there anyway to run a method that returns void in an asynchronous way on Spring?
Try the following:
@Async
public void saveUser(String userid) {
User user = new User();
user.setUserId(userid);
mongoTemplate.save(user);
}
Future needs to be used only when there is return type other than void.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With