SendMessageTask.java
2.21 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
package cn.fw.hestia.server.task;
import cn.fw.hestia.domain.db.MessageHistory;
import cn.fw.hestia.domain.enums.MessageStateEnum;
import cn.fw.hestia.service.buz.MessageCenterBizService;
import cn.fw.hestia.service.data.MessageHistoryService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import static cn.fw.hestia.common.constant.MessageConstant.MAX_FREQUENCY;
/**
* @author : kurisu
* @className : FollowNoticeTask
* @description : 服务号消息定时任务
* @date: 2020-08-25 16:57
*/
@Component
@ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
public class SendMessageTask {
private final MessageCenterBizService messageCenterBizService;
private final MessageHistoryService messageHistoryService;
@Autowired
public SendMessageTask(final MessageCenterBizService messageCenterBizService,
final MessageHistoryService messageHistoryService) {
this.messageCenterBizService = messageCenterBizService;
this.messageHistoryService = messageHistoryService;
}
/**
* 发送模板消息
*/
@Scheduled(initialDelay = 1000 * 10, fixedRate = 1000 * 10)
@Transactional(rollbackFor = Exception.class)
public void sendNotice() {
List<MessageHistory> list = messageHistoryService.list(Wrappers.<MessageHistory>lambdaQuery()
.eq(MessageHistory::getState, MessageStateEnum.MADA)
.eq(MessageHistory::getYn, Boolean.TRUE)
.lt(MessageHistory::getFrequency, MAX_FREQUENCY)
.lt(MessageHistory::getSendTime, new Date())
.last("limit 1000")
);
if (CollectionUtils.isEmpty(list)) {
return;
}
for (MessageHistory history : list) {
messageCenterBizService.sendMessage(history);
}
}
}