RABTTMQ PHP Delay message related code (online did not find themselves thinking about the fix)

Source: Internet
Author: User

Objective:
As an operation and maintenance personnel do not back pot, who back it!


Body:

Online are 2 ways to achieve, the second I will not say, to upgrade rabbitmq Others also say what is not

OK ~ Today according to the online TTL + DLX Way to implement the delay message (Java Python related code has no Huang Film!!!!) )


Code:


1. First you have to create a new queue for the new switch to hold the delay message and set the new queue message TTL expires after forwarding the switch and routeing key (my old switch for e_test key for K1)


The main code is:

function Mqinityanchi ($e _name, $q _name, $k _route, $conn)

{

Creating Connections and Channel

$channel = new Amqpchannel ($conn);

To create a switch object

$ex = new Amqpexchange ($channel);

$ex->setname ($e _name);

$ex->settype (Amqp_ex_type_direct); Must be set

$ex->setflags (amqp_durable); Persistence does not have to be selected

echo "Exchange status:" $ex->declareexchange (). ' \ n ';


Binds the switch to the queue and specifies the routing key

Create a queue

$q = new Amqpqueue ($channel);

$q->setname ($q _name);

# $q->setflags (amqp_durable); Persistence of

$q->setargument (' X-dead-letter-exchange ', ' e_test '); Set TTL timeout forwarding to E_test

$q->setargument (' X-dead-letter-routing-key ', ' K1 '); Set TTL timeout to forward the key to K1

echo "Message total:". $q->declare (). " \ n ";


Echo ' Queue Bind: '. $q->bind ($e _name, $k _route). " \ n ";


return $ex;

}


2. That is, when the message is sent, set the TTL on the line. It's really simple.


Main code:


$message = "Test delay!";


$ex 1=mqinityanchi (' E_yanchi ', ' Q_yanchi ', ' K1 ', $conn);

$attributes = Array (' Expiration ' = 5000); Set to 5 Seconds

$ex 1->publish ($message, ' K1 ', ' 1 ', $attributes);




So that it can be sent to the Q_yanchi queue inside the message five seconds after expiration forwarded to the E_test-bound switch routing-key to the K1 queue.


It's a little rough to get off work. You can leave a message.

RABTTMQ PHP Delay message related code (online did not find themselves thinking about the fix)

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.