隊列使用手冊__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
- 訪問阿裏雲API的密鑰對;
- 如果使用主賬號訪問,登陸阿裏雲 AccessKey 管理頁麵創建、查看;
- 如果使用子賬號訪問,請登錄阿裏雲訪問控製控製台查看;
- Endpoint
- 訪問MNS的接入地址,登陸MNS控製台 單擊右上角 獲取Endpoint 查看;
- 不同地域的接入地址不同,分為公網以及內網域名;
- AccessKeyId、AccessKeySecret
2. 創建隊列
下麵給出了創建隊列的代碼段(隊列詳細信息請參考詳情);
public class CreateQueueDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全String queueName = "TestQueue";QueueMeta meta = new QueueMeta(); //生成本地QueueMeta屬性,有關隊列屬性詳細介紹見https://help.aliyun.com/document_detail/27476.htmlmeta.setQueueName(queueName); // 設置隊列名meta.setPollingWaitSeconds(15);meta.setMaxMessageSize(2048L);try {CloudQueue queue = client.createQueue(meta);} catch (ClientException ce){System.out.println("Something wrong with the network connection between client and MNS service."+ "Please check your network and DNS availablity.");ce.printStackTrace();} catch (ServiceException ex){se.printStackTrace();logger.error("MNS exception requestId:" + se.getRequestId(), se);if (se.getErrorCode() != null) {if (se.getErrorCode().equals("QueueNotExist")){System.out.println("Queue is not exist.Please create before use");} else if (se.getErrorCode().equals("TimeExpired")){System.out.println("The request is time expired. Please check your local machine timeclock");}/*you can get more MNS service error code from following link:https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html*/}} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close(); // 程序退出時,需主動調用client的close方法進行資源釋放}}
3. 發送消息
創建完隊列之後,就可以向隊列發送消息
public class ProducerDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全try {CloudQueue queue = client.getQueueRef("TestQueue");Message message = new Message();message.setMessageBody("I am test message ");Message putMsg = queue.putMessage(message);System.out.println("Send message id is: " + putMsg.getMessageId());} catch (ClientException ce){System.out.println("Something wrong with the network connection between client and MNS service."+ "Please check your network and DNS availablity.");ce.printStackTrace();} catch (ServiceException ex){se.printStackTrace();logger.error("MNS exception requestId:" + se.getRequestId(), se);if (se.getErrorCode() != null) {if (se.getErrorCode().equals("QueueNotExist")){System.out.println("Queue is not exist.Please create before use");} else if (se.getErrorCode().equals("TimeExpired")){System.out.println("The request is time expired. Please check your local machine timeclock");}/*you can get more MNS service error code from following link:https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html*/}} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close(); // 程序退出時,需主動調用client的close方法進行資源釋放}}
4. 接收和刪除消息
隊列中已經發送了1條消息,下麵是從隊列中取出並刪除該條消息。
public class ConsumerDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全try{CloudQueue queue = client.getQueueRef("TestQueue");Message popMsg = queue.popMessage();if (popMsg != null){System.out.println("message handle: " + popMsg.getReceiptHandle());System.out.println("message body: " + popMsg.getMessageBodyAsString());System.out.println("message id: " + popMsg.getMessageId());System.out.println("message dequeue count:" + popMsg.getDequeueCount());//刪除已經取出消費的消息queue.deleteMessage(popMsg.getReceiptHandle());System.out.println("delete message successfully.n");}else{System.out.println("message not exist in TestQueue.n");}} catch (ClientException ce){System.out.println("Something wrong with the network connection between client and MNS service."+ "Please check your network and DNS availablity.");ce.printStackTrace();} catch (ServiceException se){se.printStackTrace();logger.error("MNS exception requestId:" + se.getRequestId(), se);if (se.getErrorCode() != null) {if (se.getErrorCode().equals("QueueNotExist")){System.out.println("Queue is not exist.Please create before use");} else if (se.getErrorCode().equals("TimeExpired")){System.out.println("The request is time expired. Please check your local machine timeclock");}/*you can get more MNS service error code from following link:https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html*/}} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close();}}
5. 刪除隊列
public class DeleteQueueDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全try{CloudQueue queue = client.getQueueRef("TestQueue");queue.delete();} catch (ClientException ce){System.out.println("Something wrong with the network connection between client and MNS service."+ "Please check your network and DNS availablity.");ce.printStackTrace();} catch (ServiceException se){se.printStackTrace();} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close();}}
最後更新:2016-11-23 16:04:12
上一篇:
下載__Java SDK_SDK使用手冊_消息服務-阿裏雲
下一篇:
主題使用手冊__Java SDK_SDK使用手冊_消息服務-阿裏雲
添加解析記錄__解析管理接口_API文檔_雲解析-阿裏雲
MetaService__用戶指南_E-MapReduce-阿裏雲
資源報表__控製台使用指南_消息隊列 MQ-阿裏雲
環境配置問題__常見問題_彈性伸縮-阿裏雲
錯誤碼表__錯誤說明_API 網關-阿裏雲
查詢資源類型模板信息__資源相關接口_API 文檔_資源編排-阿裏雲
鏡像部署Windows環境__建站教程_雲服務器 ECS-阿裏雲
步驟 2:創建Windows實例__快速入門(Windows)_雲服務器 ECS-阿裏雲
類目管理__媒體庫管理_開發人員指南_視頻點播-阿裏雲
阿裏雲MaxCompute香港開服 將引入更多人工智能服務
相關內容
常見錯誤說明__附錄_大數據計算服務-阿裏雲
發送短信接口__API使用手冊_短信服務-阿裏雲
接口文檔__Android_安全組件教程_移動安全-阿裏雲
運營商錯誤碼(聯通)__常見問題_短信服務-阿裏雲
設置短信模板__使用手冊_短信服務-阿裏雲
OSS 權限問題及排查__常見錯誤及排除_最佳實踐_對象存儲 OSS-阿裏雲
消息通知__操作指南_批量計算-阿裏雲
設備端快速接入(MQTT)__快速開始_阿裏雲物聯網套件-阿裏雲
查詢API調用流量數據__API管理相關接口_API_API 網關-阿裏雲
使用STS訪問__JavaScript-SDK_SDK 參考_對象存儲 OSS-阿裏雲