【发布时间】:2020-09-14 06:26:09
【问题描述】:
我正在尝试从弹簧控制器异步保存文档。由于它经常会失败,因此我希望在异步运行的服务上使用以下方法,以便我可以重复调用。 UI 客户端不需要等待它完成。
@Service
public class AsyncService {
@Autowired
DocumentClient documentClient;
@Async
public void save(Document document) {
int tryCount = 0;
while (tryCount < RETRY_LIMIT) {
try {
tryCount++;
documentClient.save(document);
return;
} catch (Exception e) {
if (tryCount < RETRY_LIMIT) {
log.info("Retrying save document");
}
}
}
}
}
当我在documentClient.save(document); 行设置断点时,它处于阻塞状态,控制器正在等待。但是当我在“返回”处设置断点时,调用异步执行并且控制器返回,而我仍然在断点处暂停。任何想法为什么会发生这种情况?我认为整个方法save(Document document) 将是非阻塞的。
我使用 Springboot 1.5 和 Intellij 作为调试器/运行时。
【问题讨论】:
-
断点会导致所有线程在那一刻暂停。因此,当它启动时,一切都会暂停。因此,在保存时设置它时,它也会暂停主执行线程。
-
检查this
-
可能你忘记用@EnableAsync注释主类了。
标签: java spring spring-boot asynchronous