RabbitMQ with AMQP PHP

RabbitMQ AMQP-PHP library build on Ubuntu 13.10 memo.

# environment
# cat /etc/lsb-release
# DISTRIB_ID=Ubuntu
# DISTRIB_RELEASE=13.10
# DISTRIB_CODENAME=saucy
# DISTRIB_DESCRIPTION=”Ubuntu 13.10″

# build rabbitmq-c
git clone https://github.com/alanxz/rabbitmq-c.git
cd rabbitmq-c/
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq-c/ ..
cmake –build .
make install

# build AMQP PHP
wget http://pecl.php.net/get/amqp-1.4.0.tgz
tar -zxvf amqp-1.4.0.tgz
cd amqp-1.4.0/
phpize
./configure –with-librabbitmq-dir=/usr/local/rabbitmq-c –with-amqp

# modify Makefile
AMQP_SHARED_LIBADD = -Wl,-rpath,/usr/local/rabbitmq-c/lib -L/usr/local/rabbitmq-c/lib -lrabbitmq /usr/local/rabbitmq-c/lib/x86_64-linux-gnu/
to
AMQP_SHARED_LIBADD = -Wl,-rpath,/usr/local/rabbitmq-c/lib -L/usr/local/rabbitmq-c/lib -lrabbitmq -L/usr/local/rabbitmq-c/lib/x86_64-linux-gnu/

make
make install
cat <<EOF > /etc/ld.so.conf.d/rabbitmq-c.conf
/usr/local/rabbitmq-c/lib/x86_64-linux-gnu/
EOF
ldconfig

# add apache, php.ini
cat <<EOF > /etc/php5/mods-available/amqp.ini
extension=amqp.so
EOF
ln -s /etc/php5/mods-available/amqp.ini /etc/php5/apache2/conf.d/20-amqp.ini
service apache2 restart

sender.php

<?php
$cnn = new AMQPConnection();
$cnn->connect();
$ch = new AMQPChannel($cnn);

// Declare a new exchange
$ex = new AMQPExchange($ch);
$ex->setName('exchange-test');
$ex->setType(AMQP_EX_TYPE_FANOUT);
$ex->declareExchange();

// Create a new queue
$q = new AMQPQueue($ch);
$q->setName('queue-test');
$q->setFlags(AMQP_NOPARAM);
$q->declareQueue();
$q->bind($ex->getName());

for ($i = 0; $i < 10000; $i++) {
    $data = array('id' => $i, 'time' => time());
    $ex->publish(serialize($data));
}
$cnn->disconnect();

?>

reciever.php

<?php

ini_set('amqp.auto_ack', false);

$cnn = new AMQPConnection();
$cnn->connect();
$ch = new AMQPChannel($cnn);
$ch->setPrefetchCount(1);

// Declare a new exchange
$ex = new AMQPExchange($ch);
$ex->setName('exchange-test');
$ex->setType(AMQP_EX_TYPE_FANOUT);
$ex->declareExchange();

// Create a new queue
$q = new AMQPQueue($ch);
$q->setName('queue-test');
$q->setFlags(AMQP_NOPARAM);
$q->declareQueue();
$q->bind($ex->getName());

$q->consume(
    function (AMQPEnvelope $msg, AMQPQueue $queue) use ($ex) {
        echo $msg->getBody(), PHP_EOL;
        return $queue->ack($msg->getDeliveryTag());
});
?>

rabbitmqctl list_queues
Listing queues …
queue-test 10764
…done.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください