package com.ks.push.manager;
|
|
import com.google.gson.Gson;
|
import com.ks.push.dto.BPushDeviceDataSet;
|
import com.ks.push.dto.mq.InvalidDeviceTokenInfo;
|
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";
|
|
/**
|
* 无效设备队列
|
*/
|
public static String PUSH_TOKEN_INVALID = "bpush-token-invalid";
|
|
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);
|
cmqUtil.createQueue(PUSH_TOKEN_INVALID);
|
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);
|
}
|
|
|
/**
|
* 发送无效设备消息
|
*
|
* @param info
|
*/
|
public void addInvalidDevieToken(InvalidDeviceTokenInfo info) {
|
if (info == null) {
|
return;
|
}
|
cmqUtil.sendMsg(PUSH_TOKEN_INVALID, new Gson().toJson(info));
|
}
|
|
/**
|
* 消费无效设备消息
|
*
|
* @param count
|
*/
|
public List<MQMsgConsumeResult> consumeInvalidDeviceTokenQueue(int count) throws Exception {
|
List<Message> list = cmqUtil.recieveMsg(count, PUSH_TOKEN_INVALID);
|
if (list != null) {
|
List<MQMsgConsumeResult> resultList = new ArrayList<>();
|
for (Message msg : list) {
|
String result = msg.msgBody;
|
InvalidDeviceTokenInfo tokenInfo = new Gson().fromJson(result, InvalidDeviceTokenInfo.class);
|
resultList.add(new MQMsgConsumeResult(tokenInfo, PUSH_TOKEN_INVALID, msg.receiptHandle));
|
}
|
return resultList;
|
}
|
return null;
|
}
|
|
|
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;
|
}
|
}
|
|
|
}
|