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);
You could create a database table that saves the task you want to execute when a user calls your endpoint. Then you can use a scheduler using the data saved in the database.
Task | Cron | Execs |
---|---|---|
0 0 */1 * * * | 10 |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.