RABBITMQ Learning Record-TTL

Source: Internet
Author: User

Send the monkeys to the reinforcements.The TTL of the queue itself

Note that the TTL of the queue itself is said here. It's not about the news.

When declaring a queue, you can specify the queue's TTL value with X-expires. After it expires, the queue is deleted.

No matter if there's any news, no spending.

#!/usr/bin/env python#-*-Coding:utf-8-*-" "the queue exists for only 5 seconds, whether there is messages!" "Import PikaImport SYSdef Main():    Body = "'.Join(SYS.argv[1:]) or ' Hello World '    Connection = Pika.blockingconnection(        Pika.connectionparameters(            Host=' localhost '))    Channel = Connection.Channel()    Channel.Queue_declare(Queue=' Ttlhello ',arguments={"X-expires": the}) # TTL 5 second    Channel.Basic_publish(Exchange="',                          Routing_key=' Ttlhello ',                          Body=Body,                          )    Connection.Close()if __name__ == ' __main__ ':    Main()

Run for a look at the effect

# python queueTTL.py ;sleep 1; rabbitmqctl list_queues; sleep 6;rabbitmqctl list_queues  Listing queues ...  ttlhello    1  Listing queues ...  
Per-queue Message TTL

This is also the property of the queue, not the message. All messages in the queue are deleted when the TTL is over.

#!/usr/bin/env python#-*-Coding:utf-8-*-" "The messages in the queue exist for only 5 seconds" "Import PikaImport SYSdef Main():    Body = "'.Join(SYS.argv[1:]) or ' Hello World '    Connection = Pika.blockingconnection(        Pika.connectionparameters(            Host=' localhost '))    Channel = Connection.Channel()    Channel.Queue_declare(Queue=' Ttlmessagehello ',arguments={"X-message-ttl": the}) # TTL 5 second    Channel.Basic_publish(Exchange="',                          Routing_key=' Ttlmessagehello ',                          Body=Body,                          )    Connection.Close()if __name__ == ' __main__ ':    Main()

Run for a look at the effect

# python queueMessageTTL.py; rabbitmqctl list_queues; sleep 6; rabbitmqctl list_queues  Listing queues ...  1  Listing queues ...  0  
Per-message TTL

When sending a message, you can also give each message a TTL attribute.

messagettlsend.py

#!/usr/bin/env python#-*-Coding:utf-8-*-Import PikaImport SYSdef Main():    Body = "'.Join(SYS.argv[1:]) or ' Hello World '    Connection = Pika.blockingconnection(        Pika.connectionparameters(            Host=' localhost '))    Channel = Connection.Channel()    Channel.Queue_declare(Queue=' Hello ')    Channel.Basic_publish(Exchange="',                          Routing_key=' Hello ',                          Body=Body,                          Properties=Pika.basicproperties(                              Expiration=" the"                             )                          )    Connection.Close()if __name__ == ' __main__ ':    Main()

Run for a look at the effect

# python messageTTLSend.py; rabbitmqctl list_queues; sleep 6; rabbitmqctl list_queues;  Listing queues ...  hello   1  Listing queues ...  hello   0  

Finally, as Per-message ttl and Per-queue message ttl are not the same, press small.

RABBITMQ Learning Record-TTL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.