From 85ca166b03d4c5a7344101e796dc646d257e2e0f Mon Sep 17 00:00:00 2001 From: wxc <191104855@qq.com> Date: Mon, 22 Sep 2025 09:48:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E4=B8=8D=E6=89=A7=E8=A1=8C=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biutag/outer/config/SchedulerConfig.java | 35 +++++++++++++++++++ .../main/java/com/biutag/outer/job/Job.java | 24 +++++++------ 2 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 mailbox-outer/src/main/java/com/biutag/outer/config/SchedulerConfig.java diff --git a/mailbox-outer/src/main/java/com/biutag/outer/config/SchedulerConfig.java b/mailbox-outer/src/main/java/com/biutag/outer/config/SchedulerConfig.java new file mode 100644 index 0000000..ae85f40 --- /dev/null +++ b/mailbox-outer/src/main/java/com/biutag/outer/config/SchedulerConfig.java @@ -0,0 +1,35 @@ +package com.biutag.outer.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; + +import java.util.concurrent.ThreadPoolExecutor; + +/** + * @author wxc + * @date 2025/9/22 + */ +@Configuration +@EnableScheduling +public class SchedulerConfig { + + @Bean + public ThreadPoolTaskScheduler taskScheduler() { + ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); + // 设置线程池大小 + scheduler.setPoolSize(10); + // 设置线程名前缀 + scheduler.setThreadNamePrefix("scheduled-task-"); + // 设置线程池关闭时等待所有任务完成 + scheduler.setWaitForTasksToCompleteOnShutdown(true); + // 设置等待终止时间(单位:秒) + scheduler.setAwaitTerminationSeconds(60); + // 设置拒绝策略 + scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + // 初始化线程池 + scheduler.initialize(); + return scheduler; + } +} diff --git a/mailbox-outer/src/main/java/com/biutag/outer/job/Job.java b/mailbox-outer/src/main/java/com/biutag/outer/job/Job.java index b161355..6c420ba 100644 --- a/mailbox-outer/src/main/java/com/biutag/outer/job/Job.java +++ b/mailbox-outer/src/main/java/com/biutag/outer/job/Job.java @@ -72,7 +72,9 @@ public class Job { .maximumSize(1000) .build(); - // 60s + /** + * 每60s执行一次(1分钟) + */ @Scheduled(fixedRate = 60000) public void pushMailData() { log.info("推送信件数据-----------------------------"); @@ -133,9 +135,9 @@ public class Job { } /** - * 推送信件评价(网络) 60 s + * 推送信件评价(网络) 10分钟 */ - @Scheduled(fixedRate = 60000) + // @Scheduled(cron = "0 */10 * * * ?") public void pushMailEvaluate() { log.info("推送信件评价(网络)-----------------------------"); List mails = mailMapper.listByMailEvaluateEtl(); @@ -166,9 +168,9 @@ public class Job { } /** - * 推送信件评价(短信) 60 s + * 推送信件评价(短信) 10分钟 */ - @Scheduled(fixedRate = 60000) + @Scheduled(cron = "0 */10 * * * ?") public void pushMailSmsEvaluate() { log.info("推送信件评价(短信)-----------------------------"); List mails = mailMapper.listByMailSmsEvaluateEtl(); @@ -202,7 +204,7 @@ public class Job { } /** - * 发送短信 + * 发送短信 60s */ @Scheduled(fixedRate = 60000) public void sendSms() { @@ -245,9 +247,9 @@ public class Job { } /** - * 更新信件状态 60s + * 更新信件状态 10分钟 */ - @Scheduled(fixedRate = 60000) + @Scheduled(cron = "0 */10 * * * ?") public void updateMailState() { List mails = mailMapper.listByMailState(); log.info("未获取信件状态的数量为:{}", mails.size()); @@ -280,7 +282,7 @@ public class Job { /** * 黑名单 10分钟 更新一次 */ - @Scheduled(fixedRate = 600000) + @Scheduled(cron = "0 */10 * * * ?") public void balcklist() { long timestamp = new Date().getTime(); HttpResponse httpResponse = HttpUtil.createRequest(Method.GET, mailboxUrl + "blacklist") @@ -328,9 +330,9 @@ public class Job { } /** - * 推送12345信箱服务 + * 推送12345信箱服务 60s */ - @Scheduled(cron = "0 0 1 * * *") + @Scheduled(fixedRate = 60000) public void push12345() { log.info("推送12345信箱服务-----------------------------"); try {