[英]Task Scheduler in spring boot
I asked this question before but unfortunately did not receive an answer.我之前问过这个问题,但不幸的是没有得到答案。
I am writing a Spring Boot application.我正在编写 Spring 引导应用程序。 We send out over 1000 emails per day to our employees.
我们每天向员工发送超过 1000 封电子邮件。
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.由于每小时可以发送的电子邮件数量有限制,我想每小时向我列表中的 email 地址发送 99 封电子邮件。
I have the code that you see on your screens, how could I rewrite it with TASK SCHEDULER?我有你在屏幕上看到的代码,我怎么能用 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.邮件在前面按下发送按钮后开始(我有一个带有 POST 方法的端点)邮件所以我不使用在特定时间运行的正常调度程序,无论我的 controller 方法是否被调用从前面。
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![]() |
---|---|---|
Email ![]() |
0 0 */1 * * * ![]() |
10 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.