@Async这是Spring Boot 中正确的使用方法吗?
@Service
class someServiceImpl {
...
public someResponseDTO getUsers(int userId) {
// Do some logic
...
// Call external API with another service method from another service impl
anotherService.emailUserInTheBackground(userId);
return someResponseDTO;
}
...
}
Run Code Online (Sandbox Code Playgroud)
@Service
public class AnotherService {
@Async
public void emailUserInTheBackground(int userId) {
// This might take a while...
...
}
}
Run Code Online (Sandbox Code Playgroud)
既然emailUserInTheBackground()有@Async注释和返回类型,它会完全void阻塞该行吗?return someResponseDTO
我想要的只是将响应返回给调用者而无需等待,因为emailUserInTheBackground()完成时间太长并且不直接绑定到响应对象。