I asked this question before but unfortunately did not receive an answer.
I am writing a Spring Boot application. We send out over 1000 emails per day to our employees.
Since there is a limit on the number of emails I can send per hour, I want to send 99 emails per hour to the email addresses on my list.
I have the code that you see on your screens, how could I rewrite it with TASK SCHEDULER?
The mailing starts AFTER the send button is pressed on the front (I have an endpoint with a POST method for this) mailing so I don't use a normal SCHEDULER with cron that runs at a specific time regardless of whether my controller method was called from the front.
Runnable task = () -> {
int counter = 0;
long startTime = System.currentTimeMillis();
for (String emailAddress : usersEmailAddresses) {
counter++;
bulkEmailService.send(BulkEmailMessage.builder()
.from(bulkEmailMessage.getFrom())
.subject(bulkEmailMessage.getSubject())
.content(bulkEmailMessage.getContent())
.toAddresses(emailAddress)
.build());
if (counter % 99 == 0) {
long endTime = System.currentTimeMillis() - startTime;
try {
Thread.sleep(6000 * 60 * 60 - endTime);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
startTime = System.currentTimeMillis();
}
}
};
taskExecutor.execute(task);