From 7c99dd557687b48717b9d53364c3c06a823a53e7 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Thu, 1 Dec 2022 16:32:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=E5=A2=9E=E5=8A=A0=E6=B8=85?= =?UTF-8?q?=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1010672 --user=李玉号 【项目设置】消息通知增加清理机制 https://www.tapd.cn/55049933/s/1309642 --- .../job/schedule/CleanNotificationJob.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 system-setting/backend/src/main/java/io/metersphere/job/schedule/CleanNotificationJob.java diff --git a/system-setting/backend/src/main/java/io/metersphere/job/schedule/CleanNotificationJob.java b/system-setting/backend/src/main/java/io/metersphere/job/schedule/CleanNotificationJob.java new file mode 100644 index 0000000000..0a03d88e38 --- /dev/null +++ b/system-setting/backend/src/main/java/io/metersphere/job/schedule/CleanNotificationJob.java @@ -0,0 +1,81 @@ +package io.metersphere.job.schedule; + + +import com.fit2cloud.quartz.anno.QuartzScheduled; +import io.metersphere.base.domain.Notification; +import io.metersphere.base.domain.NotificationExample; +import io.metersphere.base.mapper.NotificationMapper; +import io.metersphere.commons.utils.LogUtil; +import org.apache.commons.collections.CollectionUtils; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author lyh + */ +@Component +public class CleanNotificationJob { + + @Resource + private NotificationMapper notificationMapper; + + + @QuartzScheduled(cron = "0 3 0 * * ?") + public void cleanupNotification() { + LogUtil.info("clean up notification start."); + try { + LocalDate date = LocalDate.now().minusMonths(3); + long timestamp = date.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli(); + this.doCleanupNotification(timestamp); + } catch (Exception e) { + LogUtil.error("clean up notification error.", e); + } + LogUtil.info("clean up notification end."); + } + + private void doCleanupNotification(long timestamp) { + NotificationExample example = new NotificationExample(); + example.createCriteria().andCreateTimeLessThanOrEqualTo(timestamp); + List notifications = notificationMapper.selectByExample(example); + if (CollectionUtils.isEmpty(notifications)) { + return; + } + + List ids = notifications.stream() + .map(Notification::getId) + .collect(Collectors.toList()); + + int handleCount = 5000; + while (ids.size() > handleCount) { + List deleteIds = new ArrayList<>(handleCount); + List otherIds = new ArrayList<>(); + for (int index = 0; index < ids.size(); index++) { + if (index < handleCount) { + deleteIds.add(ids.get(index)); + } else { + otherIds.add(ids.get(index)); + } + } + this.deleteNotification(deleteIds); + ids = otherIds; + } + this.deleteNotification(ids); + } + + private void deleteNotification(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } + NotificationExample example = new NotificationExample(); + example.createCriteria().andIdIn(ids); + notificationMapper.deleteByExample(example); + } + + +}