From 98b1a0affd69bbe63223c21fdd2c404e8bedfccb Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期三, 20 五月 2020 17:25:08 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/div' into 2.1.2 --- utils/src/main/java/org/yeshi/utils/CMQUtil.java | 126 ++++++++++++++++++++++++++++++++++++++++- 1 files changed, 122 insertions(+), 4 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/CMQUtil.java b/utils/src/main/java/org/yeshi/utils/CMQUtil.java index 76c4682..1a5619a 100644 --- a/utils/src/main/java/org/yeshi/utils/CMQUtil.java +++ b/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; + } + } -- Gitblit v1.8.0