| | |
| | | package com.yeshi.fanli.util.mq.cmq; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import com.yeshi.fanli.util.Constant; |
| | | import org.yeshi.utils.CMQUtil; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.qcloud.cmq.Message; |
| | | import com.yeshi.fanli.dto.money.UserMoneyChangeDTO; |
| | | import com.yeshi.fanli.log.LogHelper; |
| | | import com.yeshi.fanli.util.Constant; |
| | | import com.yeshi.fanli.util.mq.rabbit.RabbitmqSenderUtil; |
| | | import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | public class UserMoneyChangeCMQManager { |
| | | |
| | | private static String secretId = "AKIDTlpgJhLjOozvd6QI2XnpfGbgV4NQJk25"; |
| | | private static String secretKey = "xhCSUHo55oHUQ6XicFcmfIgspX0EEzWo"; |
| | | private static UserMoneyChangeCMQManager userMoneyChangeCMQManager; |
| | | private static CMQUtil cmqUtil; |
| | | private static TDMQUtil tdmqUtil; |
| | | @Resource |
| | | private RabbitTemplate rabbitTemplate; |
| | | |
| | | |
| | | private final static String TOPIC_NAME = "topic_user_money"; |
| | | |
| | | public static String QUEUE_DEBT = TOPIC_NAME + "_" + "debt"; |
| | | |
| | | public static String SUBSCRIBE_DEBT = "debt"; |
| | | |
| | | static { |
| | | cmqUtil = CMQUtil.getInstance(secretId, secretKey); |
| | | TDMQUtil.getInstance().init(secretId, secretKey, Constant.TDMQ_PUBLIC); |
| | | tdmqUtil = TDMQUtil.getInstance(); |
| | | // 创建主题,添加订阅 |
| | | tdmqUtil.createTopic(TOPIC_NAME); |
| | | // 用户券订阅 |
| | | String[] subscripts = new String[]{SUBSCRIBE_DEBT}; |
| | | String[] queues = new String[]{QUEUE_DEBT}; |
| | | |
| | | for (int i = 0; i < subscripts.length; i++) { |
| | | String queueName = queues[i]; |
| | | try { |
| | | tdmqUtil.createQueue(queueName); |
| | | } catch (Exception e) { |
| | | } |
| | | try { |
| | | tdmqUtil.subscribeTopic(TOPIC_NAME, subscripts[i], queueName); |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static UserMoneyChangeCMQManager getInstance() { |
| | | if (userMoneyChangeCMQManager == null) |
| | | userMoneyChangeCMQManager = new UserMoneyChangeCMQManager(); |
| | | return userMoneyChangeCMQManager; |
| | | } |
| | | |
| | | /** |
| | | * 发布 |
| | |
| | | public void addUserMoneyChangeMsg(UserMoneyChangeDTO dto) { |
| | | if (dto == null) |
| | | return; |
| | | tdmqUtil.publishTopicMessage(TOPIC_NAME, new Gson().toJson(dto)); |
| | | RabbitmqSenderUtil.sendExchangeMsg(rabbitTemplate, TOPIC_NAME,"*", new Gson().toJson(dto) ); |
| | | LogHelper.test("资金消息投递成功"); |
| | | } |
| | | |
| | | /** |
| | | * 消费队列消息 |
| | | * |
| | | * @param queueName |
| | | * @param count |
| | | * @return |
| | | */ |
| | | public Map<String, UserMoneyChangeDTO> consumeQueueMsg(String queueName, int count) { |
| | | List<Message> list = Constant.TDMQ_CONSUMER ? tdmqUtil.recieveMsg(count, queueName) : cmqUtil.recieveMsg(count, queueName); |
| | | Map<String, UserMoneyChangeDTO> map = new HashMap<>(); |
| | | |
| | | if (list != null) |
| | | for (Message msg : list) { |
| | | String result = msg.msgBody; |
| | | UserMoneyChangeDTO dto = new Gson().fromJson(result, UserMoneyChangeDTO.class); |
| | | map.put(msg.receiptHandle, dto); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | public void deleteQueueMsg(String queueName, String receiptHandle) { |
| | | if (Constant.TDMQ_CONSUMER) { |
| | | tdmqUtil.deleteMsg(queueName, receiptHandle); |
| | | } else { |
| | | cmqUtil.deleteMsg(queueName, receiptHandle); |
| | | } |
| | | } |
| | | |
| | | } |