use*_*453 5 java spring-boot spring-async
我必须在 Spring Boot 中实现具有异步功能的方法:
我对注释异步的位置有点困惑,基本上我的休息控制器如下:
@RestController
@RequestMapping("/email")
public class EmailController {
public @ResponseBody ResponseEntity<String> sendMailCon(@RequestBody EmailRequestDto emailRequestDto) {
LOG.debug("calling method sendMail from controller ");
//do complex stuff
sendMailService.sendEmail(emailRequestDto);
return new ResponseEntity<>("Mail has been sent successfully", HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
服务等级如下:
@Component
public class SendMailServiceImpl implements SendMailService {
private static final Logger LOG = LoggerFactory.getLogger(SendMailServiceImpl.class);
@Autowired
private JavaMailSender javaMailSender;
@Override
@Async("threadPoolExecutor")
public void sendEmail(EmailRequestDto emailRequestDto) {
LOG.debug("calling method sendMail do complex stuff");
...
}
Run Code Online (Sandbox Code Playgroud)
我的异步 bean 配置如下:
@EnableAsync
@Configuration
public class AsyncConfig {
@Bean(name = "threadPoolExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(25);
executor.setQueueCapacity(100);
executor.initialize();
return executor;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 SendMailServiceImpl 上的注释 @Async 是正确的,还是我需要将其添加到控制器的 sendMailCon 方法上?
| 归档时间: |
|
| 查看次数: |
5261 次 |
| 最近记录: |