Two years ago has sent a "try rabbitmq with Python", did not expect two years later today, based on the AMQP data PHP is still very poor, the original several extensions are discarded, only the AMQP is still alive, and was pecl included. Although it is included, the information in the Official handbook is slightly thin.
Anyway, the installation of AMQP extension is not much to say, you can see the previous days of the "Install AMQP extensions to PHP"
Once you've installed AMQP, you're ready to start writing code:
Consumer: Receiving Messages
Logic:
Create a connection--create a channel--> switch--Create a queue--bind switch/queue/route keys receive Message
[PHP]
/*************************************
* PHP AMQP (RabbitMQ) Demo-consumer
* AUTHOR:LINVO
* DATE:2012/7/30
*************************************/
Configuration information
$conn _args = Array (
' Host ' = ' 192.168.1.93 ',
' Port ' = ' 5672 ',
' Login ' = ' guest ',
' Password ' = ' guest ',
' Vhost ' = '/'
);
$e _name = ' e_linvo '; Switch name
$q _name = ' q_linvo '; Queue name
$k _route = ' key_1 '; Route key
Creating Connections and Channel
$conn = new Amqpconnection ($conn _args);
if (! $conn->connect ()) {
Die ("Cannot connect to the broker!\n");
}
$channel = new Amqpchannel ($conn);
Create a switch
$ex = new Amqpexchange ($channel);
$ex->setname ($e _name);
$ex->settype (Amqp_ex_type_direct); Direct type
$ex->setflags (amqp_durable); Persistence of
echo "Exchange Status:" $ex->declare (). " \ n ";
Create a queue
$q = new Amqpqueue ($channel);
$q->setname ($q _name);
$q->setflags (amqp_durable); Persistence of
echo "Message total:". $q->declare (). " \ n ";
Binds the switch to the queue and specifies the routing key
Echo ' Queue Bind: '. $q->bind ($e _name, $k _route). " \ n ";
Blocking mode receiving messages
echo "message:\n";
while (True) {
$q->consume (' ProcessMessage ');
$q->consume (' ProcessMessage ', amqp_autoack); Automatic ACK response
}
$conn->disconnect ();
/**
* Consumption callback function
* Processing messages
*/
function ProcessMessage ($envelope, $queue) {
$msg = $envelope->getbody ();
echo $msg. " \ n "; Processing messages
$queue->ack ($envelope->getdeliverytag ()); To send an ACK response manually
}
Producer: Send Message
Logic:
Create a connection--create a channel--> switch Object--Send a message
[PHP]
/*************************************
* PHP AMQP (RabbitMQ) demo-publisher
* AUTHOR:LINVO
* DATE:2012/7/30
*************************************/
Configuration information
$conn _args = Array (
' Host ' = ' 192.168.1.93 ',
' Port ' = ' 5672 ',
' Login ' = ' guest ',
' Password ' = ' guest ',
' Vhost ' = '/'
);
$e _name = ' e_linvo '; Switch name
$q _name = ' q_linvo '; No queue name required
$k _route = ' key_1 '; Route key
Creating Connections and Channel
$conn = new Amqpconnection ($conn _args);
if (! $conn->connect ()) {
Die ("Cannot connect to the broker!\n");
}
$channel = new Amqpchannel ($conn);
Message content
$message = "TEST message! Test Message! ";
To create a switch object
$ex = new Amqpexchange ($channel);
$ex->setname ($e _name);
Send Message
$channel->starttransaction (); Start a transaction
for ($i =0; $i <5; + + $i) {
echo "Send Message:". $ex->publish ($message, $k _route). " \ n ";
}
$channel->committransaction (); Commit a transaction
$conn->disconnect ();
The areas to note are:
The queue object has two methods available for fetching messages: consume and get.
The former is blocked, no message will be suspended, suitable for use in the loop;
The latter is non-blocking, when the message is taken, and none returns false.
Test
Run the consumer:
Run producer, send message:
The consumer receives the message:
Author: linvo
http://www.bkjia.com/PHPjc/478053.html www.bkjia.com true http://www.bkjia.com/PHPjc/478053.html techarticle two years ago has sent a "try rabbitmq with Python", did not expect two years later today, PHP-based AMQP data is still very poor, the original several extensions have been discarded, only left ...