8000 Actually, publishing to a non-existing exchange is fatal. · bsumgit/rabbitmq-tutorials@37e2c49 · GitHub
[go: up one dir, main page]

Skip to content 10000

Commit 37e2c49

Browse files
committed
Actually, publishing to a non-existing exchange is fatal.
1 parent ffbb469 commit 37e2c49

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

python/emit_log.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
credentials=pika.PlainCredentials('guest', 'guest')))
88
channel = connection.channel()
99

10+
channel.exchange_declare(exchange='logs',
11+
type='fanout')
12+
1013
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
1114
channel.basic_publish(exchange='logs',
1215
routing_key='',

python/tutorial-three.mdx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ its value is ignored for `fanout` exchanges. Here goes the code for
242242
credentials=pika.PlainCredentials('guest', 'guest')))
243243
channel = connection.channel()
244244

245+
channel.exchange_declare(exchange='logs',
246+
type='fanout')
247+
245248
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
246249
channel.basic_publish(exchange='logs',
247250
routing_key='',
@@ -250,10 +253,13 @@ its value is ignored for `fanout` exchanges. Here goes the code for
250253
{% endhighlight %}
251254
[(emit_log.py source)]({{ examples_url }}/python/emit_log.py)
252255

253-
As you see, we avoided declaring exchange here. If the `logs` exchange
254-
isn't created at the time this code is executed the message will be
255-
lost. That's okay for us - if no consumer is listening yet (ie:
256-
the exchange hasn't been created) we can discard the message.
256+
As you see, after establishing the connection we declared the
257+
exchange. This step is neccesary as publishing to a non-existing
258+
exchange is forbidden.
259+
260+
The messages will be lost if no queue is bound to the exchange yet,
261+
but that's okay for us; if no consumer is listening yet (ie: the
262+
exchange hasn't been created) we can safely discard the message.
257263

258264
The code for `receive_logs.py`:
259265

0 commit comments

Comments
 (0)
0