Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fanout Exchange #9

Closed
ouziel-slama opened this issue Nov 24, 2015 · 4 comments
Closed

Fanout Exchange #9

ouziel-slama opened this issue Nov 24, 2015 · 4 comments

Comments

@ouziel-slama
Copy link

Hello,

I'am trying to set up a fanout exchange to deliver a message to multiple consumers. I didn't find any example for that. I tried to write something similar to the pika example here https://www.rabbitmq.com/tutorials/tutorial-three-python.html. The code doesn't raise any error, but only the first consumer receives message. Here the relevant code:

Publisher:

self.connection = amqpstorm.Connection(self.host, self.username, self.password)
self.channel = self.connection.channel()
self.channel.exchange.declare(exchange=self.exchange, exchange_type='fanout')
self.channel.queue.declare(self.queue, durable=True)
self.channel.queue.bind(self.queue, exchange=self.exchange)
self.channel.confirm_deliveries()
self.channel.basic.publish(body=body, routing_key=self.queue, mandatory=True, exchange=self.exchange)

Consumer:

self.connection = amqpstorm.Connection(self.host, self.username, self.password)
self.channel = self.connection.channel()
self.channel.exchange.declare(exchange=self.exchange, exchange_type='fanout')
self.channel.queue.declare(self.queue, durable=True)
self.channel.queue.bind(self.queue, exchange=self.exchange)
self.channel.confirm_deliveries()
self.channel.basic.consume(self.queue, no_ack=False)

Is the correct code to set up a fanout exchange ? I tried with no_ack=False and no_ack=True, and different variants of the code above, but I always get the same thing: no error, but only the first consumer receives the message.

Thank you in advance for your help.

@eandersson
Copy link
Owner

Hey,

If you have multiple consumers consuming from the same queue, only one of them will get the message. You will need to use different queues for each consumer, as an example in the pika example this is done using channel.queue_declare(exclusive=True), but you could simple use different names for the various consumers; e.g. queue1 and queue2.

In amqpstorm you would implement an exclusive queue like this.

result = self.channel.queue.declare(durable=True, exclusive=True)
self.queue = result['queue']

I hope this helps. =]

@ouziel-slama
Copy link
Author

Hey,

Ah yeah, got it! Thank you very much for your answer, will try that now!

Best

@eandersson
Copy link
Owner

No worries. Let me know if you run in to any issues!

@ouziel-slama
Copy link
Author

Works very well thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants