Use PHP to try RabbitMQ (amqp extension ). Two years ago, I published an article "try RabbitMQ with Python". I did not expect that today, two years later, the amqp data based on PHP is still poor, and several extensions are also discarded one by one, two years ago, I sent an article "try RabbitMQ with Python". I didn't expect that the amqp data based on PHP is still quite poor today, several original extensions were also discarded one by one, with only amqp remaining and included in PECL. Although it is included, the information in the official manual is still slightly thin.
To put it bluntly, there is not much to say about the installation of amqp Extensions. For more information, see the article "installing amqp extensions for PHP" published a few days ago.
After installing amqp, you can start to write the code:
Consumer: Receives Messages
Logic:
Create a connection --> create a channel --> Create a switch --> Create a queue --> bind a switch, queue, or route key --> receive a message
[Php]
/*************************************
* PHP amqp (RabbitMQ) Demo-consumer
* Author: Linvo
* Date: 2012/7/30
*************************************/
// Configuration information
$ Conn_args = array (
'Host' => '2017. 168.1.93 ',
'Port' => '123 ',
'Login' => 'guest ',
'Password' => 'guest ',
'Vhost' => '/'
);
$ E_name = 'e _ linvo '; // switch name
$ Q_name = 'Q _ linvo '; // queue name
$ K_route = 'key _ 1'; // route key
// Create a connection and a channel
$ Conn = new AMQPConnection ($ conn_args );
If (! $ Conn-> connect ()){
Die ("Cannot connect to the broker! \ N ");
}
$ Channel = new AMQPChannel ($ conn );
// Create a vSwitch
$ 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 a queue
$ Q = new AMQPQueue ($ channel );
$ Q-> setName ($ q_name );
$ Q-> setFlags (AMQP_DURABLE); // persistence
Echo "Message Total:". $ q-> declare (). "\ n ";
// Bind the vSwitch and queue, and specify the route key
Echo 'queue Bind: '. $ q-> bind ($ e_name, $ k_route). "\ n ";
// Receives 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
* Process messages
*/
Function processMessage ($ envelope, $ queue ){
$ Msg = $ envelope-> getBody ();
Echo $ msg. "\ n"; // process the message
$ Queue-> ack ($ envelope-> getDeliveryTag (); // manually send an ACK response
}
Producer: send messages
Logic:
Create a connection --> create a channel --> Create a switch object --> Send a Message
[Php]
/*************************************
* PHP amqp (RabbitMQ) Demo-publisher
* Author: Linvo
* Date: 2012/7/30
*************************************/
// Configuration information
$ Conn_args = array (
'Host' => '2017. 168.1.93 ',
'Port' => '123 ',
'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
// Create a connection and a 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 a vSwitch object
$ Ex = new AMQPExchange ($ channel );
$ Ex-> setName ($ e_name );
// Send a message
// $ Channel-> startTransaction (); // start the transaction
For ($ I = 0; $ I <5; ++ $ I ){
Echo "Send Message:". $ ex-> publish ($ message, $ k_route). "\ n ";
}
// $ Channel-> commitTransaction (); // submit the transaction
$ Conn-> disconnect ();
Note the following:
The queue object has two methods for obtaining messages: consume and get.
The former is blocked and will be suspended when there is no message, which is suitable for loop use;
The latter is non-blocking. If no message is received, false is returned.
Test
Running consumer:
Run the producer to send messages:
The consumer receives the message:
Author: linvo
...