| | |
| | | package com.yeshi.fanli.util.mq.cmq.order; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import org.yeshi.utils.CMQUtil; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.qcloud.cmq.Message; |
| | | import com.yeshi.fanli.entity.bus.user.Order; |
| | | import com.yeshi.fanli.entity.order.CommonOrder; |
| | | import com.yeshi.fanli.util.mq.rabbit.RabbitmqSenderUtil; |
| | | import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | @Component |
| | | public class PlaceOrderCMQManager { |
| | | |
| | | private static String secretId = "AKIDTlpgJhLjOozvd6QI2XnpfGbgV4NQJk25"; |
| | | private static String secretKey = "xhCSUHo55oHUQ6XicFcmfIgspX0EEzWo"; |
| | | private static PlaceOrderCMQManager placeOrderCMQManager; |
| | | private static CMQUtil cmqUtil; |
| | | @Resource |
| | | private RabbitTemplate rabbitTemplate; |
| | | |
| | | private final static String TOPIC_NAME = "topic_place_order"; |
| | | private final static String TOPIC_NAME = "topic_place_order"; |
| | | |
| | | public static String QUEUE_AD = TOPIC_NAME + "_" + "ad"; |
| | | public static String QUEUE_INTEGRAL = TOPIC_NAME + "_" + "integral"; |
| | | public static String QUEUE_AD = TOPIC_NAME + "_" + "ad"; |
| | | public static String QUEUE_INTEGRAL = TOPIC_NAME + "_" + "integral"; |
| | | |
| | | public static String SUBSCRIBE_AD = "ad"; |
| | | public static String SUBSCRIBE_INTEGRAL = "integral"; |
| | | public static String SUBSCRIBE_AD = "ad"; |
| | | public static String SUBSCRIBE_INTEGRAL = "integral"; |
| | | |
| | | static { |
| | | cmqUtil = CMQUtil.getInstance(secretId, secretKey); |
| | | // 创建主题,添加订阅 |
| | | cmqUtil.createTopic(TOPIC_NAME); |
| | | // 用户券订阅 |
| | | String[] subscripts = new String[] { SUBSCRIBE_AD, SUBSCRIBE_INTEGRAL }; |
| | | String[] queues = new String[] { QUEUE_AD, 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 PlaceOrderCMQManager getInstance() { |
| | | if (placeOrderCMQManager == null) |
| | | placeOrderCMQManager = new PlaceOrderCMQManager(); |
| | | return placeOrderCMQManager; |
| | | } |
| | | |
| | | /** |
| | | * 下单 |
| | | * |
| | | * @param order(只需要订单号与sourceType) |
| | | */ |
| | | public void addPlaceOrderMsg(Order order) { |
| | | if (order == null) |
| | | return; |
| | | cmqUtil.publishTopicMessage(TOPIC_NAME, new Gson().toJson(order)); |
| | | } |
| | | |
| | | /** |
| | | * 消费队列消息 |
| | | * |
| | | * @param queueName |
| | | * @param count |
| | | * @return |
| | | */ |
| | | public Map<String, Order> consumeQueueMsg(String queueName, int count) { |
| | | List<Message> list = cmqUtil.recieveMsg(count, queueName); |
| | | Map<String, Order> map = new HashMap<>(); |
| | | |
| | | if (list != null) |
| | | for (Message msg : list) { |
| | | String result = msg.msgBody; |
| | | Order dto = new Gson().fromJson(result, Order.class); |
| | | map.put(msg.receiptHandle, dto); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | public void deleteQueueMsg(String queueName, String receiptHandle) { |
| | | cmqUtil.deleteMsg(queueName, receiptHandle); |
| | | } |
| | | /** |
| | | * 下单 |
| | | * |
| | | * @param order(只需要订单号与sourceType) |
| | | */ |
| | | public void addPlaceOrderMsg(Order order) { |
| | | if (order == null) |
| | | return; |
| | | RabbitmqSenderUtil.sendExchangeMsg(rabbitTemplate, TOPIC_NAME, "*",new Gson().toJson(order)); |
| | | } |
| | | |
| | | } |