How to use TTL+DLX to implement delayed messages
This article mainly introduces how to use TTL+DLX to achieve delayed messages, the article is very detailed, has a certain reference value, interested friends must read it!
Text:
There are two ways to achieve it on the Internet. I won't talk about the second one. If you want to upgrade rabbitmq, others will say what you can't do.
TTL + DLX way to achieve delay messages (java python related code is not shooting huang film! )
Masturbate code:
1. First, you have to create a new queue, a new switch to store delayed messages and set up the switch and routeing key to forward after the expiration of the new queue message ttl (my old switch is e_test key is K1)
The main code is:
Function mqInitYanChi ($eBay _ name _
{
/ / create a connection and channel
$channel = new AMQPChannel ($conn)
/ / create switch object
$ex = new AMQPExchange ($channel)
$ex- > setName ($e_name)
$ex- > setType (AMQP_EX_TYPE_DIRECT); / / must be set
$ex- > setFlags (AMQP_DURABLE); / / persistence may not be selected
Echo "exchange status:". $ex- > declareExchange ().\ n'
/ / bind the switch and queue and specify the routing key
/ / create a queue
$Q = new AMQPQueue ($channel)
$Q-> setName ($q_name)
# $Q-> setFlags (AMQP_DURABLE); / / persistence
$Q-> setArgument ('xmurf words, exchangeholders, etc.); / / set the ttl timeout to forward to e_test
$Q-> setArgument ('xmurf words, words, routinghouses, keyboards, etc. K1'); / / set the key of ttl timeout forwarding to K1
Echo "Message Total:". $Q-> declare (). "\ n"
Echo 'Queue Bind:. $Q-> bind ($e_name, $k_route). "\ n"
Return $ex
}
two。 That is, setting ttl when sending messages is actually very simple.
Main code:
$message = "Test delay!"
$ex1=mqInitYanChi ('ethereal yanchifang pr é cor qlocyanchi pr é cor K1l pr orchrary conn)
$attributes = array ('expiration' = > 5000); / / set to 5 seconds
$ex1- > publish ($message,'k1','1',$attributes)
In this way, the message sent to the q_yanchi queue can be overdue and forwarded to the routing-key K1 queue of the e_test-bound switch after five seconds.
The above is all the contents of the article "how to use TTL+DLX to achieve delayed messages". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!