定时自动导入
This commit is contained in:
parent
78cabcb107
commit
21a1179765
@ -1,40 +1,66 @@
|
|||||||
package com.doc.parser.domain.schedule;
|
package com.doc.parser.domain.schedule;
|
||||||
|
|
||||||
import com.doc.parser.domain.iface.DocumentImporter;
|
import com.doc.parser.domain.iface.DocumentImporter;
|
||||||
|
import com.doc.parser.infrastructure.config.DynamicConfig;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.Trigger;
|
||||||
|
import org.springframework.scheduling.TriggerContext;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
||||||
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
||||||
|
import org.springframework.scheduling.support.CronTrigger;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luke.ye
|
* 动态定时任务调度器(读取 cron 表达式来自 Nacos)
|
||||||
* @date 2025/5/21 14:09
|
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class ImportScheduler {
|
@EnableScheduling
|
||||||
|
public class ImportScheduler implements SchedulingConfigurer {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ImportScheduler.class);
|
private static final Logger logger = LoggerFactory.getLogger(ImportScheduler.class);
|
||||||
|
|
||||||
@Value("${import.schedule.enabled:true}")
|
@Value("${import.schedule.enabled:true}")
|
||||||
private boolean scheduleEnabled;
|
private boolean scheduleEnabled;
|
||||||
|
|
||||||
private final List<DocumentImporter> importers;
|
@Resource
|
||||||
|
private List<DocumentImporter> importers;
|
||||||
|
|
||||||
public ImportScheduler(List<DocumentImporter> importers) {
|
@Resource
|
||||||
this.importers = importers;
|
private DynamicConfig dynamicConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
||||||
|
taskRegistrar.addTriggerTask(
|
||||||
|
this::doImportTask,
|
||||||
|
new Trigger() {
|
||||||
|
@Override
|
||||||
|
public Date nextExecutionTime(TriggerContext triggerContext) {
|
||||||
|
String cron = dynamicConfig.getImportScheduleCron();
|
||||||
|
try {
|
||||||
|
return new CronTrigger(cron).nextExecutionTime(triggerContext);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("动态 cron 表达式解析失败: {},使用默认值 0 0 * * * *", cron, e);
|
||||||
|
return new CronTrigger("0 0 * * * *").nextExecutionTime(triggerContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(cron = "${import.schedule.cron:0 0 * * * *}")
|
private void doImportTask() {
|
||||||
public void scheduledImport() {
|
|
||||||
if (!scheduleEnabled) {
|
if (!scheduleEnabled) {
|
||||||
logger.info("导入定时任务已禁用");
|
logger.info("导入定时任务已禁用");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("开始执行定时导入任务");
|
logger.info("开始执行动态定时导入任务");
|
||||||
for (DocumentImporter importer : importers) {
|
for (DocumentImporter importer : importers) {
|
||||||
try {
|
try {
|
||||||
logger.info("执行导入器: {}", importer.getType());
|
logger.info("执行导入器: {}", importer.getType());
|
||||||
@ -47,3 +73,4 @@ public class ImportScheduler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,14 @@ public class DynamicConfig {
|
|||||||
@Value("${micro.saas.doc.parser.recordMsgBody:Y}")
|
@Value("${micro.saas.doc.parser.recordMsgBody:Y}")
|
||||||
private String recordMsgBody;
|
private String recordMsgBody;
|
||||||
|
|
||||||
|
@Value("${import.schedule.cron:0 0 * * * *}")
|
||||||
|
private String importScheduleCron;
|
||||||
|
|
||||||
public String getRecordMsgBody() {
|
public String getRecordMsgBody() {
|
||||||
return recordMsgBody;
|
return recordMsgBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getImportScheduleCron() {
|
||||||
|
return importScheduleCron;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user