Skip to content

Instantly share code, notes, and snippets.

@jcnevado
Last active December 19, 2015 09:59
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 jcnevado/5936958 to your computer and use it in GitHub Desktop.
Save jcnevado/5936958 to your computer and use it in GitHub Desktop.
<?php
//Worker.php
/* Create a connection using all default credentials: */
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
/* create a queue object */
$queue = new AMQPQueue($channel);
$queue->setName('elements');
//declare the queue
$queue->declare();
$ex = new AMQPExchange($channel);
//declare the queue
$ex->setName('md');
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->declare();
$queue->bind('md', 'md:send');
$i = 0;
function processMessage($envelope, $queue) {
global $i;
echo "Message $i: " . print_r(json_decode($envelope->getBody()), true) . "\n";
$i++;
if ($i > 10) {
// Bail after 10 messages
return false;
}
}
// Consume messages on queue
$queue->consume("processMessage");
//Send.php
/* Create a connection using all default credentials: */
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
/* create a queue object */
$ex = new AMQPExchange($channel);
//declare the queue
$ex->setName('md');
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->declare();
$ex->publish(json_encode(array('Julio')), 'md:send');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment