Skip to content

Commit

Permalink
sample code for ch12
Browse files Browse the repository at this point in the history
  • Loading branch information
videlalvaro committed Oct 30, 2011
1 parent 3b2c79c commit 1051ed6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
58 changes: 58 additions & 0 deletions php/chapter-12/recent_history_consumer.php
@@ -0,0 +1,58 @@
<?php
###############################################
# RabbitMQ in Action
#
# Requires: php-amqplib
#
# Author: Alvaro Videla
# (C) 2011
###############################################
require_once('../lib/php-amqplib/amqp.inc');

define('HOST', 'localhost');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');

$exchange = 'rh-exchange';

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();

$ch->exchange_declare($exchange,
'x-recent-history',
false,
true,
false);

list($queue,,) = $ch->queue_declare('');

$ch->queue_bind($queue, $exchange);

$consumer = function($msg){
echo $msg->body, "\t";
};

$ch->basic_consume(
$queue,
'',
false,
true,
false,
false,
$consumer);

echo "consuming from queue: ", $queue, "\n";

function shutdown($conn, $ch){
$ch->close();
$conn->close();
}

register_shutdown_function('shutdown', $conn, $ch);

while(count($ch->callbacks)) {
$ch->wait();
}
?>
30 changes: 30 additions & 0 deletions php/chapter-12/recent_history_producer.php
@@ -0,0 +1,30 @@
<?php
###############################################
# RabbitMQ in Action
#
# Requires: php-amqplib
#
# Author: Alvaro Videla
# (C) 2011
###############################################
require_once('../lib/php-amqplib/amqp.inc');

define('HOST', 'localhost');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);

$channel = $conn->channel();

for($i=0; $i<100; $i++) {
$msg = new AMQPMessage('msg_'.$i,
array('content_type' => 'text/plain'));
$channel->basic_publish($msg, 'rh-exchange');
}

$channel->close();
$conn->close();
?>

0 comments on commit 1051ed6

Please sign in to comment.