小编iso*_*yer的帖子

我可以在 Spring Boot 中返回 API 响应而无需等待其他外部 API 调用吗?

@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()完成时间太长并且不直接绑定到响应对象。

java spring asynchronous spring-boot spring-async

0
推荐指数
1
解决办法
6936
查看次数

标签 统计

asynchronous ×1

java ×1

spring ×1

spring-async ×1

spring-boot ×1