From a0b9b58291ca46ccf4fb23ab773b8f39281bf6e2 Mon Sep 17 00:00:00 2001 From: wuweihua <88888888> Date: Thu, 19 Nov 2026 10:36:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=93=E5=8D=95=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=88=86=E5=8C=BA=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bms/price/task/EsBizLogCleanupTask.java | 159 ++++++++++++++++++ bms-back/sql/price/es_biz_log_cleanup_job.sql | 24 +++ 2 files changed, 183 insertions(+) create mode 100644 bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java create mode 100644 bms-back/sql/price/es_biz_log_cleanup_job.sql diff --git a/bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java b/bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java new file mode 100644 index 00000000..0f76a58d --- /dev/null +++ b/bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java @@ -0,0 +1,159 @@ +package com.bms.price.task; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.bms.common.utils.StringUtils; +import com.bms.price.search.ElasticsearchProperties; + +/** + * 清理 ES 业务日志(bms_biz_log)中超期文档。 + * + *
纳入「系统监控 - 定时任务」: + * 调用目标 {@code esBizLogCleanupTask.cleanupOlderThanMonths(3)}, + * 表示删除 3 个月前的数据;也可 {@code cleanup()} 默认 3 个月。
+ */ +@Component("esBizLogCleanupTask") +public class EsBizLogCleanupTask +{ + private static final Logger log = LoggerFactory.getLogger(EsBizLogCleanupTask.class); + + private static final int DEFAULT_MONTHS = 3; + private static final int MIN_MONTHS = 1; + private static final int MAX_MONTHS = 24; + + @Autowired + private ElasticsearchProperties properties; + + /** 默认清理 3 个月前数据。 */ + public void cleanup() + { + cleanupOlderThanMonths(Integer.valueOf(DEFAULT_MONTHS)); + } + + /** + * 删除早于(今天 - months 个月)的 ES 日志。 + * Quartz:esBizLogCleanupTask.cleanupOlderThanMonths(3) + */ + public void cleanupOlderThanMonths(Integer months) + { + if (properties == null || !properties.isEnabled()) + { + log.info("ES 未启用,跳过日志清理"); + return; + } + int keepMonths = months == null ? DEFAULT_MONTHS : months.intValue(); + if (keepMonths < MIN_MONTHS) + { + keepMonths = MIN_MONTHS; + } + if (keepMonths > MAX_MONTHS) + { + keepMonths = MAX_MONTHS; + } + String cutoff = cutoffTime(keepMonths); + String index = StringUtils.isEmpty(properties.getIndex()) ? "bms_biz_log" : properties.getIndex().trim(); + log.info("开始清理 ES 日志 index={} cutoff(<)={} keepMonths={}", index, cutoff, keepMonths); + + RestTemplate rest = buildRestTemplate(); + long deleted = deleteOlderThan(rest, index, cutoff); + log.info("ES 日志清理完成 index={} deleted={}", index, deleted); + } + + private long deleteOlderThan(RestTemplate rest, String index, String cutoff) + { + JSONObject body = new JSONObject(); + body.put("query", buildOlderQuery(cutoff)); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity