admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
utils/src/main/java/org/yeshi/utils/CMQUtil.java
@@ -7,6 +7,7 @@
import com.qcloud.cmq.Message;
import com.qcloud.cmq.Queue;
import com.qcloud.cmq.QueueMeta;
import com.qcloud.cmq.Topic;
//腾讯CMQ消息
public class CMQUtil {
@@ -26,10 +27,12 @@
   // 内网 http://cmq-queue-gz.api.tencentyun.com
   // 外网 http://cmq-queue-gz.api.qcloud.com
   private static String endpoint = "http://cmq-queue-gz.api.qcloud.com";
   private static String topicEndPoint = "https://cmq-topic-gz.api.qcloud.com";
   // private static String endpoint =
   // "http://cmq-queue-gz.api.tencentyun.com";
   private Account account;
   private Account topicAccount;
   static {
      // if (SystemUtil.getSystemType() == SystemUtil.SYSTEM_LINUX)
@@ -42,6 +45,7 @@
      this.secretId = secretId;
      this.secretKey = secretKey;
      account = new Account(endpoint, this.secretId, this.secretKey);
      topicAccount = new Account(topicEndPoint, this.secretId, this.secretKey);
   }
   public boolean existQueue(String queueName) {
@@ -192,17 +196,17 @@
    */
   public List<Message> recieveMsg(int count, String queueName) {
      Queue queue = getQueue(queueName);
      if (queue == null ) {
      if (queue == null) {
         return null;
      }
      List<Message> msgList = null;
      try {
         msgList = queue.batchReceiveMessage(count, 20);
         return msgList;
      } catch (Exception e) {
         if (e.getMessage()!=null&& !e.getMessage().contains("no message"))
         if (e.getMessage() != null && !e.getMessage().contains("no message"))
            e.printStackTrace();
      }
      return null;
@@ -230,4 +234,118 @@
      }
      return false;
   }
   /**
    * 订阅消息相关
    */
   /**
    * 创建订阅主题
    *
    * @param topicName-主题名称
    * @param maxMsgSize-消息最大长度
    * @param filterType-过滤类型
    * @return
    */
   public boolean createTopic(String topicName, int maxMsgSize, int filterType) {
      try {
         topicAccount.createTopic(topicName, maxMsgSize, filterType);
         return true;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return false;
   }
   /**
    * 创建默认参数的主题
    *
    * @param topicName
    * @return
    */
   public boolean createTopic(String topicName) {
      try {
         topicAccount.createTopic(topicName, 65536);
         return true;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return false;
   }
   /**
    * 订阅主题
    *
    * @param topicName-主题名称
    * @param subscriptionName-订阅名称
    * @param queueName-接受消息的队列名称
    * @return
    */
   public boolean subscribeTopic(String topicName, String subscriptionName, String queueName) {
      try {
         topicAccount.createSubscribe(topicName, subscriptionName, queueName, "queue");
      } catch (Exception e) {
         e.printStackTrace();
      }
      return false;
   }
   /**
    * 删除订阅
    *
    * @param topicName
    * @param subscriptionName
    * @return
    */
   public boolean deleteSubscribeTopic(String topicName, String subscriptionName) {
      try {
         topicAccount.deleteSubscribe(topicName, subscriptionName);
         return true;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return false;
   }
   /**
    * 发布订阅消息
    *
    * @param topicName
    * @param message
    * @return
    */
   public boolean publishTopicMessage(String topicName, String message) {
      try {
         Topic topic = topicAccount.getTopic(topicName);
         if (topic == null)
            return false;
         topic.publishMessage(message);
         return true;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return false;
   }
   /**
    * 批量发布消息
    *
    * @param topicName
    * @param msgList
    * @return
    */
   public boolean batchPublishTopicMessage(String topicName, List<String> msgList) {
      try {
         Topic topic = topicAccount.getTopic(topicName);
         if (topic == null)
            return false;
         topic.batchPublishMessage(msgList);
         return true;
      } catch (Exception e) {
         e.printStackTrace();
      }
      return false;
   }
}