閱讀951 返回首頁    go windows


隊列使用手冊__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 CreateQueueDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. String queueName = "TestQueue";
  6. QueueMeta meta = new QueueMeta(); //生成本地QueueMeta屬性,有關隊列屬性詳細介紹見https://help.aliyun.com/document_detail/27476.html
  7. meta.setQueueName(queueName); // 設置隊列名
  8. meta.setPollingWaitSeconds(15);
  9. meta.setMaxMessageSize(2048L);
  10. try {
  11. CloudQueue queue = client.createQueue(meta);
  12. } catch (ClientException ce)
  13. {
  14. System.out.println("Something wrong with the network connection between client and MNS service."
  15. + "Please check your network and DNS availablity.");
  16. ce.printStackTrace();
  17. } catch (ServiceException ex)
  18. {
  19. se.printStackTrace();
  20. logger.error("MNS exception requestId:" + se.getRequestId(), se);
  21. if (se.getErrorCode() != null) {
  22. if (se.getErrorCode().equals("QueueNotExist"))
  23. {
  24. System.out.println("Queue is not exist.Please create before use");
  25. } else if (se.getErrorCode().equals("TimeExpired"))
  26. {
  27. System.out.println("The request is time expired. Please check your local machine timeclock");
  28. }
  29. /*
  30. you can get more MNS service error code from following link:
  31. https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html
  32. */
  33. }
  34. } catch (Exception e)
  35. {
  36. System.out.println("Unknown exception happened!");
  37. e.printStackTrace();
  38. }
  39. client.close(); // 程序退出時,需主動調用client的close方法進行資源釋放
  40. }
  41. }

3. 發送消息

創建完隊列之後,就可以向隊列發送消息

  1. public class ProducerDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. try {
  6. CloudQueue queue = client.getQueueRef("TestQueue");
  7. Message message = new Message();
  8. message.setMessageBody("I am test message ");
  9. Message putMsg = queue.putMessage(message);
  10. System.out.println("Send message id is: " + putMsg.getMessageId());
  11. } catch (ClientException ce)
  12. {
  13. System.out.println("Something wrong with the network connection between client and MNS service."
  14. + "Please check your network and DNS availablity.");
  15. ce.printStackTrace();
  16. } catch (ServiceException ex)
  17. {
  18. se.printStackTrace();
  19. logger.error("MNS exception requestId:" + se.getRequestId(), se);
  20. if (se.getErrorCode() != null) {
  21. if (se.getErrorCode().equals("QueueNotExist"))
  22. {
  23. System.out.println("Queue is not exist.Please create before use");
  24. } else if (se.getErrorCode().equals("TimeExpired"))
  25. {
  26. System.out.println("The request is time expired. Please check your local machine timeclock");
  27. }
  28. /*
  29. you can get more MNS service error code from following link:
  30. https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html
  31. */
  32. }
  33. } catch (Exception e)
  34. {
  35. System.out.println("Unknown exception happened!");
  36. e.printStackTrace();
  37. }
  38. client.close(); // 程序退出時,需主動調用client的close方法進行資源釋放
  39. }
  40. }

4. 接收和刪除消息

隊列中已經發送了1條消息,下麵是從隊列中取出並刪除該條消息。

  1. public class ConsumerDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. try{
  6. CloudQueue queue = client.getQueueRef("TestQueue");
  7. Message popMsg = queue.popMessage();
  8. if (popMsg != null){
  9. System.out.println("message handle: " + popMsg.getReceiptHandle());
  10. System.out.println("message body: " + popMsg.getMessageBodyAsString());
  11. System.out.println("message id: " + popMsg.getMessageId());
  12. System.out.println("message dequeue count:" + popMsg.getDequeueCount());
  13. //刪除已經取出消費的消息
  14. queue.deleteMessage(popMsg.getReceiptHandle());
  15. System.out.println("delete message successfully.n");
  16. }
  17. else{
  18. System.out.println("message not exist in TestQueue.n");
  19. }
  20. } catch (ClientException ce)
  21. {
  22. System.out.println("Something wrong with the network connection between client and MNS service."
  23. + "Please check your network and DNS availablity.");
  24. ce.printStackTrace();
  25. } catch (ServiceException se)
  26. {
  27. se.printStackTrace();
  28. logger.error("MNS exception requestId:" + se.getRequestId(), se);
  29. if (se.getErrorCode() != null) {
  30. if (se.getErrorCode().equals("QueueNotExist"))
  31. {
  32. System.out.println("Queue is not exist.Please create before use");
  33. } else if (se.getErrorCode().equals("TimeExpired"))
  34. {
  35. System.out.println("The request is time expired. Please check your local machine timeclock");
  36. }
  37. /*
  38. you can get more MNS service error code from following link:
  39. https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html
  40. */
  41. }
  42. } catch (Exception e)
  43. {
  44. System.out.println("Unknown exception happened!");
  45. e.printStackTrace();
  46. }
  47. client.close();
  48. }
  49. }

5. 刪除隊列

  1. public class DeleteQueueDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient單例實現即可,多線程安全
  5. try{
  6. CloudQueue queue = client.getQueueRef("TestQueue");
  7. queue.delete();
  8. } catch (ClientException ce)
  9. {
  10. System.out.println("Something wrong with the network connection between client and MNS service."
  11. + "Please check your network and DNS availablity.");
  12. ce.printStackTrace();
  13. } catch (ServiceException se)
  14. {
  15. se.printStackTrace();
  16. } catch (Exception e)
  17. {
  18. System.out.println("Unknown exception happened!");
  19. e.printStackTrace();
  20. }
  21. client.close();
  22. }
  23. }

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

  上一篇:go 下載__Java SDK_SDK使用手冊_消息服務-阿裏雲
  下一篇:go 主題使用手冊__Java SDK_SDK使用手冊_消息服務-阿裏雲