Home > Backend Development > PHP Tutorial > Try RabbitMQ (amqp extension) with PHP_PHP Tutorial

Try RabbitMQ (amqp extension) with PHP_PHP Tutorial

WBOY
Release: 2016-07-13 17:53:08
Original
1297 people have browsed it

I posted an article "Try RabbitMQ with Python" two years ago. Unexpectedly, two years later, the amqp information based on PHP is still very little. The original extensions have been abandoned one by one, leaving only amqp is still alive and included in PECL. Although it is included, the information in the official manual is still a little thin.
Closer to home, I won’t go into details about the installation of amqp extension. You can refer to the article "Installing amqp extension for PHP" posted a few days ago
After installing amqp, you can start writing code:

Consumer: Receive message

Logic:
Create connection-->Create channel-->Create switch-->Create queue-->Bind switch/queue/routing key-->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'; //Routing key

//Create connection and channel
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
Die("Cannot connect to the broker!n");
}
$channel = new AMQPChannel($conn);

//Create switch
$ex = new AMQPExchange($channel);
$ex->setName($e_name);
$ex->setType(AMQP_EX_TYPE_DIRECT); //direct type
$ex->setFlags(AMQP_DURABLE); //Persistence
echo "Exchange Status:".$ex->declare()."n";
 
//Create queue
$q = new AMQPQueue($channel);
$q->setName($q_name);
$q->setFlags(AMQP_DURABLE); //Persistence
echo "Message Total:".$q->declare()."n";

//Bind the switch and queue, and specify the routing key
echo 'Queue Bind: '.$q->bind($e_name, $k_route)."n";

//Receive messages in blocking mode
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"; //Process message
$queue->ack($envelope->getDeliveryTag()); //Manually send ACK response
}

Producer: Send message
Logic:
Create connection-->Create channel-->Create switch object-->Send 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'; //Routing key

//Create connection 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!";

//Create switch object
$ex = new AMQPExchange($channel);
$ex->setName($e_name);

//Send message
//$channel->startTransaction(); //Start transaction
for($i=0; $i<5; ++$i){
echo "Send Message:".$ex->publish($message, $k_route)."n";
}
//$channel->commitTransaction(); //Commit transaction

$conn->disconnect();
Things to note are:

The queue object has two methods that can be used to get messages: consume and get.
The former is blocking and will be suspended when there is no message, so it is suitable for use in loops;
The latter is non-blocking. If there is a message to be fetched, it will be fetched, and if there is no message, it will return false.

Test screenshots

Run consumer:

Try RabbitMQ (amqp extension) with PHP_PHP Tutorial

Run the producer and send a message:

Try RabbitMQ (amqp extension) with PHP_PHP Tutorial
Consumer receives message:


Try RabbitMQ (amqp extension) with PHP_PHP Tutorial


Author: linvo

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478053.htmlTechArticleI published an article "Try RabbitMQ with Python" two years ago. Unexpectedly, two years later, based on There is still very little amqp information for PHP, and the original extensions have been abandoned one by one, leaving only...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template