全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网 钉钉智能硬件
消息服务

队列使用手册

更新时间:2017-06-07 13:26:11

本文档介绍如何使用java sdk中的sample代码,完成创建队列、发送消息、接收删除消息和删除队列操作。

1. 准备

  • 下载最新版java sdk,解压到aliyun-sdk-mns-samples文件夹;
  • 用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;
  • 在用户目录(Linux系统为”/home/YOURNAME/“目录或者Windows系统为 “C:\Users\YOURNAME”目录)中创建.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 se)
  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 se)
  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. }
本文导读目录