消息延时推送
夜间消息屏蔽次日发送
# 技术选型
Redis+xxl-job
将晚上收到的消息扔进Redis list,开启定时任务轮询该 list是否有数据;
# 写表
public class ShieldServiceImpl implements ShieldService{
//......
@Override
public void shield(TaskInfo taskInfo) {
if (ShieldType.NIGHT_NO_SHIELD.getCode().equals(taskInfo.getShieldType())) {
return;
}
if (isNight()) {
if (ShieldType.NIGHT_SHIELD.getCode().equals(taskInfo.getShieldType())) {
logUtils.print(AnchorInfo.builder().state(AnchorState.NIGHT_SHIELD.getCode())
.businessId(taskInfo.getBusinessId()).ids(taskInfo.getReceiver()).build());
}
if (ShieldType.NIGHT_SHIELD_BUT_NEXT_DAY_SEND.getCode().equals(taskInfo.getShieldType())) {
redisUtils.lPush(NIGHT_SHIELD_BUT_NEXT_DAY_SEND_KEY, JSON.toJSONString(taskInfo,
SerializerFeature.WriteClassName),
(DateUtil.offsetDay(new Date(), 1).getTime() / 1000) - DateUtil.currentSeconds());
logUtils.print(AnchorInfo.builder().state(AnchorState.NIGHT_SHIELD_NEXT_SEND.getCode()).businessId(taskInfo.getBusinessId()).ids(taskInfo.getReceiver()).build());
}
taskInfo.setReceiver(new HashSet<>());
}
}
//......
}
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
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
# 扫表
public class NightShieldLazyPendingHandler {
//......
/**
* 处理 夜间屏蔽(次日早上9点发送的任务)
*/
@XxlJob("nightShieldLazyJob")
public void execute() {
log.info("NightShieldLazyPendingHandler#execute!");
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
while (redisUtils.lLen(NIGHT_SHIELD_BUT_NEXT_DAY_SEND_KEY) > 0) {
String taskInfo = redisUtils.lPop(NIGHT_SHIELD_BUT_NEXT_DAY_SEND_KEY);
if (StrUtil.isNotBlank(taskInfo)) {
try {
kafkaTemplate.send(topicName, JSON.toJSONString(Arrays.asList(JSON.parseObject(taskInfo, TaskInfo.class))
, new SerializerFeature[]{SerializerFeature.WriteClassName}));
} catch (Exception e) {
log.error("nightShieldLazyJob send kafka fail! e:{},params:{}", Throwables.getStackTraceAsString(e), taskInfo);
}
}
}
});
}
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25