Function that perform a php publisher daemon. [And function to interface with Rabbit server.]
<?php
//First initialise the connection to Rabbit server
function amqp_connection() {
$connection = new AMQPConnection();
$connection->setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
if (!$connection->isConnected()) {
echo "Cannot connect to the broker";
}
return $connection;
}
//This is the daemon, it should be more performant because the sleep(1) run the publisher every 1 second. Rabbit is more faster.
function receiver($exchange, $rk, $queuename) {
$connection=amqp_connection();
$queue = new AMQPQueue($connection);
$queue->declare($queuename);
$queue->bind($exchange, $rk);
while(true){
$msg=$queue->get();
if ($msg['count']>-1){
echo "\n--------\n";
print_r($msg['msg']);
echo "\n--------\n";
}
sleep(1);
}
if (!$connection->disconnect()) {
throw new Exception('Could not disconnect');
}
}
//Next perform message delivery, the FANOUT type is for loadbalacing mode
//It perform the delivery of $text message to every Rabbit servers running in your system.
//Refer to the php manual (http://www.php.net/manual/en/amqp.constants.php) for others type.
function sender($text, $rk, $exchange){
$connection=amqp_connection();
$ex = new AMQPExchange($connection);
$ex->declare($exchange, AMQP_EX_TYPE_FANOUT, AMQP_DURABLE);
$msg=$ex->publish($text, $rk);
if (!$msg){echo "error";}echo 'Sended '.$msg.'<br>';
if (!$connection->disconnect()) {
throw new Exception('Could not disconnect');
} else {
echo "disconnected";
}
}
?>
P.S. run the daemon with ~$ php /pathto/php.ini functioncaller.php
AMQPQueue クラス
(バージョン情報なし。おそらく SVN 版にしか存在しないでしょう)
導入
AMQP キューをあらわします。
クラス概要
AMQPQueue
{
/* メソッド */
}目次
- AMQPQueue::ack — メッセージの受信を受け付ける
- AMQPQueue::bind — 指定したキューを、exchange 上のルーティングキーにバインドする
- AMQPQueue::cancel — キューのバインドをキャンセルする
- AMQPQueue::__construct — AMQPQueue オブジェクトのインスタンスを作成する
- AMQPQueue::consume — メッセージをキューから取得する
- AMQPQueue::declare — 新しいキューを宣言する
- AMQPQueue::delete — キューとその中身を削除する
- AMQPQueue::get — 次のメッセージをキューから取得する
- AMQPQueue::getArgument — 指定したキーに関連づけられた引数を取得する
- AMQPQueue::getArguments — 指定したキュー上で設定されたすべての引数を取得する
- AMQPQueue::getFlags — フラグのビットマスクを取得する
- AMQPQueue::getName — 設定された名前を取得する
- AMQPQueue::nack — メッセージを明示的に受付前の状態にする
- AMQPQueue::purge — キューの中身をパージする
- AMQPQueue::setArgument — 指定したキーの値を設定する
- AMQPQueue::setArguments — キュー上のすべての引数を設定する
- AMQPQueue::setFlags — キューのフラグを設定する
- AMQPQueue::setName — キューの名前を設定する
- AMQPQueue::unbind — キューをルーティングキーから削除する
bolo at nospam dot autistici dot org
27-Sep-2011 04:03
