1、springboot 里配置定时任务
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableAsync
@Configuration
@EnableScheduling
public class DataChatgptApplication {
public static void main(String[] args) {
SpringApplication.run(DataChatgptApplication.class, args);
}
}
@Service
@Slf4j
public class AsyncTaskService {
@Scheduled(cron = "0 35 8 * * ?")
public void executeStoredProcedureEveryHalfHour() {
log.info("Executing stored procedure...");
try {
// 调用存储过程的方法
ZBESDataInUseUtils.updateESData();
} catch (Exception e) {
log.error("An error occurred while executing the stored procedure: ", e);
}
log.info("End of executing stored procedure...");
}
}
2、导入ES和修改别名
public static void updateESData() {
try {
// 创建模板
// int status = createIndexTemp(CONTENT_ERP_NLP_HELP);
// API接口的FAQ数据
log.info("Start, task!");
LocalDateTime now = LocalDateTime.now();
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
// 格式化当前时间
String formattedDateTime = now.format(formatter);
String indexName = CONTENT_ERP_NLP_HELP + "_" + formattedDateTime;
// 创建索引
createIndex(indexName);
System.out.println("Start, task! " + indexName);
// 写入数据
ZBTempDataUtils.TempDataToES(indexName);
ZBFileInUsetils.FileToES(indexName);
ZBAPPURLExcelUtils.APPDataToES(indexName);
ZBPCURLExcelUtils.PCDatatoES(indexName);
ZBWordInUsetils.WordToES(indexName);
ZBApiFAQInUseUtils.xiqueApiDataToES(indexName);
// 获取别名下的老索引
List<String> indexOldNameList = getIndexListByAlias(CONTENT_ERP_NLP_HELP_ALIAS_TEST);
// 构建新的索引
List<String> indexNameList = new ArrayList<>();
indexNameList.add(indexName);
// 修改别名
changeNewAliases(indexOldNameList, indexNameList, CONTENT_ERP_NLP_HELP_ALIAS_TEST);
// 删除昨天的索引
// 获取昨天的日期
LocalDate yesterday = LocalDate.now().minusDays(1);
// 格式化昨天的时间(这里我们只格式化日期部分,因为昨天的时间会是零点)
String formattedYesterDateTime = yesterday.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String yesterdayIndexName = CONTENT_ERP_NLP_HELP + "_" + formattedYesterDateTime;
// 输出昨天格式化的日期
System.out.println("Yesterday's formatted date index : " + yesterdayIndexName);
List<IndicesRecord> allIndexlist = getAllIndex();
for (IndicesRecord indicesRecord : allIndexlist) {
String indexTempName = indicesRecord.index();
if (indexTempName.contains(yesterdayIndexName)) {
System.out.println("Yesterday's formatted date indexName : " + indexTempName);
deleteIndex(indexTempName);
}
}
} catch (Exception e) {
// 记录异常信息,可以选择使用log4j、slf4j或其他日志框架
log.error("An error occurred while updating ES data: ", e);
// 可以在这里发送警报、记录错误、重试机制等
}
}