Skip to content

Instantly share code, notes, and snippets.

@ludofleury
Created October 28, 2012 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludofleury/3968239 to your computer and use it in GitHub Desktop.
Save ludofleury/3968239 to your computer and use it in GitHub Desktop.
<?php
include(__DIR__ . '/config.php');
use PhpAmqpLib\Connection\AMQPConnection;
$exchange = 'router';
$queue = 'msgs';
$consumer_tag = 'consumer';
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
$ch->queue_declare($queue, false, true, false, false);
$ch->exchange_declare($exchange, 'direct', false, true, false);
$ch->queue_bind($queue, $exchange);
function process_message($msg)
{
$msg->delivery_info['channel']->
basic_nack($msg->delivery_info['delivery_tag']);
// Send a message with the string "quit" to cancel the consumer.
if ($msg->body === 'quit') {
$msg->delivery_info['channel']->
basic_cancel($msg->delivery_info['consumer_tag']);
}
}
$ch->basic_consume($queue, $consumer_tag, false, false, false, false, 'process_message');
function shutdown($ch, $conn)
{
$ch->close();
$conn->close();
}
register_shutdown_function('shutdown', $ch, $conn);
// Loop as long as the channel has callbacks registered
while (count($ch->callbacks)) {
$ch->wait();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment