在另一个方法之后执行一个方法而不是等待其执行的最佳方法是什么:
例如,我有一个spring boot api:
@GetMapping("/users/{performanceCode}")
@ResponseBody
public ResponseEntity<UserDTO> getUserPerformance(@PathVariable String performanceCode) {
return new ResponseEntity(userService.getUserPerformance(performanceCode), HttpStatus.OK);
}
上 userService.getUserPerformance(performanceCode)
我必须做些什么
@Transactional
public UserDTO getUserPerformance(String performanceCode) {
UserDTO userDTO = // some logic..... ;
//... some other code
// here I call this method that I don't have to wait for it to finish and also if it throws an error I shouldn't block the whole service to return error
userCalculationsService.resetUserPerformances(userDTO );
return userDTO;
}
所以我的问题是我想打电话 userCalculationsService.resetUserPerformances(userDTO )
但是我不想等到它完成,或者如果它在方法中抛出错误,我不应该阻止整个服务返回错误。
有人能帮我处理这件事吗?
1条答案
按热度按时间dy2hfwbg1#
在springboot应用程序主类中:
在控制器中,可以调用异步方法:
异步服务类: