What is the best way to implement @Async
method that will be executed X number of time per Y number of milliseconds/seconds.
I have requirement set by Amazon SES to send only 14 emails per second, which I'm doing asynchronously using Spring's @Async annotation but as I know I can only set max pool and max queue size and not rate.
Here is what I have:
@Bean(name = "emailSenderThreadPoolTaskExecutor")
public Executor emailSenderThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(14); // send 14 at once
executor.setMaxPoolSize(14);
executor.setThreadNamePrefix("EmailThreadPool-");
executor.initialize();
return executor;
}
and then
@Async(value = "emailSenderThreadPoolTaskExecutor")
public void sendEmail(String emailTo, String subject) {
//...
}