[rabbitmq-discuss] Fanout exchange doesnt publish to all consumers
Rader
rader.net at gmail.com
Tue Jun 26 08:53:09 BST 2012
Hi Max,
The problem is that you have only *one* queue.
"Fanout" not telling an exchange to distribute messages to different
consumers, but to different queues. So, you need at least two queues
binding to a "fanout" exchange. Then let your two consumers get message
from those two queues, one consumer to one queue.
On Sunday, June 17, 2012 4:27:15 PM UTC+8, Max Beutel wrote:
>
> 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?
>
>
On Sunday, June 17, 2012 4:27:15 PM UTC+8, Max Beutel wrote:
>
> 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?
>
>
On Sunday, June 17, 2012 4:27:15 PM UTC+8, Max Beutel wrote:
>
> 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?
>
>
On Sunday, June 17, 2012 4:27:15 PM UTC+8, Max Beutel wrote:
>
> 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/20120626/fc2cf855/attachment.htm>
More information about the rabbitmq-discuss
mailing list