TopicCleanupJob.java
2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.viontech.keliu.cron;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.viontech.keliu.service.KafkaTopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* kafka topic 清理任务
* @author: msl
* Date: 2025/5/30
*/
@Slf4j
@Component
public class TopicCleanupJob {
@Value("${vion.topicCleanup.preDay:7}")
private Integer preDay;
@Resource
private KafkaTopicService kafkaTopicService;
@Autowired
private RedisTemplate redisTemplate;
// TopicCleanupJob的分布式锁
private static final String TOPICCLEANUPJOB_LOCK_KEY = "lock:TopicCleanupJob:";
@Scheduled(cron = "${vion.topicCleanup.cron:-}")
public void doTopicCleanup() {
String lockKey = TOPICCLEANUPJOB_LOCK_KEY + DateUtil.format(new Date(), "yyyyMMdd");
Boolean locked = redisTemplate.opsForValue().setIfAbsent(lockKey, "1", 2, TimeUnit.DAYS);
log.info("doTopicCleanup.lockKey {}={}", lockKey, locked);
if (locked != null && !locked) {
return;
}
DateTime preDate = DateUtil.offsetDay(new Date(), preDay * (-1));
DateTime countDate = DateUtil.beginOfDay(preDate);
LocalDate maxCountDate = countDate.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
log.info("doTopicCleanup execute start, preDay: {}, countDate: {}", preDay, maxCountDate);
try {
kafkaTopicService.deleteEmptyTopicsByTopicDate("Mall_FaceCapture_.*", maxCountDate);
} catch (Exception e) {
log.error("doTopicCleanup[Mall_FaceCapture_].Exception={}", e.getMessage(), e);
}
log.info("doTopicCleanup execute[Mall_FaceCapture_] end, preDay: {}, countDate: {}", preDay, maxCountDate);
try {
kafkaTopicService.deleteEmptyTopicsByTopicDate("Mall_PersonLabel_.*", maxCountDate);
} catch (Exception e) {
log.error("doTopicCleanup[Mall_PersonLabel_].Exception={}", e.getMessage(), e);
}
log.info("doTopicCleanup execute[Mall_PersonLabel_] end, preDay: {}, countDate: {}", preDay, maxCountDate);
log.info("doTopicCleanup execute end, preDay: {}, countDate: {}", preDay, maxCountDate);
}
}