This commit is contained in:
雷欧(林平凡)
2025-06-03 11:33:31 +08:00
parent 99d46acaa2
commit ebe9abd880
3 changed files with 52 additions and 37 deletions

View File

@@ -450,41 +450,42 @@ public class JDScheduleJob {
return client.execute(request); return client.execute(request);
} }
@Scheduled(cron = "0 0 8-20 * * ?") // 每天从 8:00 到 20:00每小时执行一次 @Scheduled(cron = "0 0 8-20 * * ?") // 每天从 8:00 到 20:00每小时执行一次
public void fetchPL() { public void fetchPL() {
// 设置每天最多执行 3 次 logger.info("开始执行fetchPL任务");
String cacheKey = "fetchPL:executedHours"; // 设置每天最多执行 3 次
Set<String> executedHours = getExecutedHoursFromRedis(); // 从 Redis 获取已执行的小时数 Set<String> executedHours = getExecutedHoursFromRedis(); // 从 Redis 获取已执行的小时数
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
String currentHour = String.valueOf(now.getHour()); String currentHour = String.valueOf(now.getHour());
// 如果今天已经执行了3次则跳过 // 如果今天已经执行了3次则跳过
if (executedHours.size() >= 3) { if (executedHours.size() >= 3) {
return; logger.info("今天已经执行了3次跳过本次任务");
return;
}
// 随机决定是否执行本次任务(例如 50% 概率)
if (new Random().nextBoolean()) {
logger.info("执行fetchPL任务");
// 执行任务逻辑
executeFetchPL();
// 记录该小时已执行
executedHours.add(currentHour);
saveExecutedHoursToRedis(executedHours); // 存入 Redis
}
} }
// 随机决定是否执行本次任务(例如 50% 概率)
if (new Random().nextBoolean()) {
// 执行任务逻辑
executeFetchPL();
// 记录该小时已执行
executedHours.add(currentHour);
saveExecutedHoursToRedis(executedHours); // 存入 Redis
}
}
private void executeFetchPL() { private void executeFetchPL() {
HashMap<String, String> productTypeMap = jdUtil.getProductTypeMap(); HashMap<String, String> productTypeMap = jdUtil.getProductTypeMap();
int allCommentCount = 0; int usedCommentCount;
int usedCommentCount = 0; int canUseComentCount;
int canUseComentCount = 0; int addCommentCount;
int addCommentCount = 0;
for (Map.Entry<String, String> entry : productTypeMap.entrySet()) { for (Map.Entry<String, String> entry : productTypeMap.entrySet()) {
// 随机睡眠1-5分钟 // 随机睡眠1-5分钟
int sleepTime = new Random().nextInt(300) + 60; int sleepTime = new Random().nextInt(3000) + 60;
try { try {
Thread.sleep(sleepTime * 1000); Thread.sleep(sleepTime * 1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@@ -497,8 +498,8 @@ public void fetchPL() {
canUseComentCount = availableComments.size(); canUseComentCount = availableComments.size();
usedCommentCount = usedComments.size(); usedCommentCount = usedComments.size();
if (canUseComentCount > 5){ if (canUseComentCount > 5) {
logger.info("商品{} 评论可用数量大于5{}", product_id, canUseComentCount); logger.info("商品{} 评论可用数量大于5{}", product_id, canUseComentCount);
return; return;
} }

View File

@@ -1,10 +0,0 @@
# ????????????
log4j.rootLogger=DEBUG, file
# ?????? Appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/app.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=30
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c - %m%n

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/app.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>