閱讀1029 返回首頁    go 新東方


主題使用手冊__Java SDK_SDK使用手冊_消息服務-阿裏雲

本文檔介紹如何使用java sdk中的sample代碼,完成創建主題、創建訂閱,發布消息、接收消息以及刪除主題等操作。

1. 準備

  • 下載最新版java sdk,解壓到aliyun-sdk-mns-samples文件夾;
  • 用Eclipse導入Maven工程,選中aliyun-sdk-mns-samples文件夾;
  • 在用戶目錄(Linux係統為”/home/YOURNAME/“目錄或者Windows係統為 “C:UsersYOURNAME”目錄)中創建.aliyun-mns.properties文件,並填寫服務地址、AccessKeyID和AccessKeySecret:
    • AccessKeyId、AccessKeySecret
    • Endpoint
      • 訪問MNS的接入地址,登陸MNS控製台 單擊右上角 獲取Endpoint 查看;
      • 不同地域的接入地址不同,分為公網以及內網域名;

2. 創建主題

下麵給出了創建主題的代碼示例,有關主題詳細信息請參考詳情;

  1. public class CreateTopicDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. String topicName = "TestTopic";
  6. TopicMeta meta = new TopicMeta();
  7. meta.setTopicName(topicName);
  8. try {
  9. CloudTopic topic = client.createTopic(meta);
  10. } catch (Exception e)
  11. e.printStackTrace();
  12. System.out.println("create topic error, " + e.getMessage());
  13. }
  14. client.close();
  15. }
  16. }

3. 啟動HttpEndpoint

aliyun-sdk-mns-samples中有一個HttpEndpoint.java類,簡單實現了一個本地啟動的Http消息接收端,主要功能包括:

  1. 對MNS推送消息請求做簽名驗證;
  2. 解析推送請求的消息body體;
  3. 返回相應的處理返回碼:200;

HttpEndpoint的具體實現源碼可參考sdk中源碼;

4. 創建訂閱

對已經創建好的主題Topic進行訂閱,在訂閱時需要設置對應的推送Endpoint地址(目前支持HTTP、郵件以及隊列)、錯誤重試策略、推送消息格式等;

  1. public class SubscribeDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. CloudTopic topic = client.getTopicRef("TestTopic");
  6. try {
  7. SubscriptionMeta subMeta = new SubscriptionMeta();
  8. subMeta.setSubscriptionName("TestSub");
  9. subMeta.setEndpoint(HttpEndpoint.GenEndpointLocal());
  10. subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.XML);
  11. String subUrl = topic.subscribe(subMeta);
  12. System.out.println("subscription url: " + subUrl);
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. System.out.println("subscribe/unsubribe error");
  16. }
  17. client.close();
  18. }
  19. }

5.發布消息

在創建好主題以及訂閱之後,並且已經啟動了HttpEndpoint,我們可以向Topic發布消息。

  1. public class PublishMessageDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. CloudTopic topic = client.getTopicRef("TestTopic");
  6. try {
  7. TopicMessage msg = new Base64TopicMessage(); //可以使用TopicMessage結構,選擇不進行Base64加密
  8. msg.setMessageBody("hello world!");
  9. msg = topic.publishMessage(msg);
  10. System.out.println(msg.getMessageId());
  11. System.out.println(msg.getMessageBodyMD5());
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. System.out.println("subscribe error");
  15. }
  16. client.close();
  17. }
  18. }

6. 查看HttpEndpoint接收消息

第5步發布了一條消息到Topic中,MNS會將該消息推送到所有的訂閱Endpoint,本例中的HttpEndpoint會將接收到的消息打印出來。

7. 取消訂閱

如果不需要接收主題的消息,則可以選擇取消訂閱。

  1. public class UnsubscribeDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. CloudTopic topic = client.getTopicRef("TestTopic");
  6. try {
  7. topic.unsubscribe("TestSub");
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. System.out.println("unsubribe error");
  11. }
  12. client.close();
  13. }
  14. }

8.刪除主題

最後選擇將Topic刪除。

  1. public class DeleteTopicDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. CloudTopic topic = client.getTopicRef("TestTopic");
  6. try {
  7. topic.delete();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. System.out.println("delete topic error");
  11. }
  12. client.close();
  13. }
  14. }

最後更新:2016-11-23 16:04:12

  上一篇:go 隊列使用手冊__Java SDK_SDK使用手冊_消息服務-阿裏雲
  下一篇:go 發送消息示例代碼__Java SDK_SDK使用手冊_消息服務-阿裏雲