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