note 105947 deleted from class.amqpqueue by sobak
| From: | sobak@php.net | Date: | Thu, 24 Nov 2016 20:34:42 +0000 |
| Subject: | note 105947 deleted from class.amqpqueue by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207589@lists.php.net to get a copy of this message | ||
Note Submitter: bolo at nospam dot autistici dot org
----
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