package com.yeshi.fanli.util.cmq;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.yeshi.utils.CMQUtil;
|
|
import com.qcloud.cmq.Message;
|
import com.yeshi.fanli.log.LogHelper;
|
|
public class HongBaoRecieveCMQManager {
|
|
private static String secretId = "AKIDTlpgJhLjOozvd6QI2XnpfGbgV4NQJk25";
|
private static String secretKey = "xhCSUHo55oHUQ6XicFcmfIgspX0EEzWo";
|
private static HongBaoRecieveCMQManager userMoneyChangeCMQManager;
|
private static CMQUtil cmqUtil;
|
|
private final static String TOPIC_NAME = "topic_hongbao";
|
|
public static String QUEUE_INTEGRAL = TOPIC_NAME + "_" + "integral";
|
|
public static String SUBSCRIBE_INTEGRAL = "integral";
|
|
static {
|
cmqUtil = CMQUtil.getInstance(secretId, secretKey);
|
// 创建主题,添加订阅
|
cmqUtil.createTopic(TOPIC_NAME);
|
// 用户券订阅
|
String[] subscripts = new String[] { SUBSCRIBE_INTEGRAL };
|
String[] queues = new String[] { QUEUE_INTEGRAL };
|
|
for (int i = 0; i < subscripts.length; i++) {
|
String queueName = queues[i];
|
try {
|
cmqUtil.createQueue(queueName);
|
} catch (Exception e) {
|
}
|
try {
|
cmqUtil.subscribeTopic(TOPIC_NAME, subscripts[i], queueName);
|
} catch (Exception e) {
|
|
}
|
}
|
}
|
|
public static HongBaoRecieveCMQManager getInstance() {
|
if (userMoneyChangeCMQManager == null)
|
userMoneyChangeCMQManager = new HongBaoRecieveCMQManager();
|
return userMoneyChangeCMQManager;
|
}
|
|
/**
|
* 发布
|
*
|
* @param hongBaoId
|
*/
|
public void addHongBaoRecieveMsg(Long hongBaoId) {
|
if (hongBaoId == null)
|
return;
|
cmqUtil.publishTopicMessage(TOPIC_NAME, hongBaoId + "");
|
LogHelper.test("红包消息投递成功");
|
}
|
|
/**
|
* 消费队列消息
|
*
|
* @param queueName
|
* @param count
|
* @return
|
*/
|
public Map<String, Long> consumeQueueMsg(String queueName, int count) {
|
List<Message> list = cmqUtil.recieveMsg(count, queueName);
|
Map<String, Long> map = new HashMap<>();
|
|
if (list != null)
|
for (Message msg : list) {
|
String result = msg.msgBody;
|
map.put(msg.receiptHandle, Long.parseLong(result));
|
}
|
return map;
|
}
|
|
public void deleteQueueMsg(String queueName, String receiptHandle) {
|
cmqUtil.deleteMsg(queueName, receiptHandle);
|
}
|
|
}
|