|
| 1 | +<?php |
| 2 | + |
| 3 | +// from https://github.com/php-amqplib/php-amqplib demo |
| 4 | + |
| 5 | +include(__DIR__ . '/config.php'); |
| 6 | +use PhpAmqpLib\Connection\AMQPStreamConnection; |
| 7 | + |
| 8 | +$exchange = 'router'; |
| 9 | +$queue = 'msgs'; |
| 10 | +$consumerTag = 'consumer'; |
| 11 | + |
| 12 | +$connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST); |
| 13 | +$channel = $connection->channel(); |
| 14 | + |
| 15 | +/* |
| 16 | + The following code is the same both in the consumer and the producer. |
| 17 | + In this way we are sure we always have a queue to consume from and an |
| 18 | + exchange where to publish messages. |
| 19 | +*/ |
| 20 | + |
| 21 | +/* |
| 22 | + name: $queue |
| 23 | + passive: false |
| 24 | + durable: true // the queue will survive server restarts |
| 25 | + exclusive: false // the queue can be accessed in other channels |
| 26 | + auto_delete: false //the queue won't be deleted once the channel is closed. |
| 27 | +*/ |
| 28 | +$channel->queue_declare($queue, false, true, false, false); |
| 29 | + |
| 30 | +/* |
| 31 | + name: $exchange |
| 32 | + type: direct |
| 33 | + passive: false |
| 34 | + durable: true // the exchange will survive server restarts |
| 35 | + auto_delete: false //the exchange won't be deleted once the channel is closed. |
| 36 | +*/ |
| 37 | + |
| 38 | +$channel->exchange_declare($exchange, 'direct', false, true, false); |
| 39 | + |
| 40 | +$channel->queue_bind($queue, $exchange); |
| 41 | + |
| 42 | +/** |
| 43 | + * @param \PhpAmqpLib\Message\AMQPMessage $message |
| 44 | + */ |
| 45 | +function process_message($message) |
| 46 | +{ |
| 47 | + echo "\n--------\n"; |
| 48 | + echo $message->body; |
| 49 | + echo "\n--------\n"; |
| 50 | + |
| 51 | + $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']); |
| 52 | + |
| 53 | + // Send a message with the string "quit" to cancel the consumer. |
| 54 | + if ($message->body === 'quit') { |
| 55 | + $message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | + queue: Queue from where to get the messages |
| 61 | + consumer_tag: Consumer identifier |
| 62 | + no_local: Don't receive messages published by this consumer. |
| 63 | + no_ack: Tells the server if the consumer will acknowledge the messages. |
| 64 | + exclusive: Request exclusive consumer access, meaning only this consumer can access the queue |
| 65 | + nowait: |
| 66 | + callback: A PHP Callback |
| 67 | +*/ |
| 68 | + |
| 69 | +$channel->basic_consume($queue, $consumerTag, false, false, false, false, 'process_message'); |
| 70 | + |
| 71 | +/** |
| 72 | + * @param \PhpAmqpLib\Channel\AMQPChannel $channel |
| 73 | + * @param \PhpAmqpLib\Connection\AbstractConnection $connection |
| 74 | + */ |
| 75 | +function shutdown($channel, $connection) |
| 76 | +{ |
| 77 | + $channel->close(); |
| 78 | + $connection->close(); |
| 79 | +} |
| 80 | + |
| 81 | +register_shutdown_function('shutdown', $channel, $connection); |
| 82 | + |
| 83 | +// Loop as long as the channel has callbacks registered |
| 84 | +while (count($channel->callbacks)) { |
| 85 | + $channel->wait(); |
| 86 | +} |
0 commit comments