I have to implement a method with async features in spring boot:
I am a bit confused regarding the location of the annotation asyn, basically my rest controller is as follows:
@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);
}
And service class is as follows:
@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");
...
}
I have configured my async bean as follows:
@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;
}
My question is the annotation @Async on the SendMailServiceImpl is correct or i need to add it on the method sendMailCon from controller?