全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 阿里云办公 培训与认证 物联网
消息服务

队列使用手册

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

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

1. 准备

  • 下载最新版python sdk,解压后进入mns_python_sdk子目录;
  • 打开sample.cfg文件,配置AccessKeyID、AccessKeySecret和Endpoint;
    • AccessKeyId、AccessKeySecret
    • Endpoint
      • 访问MNS的接入地址,登陆MNS控制台 单击右上角 获取Endpoint 查看;
      • 不同地域的接入地址不同;
    • SecurityToken
      • 阿里云访问控制服务提供的短期访问权限凭证,直接使用阿里云账号或者子账号访问不需要配置该项,了解详情;
  • 进入sample目录,后续使用的脚本都在这里;

2. 创建队列

运行 createqueue.py 创建队列;
默认创建的队列名称是 MySampleQueue,也可以通过参数指定队列名称;
队列详细信息请参考详情;

  • 运行
  1. $python createqueue.py MyQueue1
  2. Create Queue Succeed! QueueName:MyQueue1
  • 核心代码

endpoint,accid,acckey和token从第1步的配置文件中读取;

  1. #init my_account, my_queue
  2. my_account = Account(endpoint, accid, acckey, token)
  3. queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue"
  4. my_queue = my_account.get_queue(queue_name)
  5. #you can get more information of QueueMeta from mns/queue.py
  6. queue_meta = QueueMeta()
  7. try:
  8. queue_url = my_queue.create(queue_meta)
  9. print "Create Queue Succeed! QueueName:%s\n" % queue_name
  10. except MNSExceptionBase, e:
  11. if e.type == "QueueAlreadyExist":
  12. print "Queue already exist, please delete it before creating or use it directly."
  13. sys.exit(0)
  14. print "Create Queue Fail! Exception:%s\n" % e

3. 发送消息

运行sendmessage.py,发送多条消息到队列中;
如果第2步指定了队列名称,这里同样通过参数指定队列名称;
消息详细信息请参考详情;

  • 运行
  1. $python sendmessage.py MyQueue1
  2. ==========Send Message To Queue==========
  3. QueueName:MyQueue1
  4. MessageCount:3
  5. Send Message Succeed! MessageBody:I am test message 0. MessageID:3EBE662B52BC99BC-1-154BD99CCA7-200000001
  6. Send Message Succeed! MessageBody:I am test message 1. MessageID:64B92941FC57837F-2-154BD99CCCE-200000001
  7. Send Message Succeed! MessageBody:I am test message 2. MessageID:3EBE662B52BC99BC-1-154BD99CCF0-200000002
  • 核心代码

endpoint,accid,acckey和token从第1步的配置文件中读取;

  1. #init my_account, my_queue
  2. my_account = Account(endpoint, accid, acckey, token)
  3. queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue"
  4. my_queue = my_account.get_queue(queue_name)
  5. #send some messages
  6. msg_count = 3
  7. print "%sSend Message To Queue%s\nQueueName:%s\nMessageCount:%s\n" % (10*"=", 10*"=", queue_name, msg_count)
  8. for i in range(msg_count):
  9. try:
  10. msg_body = "I am test message %s." % i
  11. msg = Message(msg_body)
  12. re_msg = my_queue.send_message(msg)
  13. print "Send Message Succeed! MessageBody:%s MessageID:%s" % (msg_body, re_msg.message_id)
  14. except MNSExceptionBase, e:
  15. if e.type == "QueueNotExist":
  16. print "Queue not exist, please create queue before send message."
  17. sys.exit(0)
  18. print "Send Message Fail! Exception:%s\n" % e

4. 接收和删除消息

运行recvdelmessage.py,接收并删除队列中的消息,直到队列为空;
如果第2步指定了队列名称,这里同样通过参数指定队列名称;
程序中receive message使用long polling方式,指定 wait seconds为3秒,因此当队列为空时,程序会等待3秒;
消息详细信息请参考详情;

  • 运行
  1. $python recvdelmessage.py MyQueue1
  2. ==========Receive And Delete Message From Queue==========
  3. QueueName:MyQueue1
  4. WaitSeconds:3
  5. Receive Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTEtOA== MessageBody:I am test message 0. MessageID:3EBE662B52BC99BC-1-154BD99CCA7-200000001
  6. Delete Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTEtOA==
  7. Receive Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5NC0xNDYzNDcwNDU4LTEtOA== MessageBody:I am test message 2. MessageID:3EBE662B52BC99BC-1-154BD99CCF0-200000002
  8. Delete Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5NC0xNDYzNDcwNDU4LTEtOA==
  9. Receive Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTItOA== MessageBody:I am test message 1. MessageID:64B92941FC57837F-2-154BD99CCCE-200000001
  10. Delete Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTItOA==
  11. Queue is empty!
  • 核心代码

endpoint,accid,acckey和token从第1步的配置文件中读取;

  1. #init my_account, my_queue
  2. my_account = Account(endpoint, accid, acckey, token)
  3. queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue"
  4. my_queue = my_account.get_queue(queue_name)
  5. #receive and delete message from queue util the queue is empty
  6. #set the long polling wait time 3 seoncds by wait_seconds
  7. wait_seconds = 3
  8. print "%sReceive And Delete Message From Queue%s\nQueueName:%s\nWaitSeconds:%s\n" % (10*"=", 10*"=", queue_name, wait_seconds)
  9. while True:
  10. #receive message
  11. try:
  12. recv_msg = my_queue.receive_message(wait_seconds)
  13. print "Receive Message Succeed! ReceiptHandle:%s MessageBody:%s MessageID:%s" % (recv_msg.receipt_handle, recv_msg.message_body, recv_msg.message_id)
  14. except MNSExceptionBase,e:
  15. if e.type == "QueueNotExist":
  16. print "Queue not exist, please create queue before receive message."
  17. sys.exit(0)
  18. elif e.type == "MessageNotExist":
  19. print "Queue is empty!"
  20. sys.exit(0)
  21. print "Receive Message Fail! Exception:%s\n" % e
  22. continue
  23. #delete message
  24. try:
  25. my_queue.delete_message(recv_msg.receipt_handle)
  26. print "Delete Message Succeed! ReceiptHandle:%s" % recv_msg.receipt_handle
  27. except MNSException,e:
  28. print "Delete Message Fail! Exception:%s\n" % e

5. 删除队列

运行deletequeue.py 删除队列

  • 运行
  1. $python deletequeue.py MyQueue1
  2. Delete Queue Succeed! QueueName:MyQueue1
  • 核心代码

endpoint,accid,acckey和token从第1步的配置文件中读取;

  1. #init my_account, my_queue
  2. my_account = Account(endpoint, accid, acckey, token)
  3. queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue"
  4. my_queue = my_account.get_queue(queue_name)
  5. #delete queue
  6. try:
  7. my_queue.delete()
  8. print "Delete Queue Succeed! QueueName:%s\n" % queue_name
  9. except MNSExceptionBase, e:
  10. print "Delete Queue Fail! Exception:%s\n" % e
本文导读目录
本文导读目录
以上内容是否对您有帮助?