admin
2021-07-22 5f9704c02fc61da33ed4d3db0d1172976e461089
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.ks.push.manager;
 
import com.google.gson.Gson;
import com.ks.push.dto.BPushDeviceDataSet;
import com.ks.push.pojo.DO.PushPlatform;
import com.qcloud.cmq.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.yeshi.utils.CMQUtil;
 
import java.util.ArrayList;
import java.util.List;
 
public class CMQManager {
    static Logger logger = LoggerFactory.getLogger(CMQManager.class);
 
    private static String secretId = "AKIDTlpgJhLjOozvd6QI2XnpfGbgV4NQJk25";
    private static String secretKey = "xhCSUHo55oHUQ6XicFcmfIgspX0EEzWo";
 
    private static CMQManager cmqManager;
    private static CMQUtil cmqUtil;
    /**
     * 小米推送队列
     */
    public static String PUSH_XM = "bpush-xm";
    /**
     * 华为推送队列
     */
    public static String PUSH_HUAWEI = "bpush-huawei";
    /**
     * oppo推送队列
     */
    public static String PUSH_OPPO = "bpush-oppo";
    /**
     * vivo推送队列
     */
    public static String PUSH_VIVO = "bpush-vivo";
    /**
     * 魅族推送队列
     */
    public static String PUSH_MZ = "bpush-mz";
 
    static {
        cmqUtil = CMQUtil.getInstance(secretId, secretKey);
        // 最大消息为1M
        cmqUtil.createQueue(PUSH_XM);
        cmqUtil.createQueue(PUSH_HUAWEI);
        cmqUtil.createQueue(PUSH_OPPO);
        cmqUtil.createQueue(PUSH_VIVO);
        cmqUtil.createQueue(PUSH_MZ);
        logger.info("创建队列完毕");
    }
 
 
    public static CMQManager getInstance() {
        if (cmqManager == null) {
            cmqManager = new CMQManager();
        }
        return cmqManager;
    }
 
    private String getQueueName(PushPlatform platform) {
        String queueName = null;
        if (platform == PushPlatform.xm) {
            queueName = PUSH_XM;
        } else if (platform == PushPlatform.hw) {
            queueName = PUSH_HUAWEI;
        } else if (platform == PushPlatform.oppo) {
            queueName = PUSH_OPPO;
        } else if (platform == PushPlatform.vivo) {
            queueName = PUSH_VIVO;
        } else if (platform == PushPlatform.mz) {
            queueName = PUSH_MZ;
        }
        return queueName;
    }
 
    /**
     * 添加到推送队列
     *
     * @param platform
     * @param dataSet
     */
    public void addToPushQueue(PushPlatform platform, BPushDeviceDataSet dataSet) {
        String queueName = getQueueName(platform);
        if (queueName == null) {
            return;
        }
        cmqUtil.sendMsg(queueName, new Gson().toJson(dataSet));
    }
 
    /**
     * 消费队列
     *
     * @param platform
     * @param count
     * @return
     */
    public List<MQMsgConsumeResult> consumePushQueue(PushPlatform platform, int count) throws Exception {
        String queueName = getQueueName(platform);
        if (queueName == null) {
            return null;
        }
 
        List<Message> list = cmqUtil.recieveMsg(count, queueName);
        if (list != null) {
            List<MQMsgConsumeResult> resultList = new ArrayList<>();
            for (Message msg : list) {
                String result = msg.msgBody;
                logger.info("队列名称:{} 消息内容:{}", queueName, result);
                BPushDeviceDataSet dataSet = new Gson().fromJson(result, BPushDeviceDataSet.class);
                resultList.add(new MQMsgConsumeResult(dataSet, queueName, msg.receiptHandle));
            }
            return resultList;
        }
        return null;
    }
 
    /**
     * 删除消息
     *
     * @param queueName
     * @param receiptHandle
     */
    public void deleteMsg(String queueName, String receiptHandle) {
        cmqUtil.deleteMsg(queueName, receiptHandle);
    }
 
 
    public static class MQMsgConsumeResult {
        private String queueName;
        private Object data;
        private String receiptHandle;
 
        public MQMsgConsumeResult(Object data, String queueName, String receiptHandle) {
            this.data = data;
            this.queueName = queueName;
            this.receiptHandle = receiptHandle;
        }
 
        public Object getData() {
            return data;
        }
 
        public void setData(Object data) {
            this.data = data;
        }
 
        public String getReceiptHandle() {
            return receiptHandle;
        }
 
        public void setReceiptHandle(String receiptHandle) {
            this.receiptHandle = receiptHandle;
        }
 
        public String getQueueName() {
            return queueName;
        }
 
        public void setQueueName(String queueName) {
            this.queueName = queueName;
        }
    }
 
 
}