admin
2020-12-25 25680e135b5bdc15658622cbfde74bab73cfee77
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.ks.lucky.service.impl;
 
import com.ks.lucky.exception.LuckyMQException;
import com.ks.lucky.mapper.LuckyMQMapper;
import com.ks.lucky.pojo.DO.LuckyMQ;
import com.ks.lucky.query.MQQuery;
import com.ks.lucky.service.LuckyMQService;
import com.ks.lucky.util.mq.CMQManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
@Service
public class LuckyMQServiceImpl implements LuckyMQService {
    @Resource
    private LuckyMQMapper luckyMQMapper;
 
    @Override
    public void addMQ(LuckyMQ mq) throws LuckyMQException {
        LuckyMQ old = luckyMQMapper.selectByTaskId(mq.getTaskId());
        if (old != null) {
            throw new LuckyMQException(LuckyMQException.CODE_EXIST, "任务ID已经存在");
        }
 
        if (mq.getCreateTime() == null) {
            mq.setCreateTime(new Date());
        }
 
        luckyMQMapper.insertSelective(mq);
    }
 
    @Override
    public List<LuckyMQ> listUnSendMsg(Date now, int page, int pageCount) {
        MQQuery query = new MQQuery();
        query.maxPreSendTime = new Date(now.getTime() + 1L);
        query.minPreSendTime = new Date(now.getTime() - 1000 * 60 * 60L * 24);
        query.state = LuckyMQ.STATE_NOT_SEND;
        query.start = (page - 1) * pageCount;
        query.count = pageCount;
        return luckyMQMapper.list(query);
    }
 
    @Override
    public long countUnSendMsg(Date now) {
        MQQuery query = new MQQuery();
        query.maxPreSendTime = new Date(now.getTime() + 1L);
        query.minPreSendTime = new Date(now.getTime() - 1000 * 60 * 60L * 24);
        query.state = LuckyMQ.STATE_NOT_SEND;
        return luckyMQMapper.count(query);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeUnSendMsg(String taskId) throws LuckyMQException {
        LuckyMQ old = luckyMQMapper.selectByTaskIdForUpdate(taskId);
        if (old == null) {
            return;
        }
        if (old.getState() != LuckyMQ.STATE_NOT_SEND) {
            throw new LuckyMQException(1, "消息不是待发送状态");
        }
        luckyMQMapper.deleteByPrimaryKey(old.getId());
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void sendMsg(String taskId) throws LuckyMQException {
        LuckyMQ mq = luckyMQMapper.selectByTaskIdForUpdate(taskId);
        if (mq == null) {
            throw new LuckyMQException(LuckyMQException.CODE_EXIST, "任务ID已经存在");
        }
 
        CMQManager.getInstance().addMsg(mq.getQueueName(), mq.getQueueContent());
        LuckyMQ update = new LuckyMQ();
        update.setId(mq.getId());
        update.setState(LuckyMQ.STATE_SENDED);
        update.setActualSendTime(new Date());
        update.setUpdateTime(new Date());
        luckyMQMapper.updateByPrimaryKeySelective(update);
    }
 
 
 
 
 
}