Queue operations

Last Updated: Sep 11, 2017

This document introduces how to use sample code in Java SDK of MNS, including CreateQueue,SendMessage, ReceiveMessage, DeleteMesssage and DeleteQueue.

1. Preparation

  1. Download the latest version of Java sdk and unzip to aliyun-sdk-mns-samples folder.
  2. Import the maven project under folder: alien-idk-mns-samples.
  3. Create config file with the endpoint, AccessKeyID, AccessKeySecret under the user folder (/home/YOURNAME/).
  4. Config file sample:
    1. mns.accountendpoint=http://$accountid.mns.$region.aliyuncs.com
    2. mns.accesskeyid=$your_accesskeyid
    3. mns.accesskeysecret=$your_accesskeysecret
    Notes:
    • AK is the short name for $your_accesskeyid and $your_accesskeysecret, which are used for accessing Alibaba Cloud Service in config file above. AK can be fetched from following AccessKey Website while using main account or from Alibaba Cloud RAM Console while using sub-account.
    • mns.accountendpoint is the access point for Alibaba Cloud Message Service. It can be acquired from Message Service Console by clicking the button Get Endpoint on the top right corner.

2. CreateQueue

Sample code below shows how to create queue.

  1. public class CreateQueueDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient();
  5. String queueName = "TestQueue";
  6. QueueMeta meta = new QueueMeta(); //Set QueueMeta attributes with default values.
  7. meta.setQueueName(queueName); // Set Queue Name.
  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. } catch (Exception e)
  31. {
  32. System.out.println("Unknown exception happened!");
  33. e.printStackTrace();
  34. }
  35. client.close(); //Please close the client at the end of program.
  36. }
  37. }

More queue attributes please refer to Queue related operations.

You can get more MNS service error code from Error codes.

3. Producer: SendMessage

To send message to the queue as the code below shows.

  1. public class ProducerDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient();
  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. } catch (Exception e)
  30. {
  31. System.out.println("Unknown exception happened!");
  32. e.printStackTrace();
  33. }
  34. client.close();
  35. }
  36. }

You can get more MNS service error code from Error codes.

4. Consumer: ReceiveMessage and DeleteMessage

Receive and delete the message sent in step 3.

  1. public class ConsumerDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient();
  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. //Delete consumed messages
  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. } catch (Exception e)
  39. {
  40. System.out.println("Unknown exception happened!");
  41. e.printStackTrace();
  42. }
  43. client.close();
  44. }
  45. }

You can get more MNS service error code from Error codes.

5.Delete Queue

  1. public class DeleteQueueDemo {
  2. public static void main(String[] args) {
  3. CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
  4. MNSClient client = account.getMNSClient();
  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. }
Thank you! We've received your feedback.