[rabbitmq-discuss] Fanout exchange doesnt publish to all consumers
Max Beutel
nash12 at gmail.com
Sun Jun 17 09:27:15 BST 2012
I am using the AMQP pecl extension (http://pecl.php.net/package/amqp) for
connecting to rabbitMQ from my PHP app. I have the following scripts:
producer.php:
$cnn = new AMQPConnection();
$cnn->connect();
$ch = new AMQPChannel($cnn);
$ex = new AMQPExchange($ch);
$ex->setName('ex2');
$ex->setType(AMQP_EX_TYPE_FANOUT);
$ex->declare();
$q = new AMQPQueue($ch);
$q->setName('test1243');
$q->setFlags(AMQP_DURABLE);
$q->declare();
$q->bind('ex2', 'routing.key');
for ($i = 0; $i < 100; $i++) {
$ex->publish('msg ' . ($i +1), 'routing.key');
}
The producer publishes 100 messages.
Then I have a consumer.php:
$cnn = new AMQPConnection();
$cnn->connect();
// Create a channel
$ch = new AMQPChannel($cnn);
$q = new AMQPQueue($ch);
$q->setName('test1243');
$q->setFlags(AMQP_DURABLE);
$q->declare();
$q->bind('ex2', 'routing.key');
$ex = new AMQPExchange($ch);
$ex->setName('ex2');
$ex->setType(AMQP_EX_TYPE_FANOUT);
$ex->declare();
function processMessage($envelope, $queue) {
echo "Message $i: " . $envelope->getBody() . "\n";
$queue->ack($envelope->getDeliveryTag());
}
$q->consume("processMessage");
The consumer just writes the messages to stdout and ack´s them.
I used the exchange type fanout, because I want each message from the
producer delivered to each consumer.
But if I start 2 consumers the output looks like this:
output consumer1:
Message : msg 1
Message : msg 3
Message : msg 5
...
output consumer2:
Message : msg 2
Message : msg 4
Message : msg 6
Message : msg 8
...
It looks like the messages are distributed using round robin or the like,
consumer 1 gets message 1, consumer 2 gets message 2 etc.
Is fanout the wrong exchange for this? Or is something wrong in my setup
code?
Defining the routing key is probably usesless as this is ignored in fanout
anway, right?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120617/f34ac7cf/attachment.htm>
More information about the rabbitmq-discuss
mailing list