|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + @author Chimdi Azubuike <me@chimdi.com> |
| 5 | +*/ |
| 6 | + |
| 7 | +//Establish connection AMQP |
| 8 | +$connection = new AMQPConnection(); |
| 9 | +$connection->setHost('127.0.0.1'); |
| 10 | +$connection->setLogin('guest'); |
| 11 | +$connection->setPassword('guest'); |
| 12 | +$connection->connect(); |
| 13 | + |
| 14 | +//Create and declare channel |
| 15 | +$channel = new AMQPChannel($connection); |
| 16 | + |
| 17 | +//AMQPC Exchange is the publishing mechanism |
| 18 | +$exchange = new AMQPExchange($channel); |
| 19 | + |
| 20 | +$routing_key = 'hello'; |
| 21 | +$queue = new AMQPQueue($channel); |
| 22 | +$queue->setName($routing_key); |
| 23 | +$queue->setFlags(AMQP_NOPARAM); |
| 24 | +$queue->declareQueue(); |
| 25 | + |
| 26 | + |
| 27 | +echo ' [*] Waiting for messages. To exit press CTRL+C ', PHP_EOL; |
| 28 | + |
| 29 | +$callback_func = function(AMQPEnvelope $message, AMQPQueue $q) use (&$max_consume) { |
| 30 | + echo PHP_EOL, "------------", PHP_EOL; |
| 31 | + echo " [x] Received ", $message->getBody(), PHP_EOL; |
| 32 | + echo PHP_EOL, "------------", PHP_EOL; |
| 33 | + |
| 34 | + $q->nack($message->getDeliveryTag()); |
| 35 | + sleep(1); |
| 36 | +}; |
| 37 | + |
| 38 | +try{ |
| 39 | + $queue->consume($callback_func); |
| 40 | +}catch(AMQPQueueException $ex){ |
| 41 | + print_r($ex); |
| 42 | +}catch(Exception $ex){ |
| 43 | + print_r($ex); |
| 44 | +} |
| 45 | + |
| 46 | +echo 'Close connection...', PHP_EOL; |
| 47 | +$queue->cancel(); |
| 48 | +$connection->disconnect(); |
| 49 | + |
0 commit comments