@@ -17,14 +17,16 @@ digraph G {
17
17
18
18
Throughout this tutorial, we'll teach you the basic concepts required for
19
19
creating RabbitMQ applications. The tutorial will be illustrated with
20
- code snippets written in [ Python] ( http://www.python.org/ ) . But don't worry if
21
- you don't know this language - the core ideas are the same for other languages.
20
+ code snippets written in [ Python] ( http://www.python.org/ ) and executed on Linux.
21
+ But don't worry if you don't know this language - the core ideas are the same
22
+ for other languages.
23
+
22
24
23
25
This tutorial consists of three parts:
24
26
25
27
* First we're going to write the simplest possible "Hello World" example.
26
- * Next we'll try to use Rabbit as a simple "Work queue" server.
27
- * Finally, we'll discuss the "Publish-subscribe" pattern .
28
+ * Next we'll try to use Rabbit as [ a simple "Work queue" server] ( { { python_two_url } } ) .
29
+ * Finally, we'll discuss how to [ broadcast a message ] ( { { python_three_url } } ) .
28
30
29
31
You need to have RabbitMQ server installed to go through this tutorial.
30
32
If you haven't installed it yet you can follow the
@@ -49,15 +51,16 @@ If you have installed RabbitMQ you should see something like:
49
51
> #### Where to get help
50
52
>
51
53
> If you're having trouble going through this tutorial you can post a message to
52
- > [rabbitmq - discuss mailing list ](https : // lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss).
54
+ > [rabbitmq - discuss mailing list ](https : // lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss)
55
+ > or join the [#rabbitmq ](irc : // irc.freenode.net/rabbitmq) irc channel.
53
56
54
57
55
58
Introduction
56
59
------------
57
60
58
61
RabbitMQ is a message broker . The principle idea is pretty simple : it accepts
59
62
and forwards messages . You can think about it as a post office : when you
E7F5
send
60
- mail to the post box and you ' re pretty sure that mr postman will eventually
63
+ mail to the post box and you ' re pretty sure that Mr. Postman will eventually
61
64
deliver the mail to your recipient . Using this metaphor RabbitMQ is a post box ,
62
65
post office and a postman.
63
66
@@ -68,7 +71,7 @@ blobs of data - _messages_.
68
71
RabbitMQ uses a weird jargon , but it ' s simple once you' ll get it. For example :
69
72
70
73
* _Producing_ means nothing more than sending . A program that sends messages
71
- is a _producer_ .
74
+ is a _producer_ . We ' ll draw it like that, with "P" :
72
75
73
76
{% dot -Gsize= " 10,0.3" - Grankdir = LR % }
74
77
digraph G {
@@ -82,7 +85,8 @@ RabbitMQ uses a weird jargon, but it's simple once you'll get it. For example:
82
85
is not bound by any limits , it can store how many messages you
83
86
like - it ' s essentially an infinite buffer. Many _producers_ can send
84
87
messages that go to the one queue , many _consumers_ can try to
85
- receive data from one _queue_.
88
+ receive data from one _queue_. Queue ' ll be drawn as like that, with
89
+ its name above it :
86
90
87
91
{% dot -Gsize= " 10,0.9" - Grankdir = LR % }
88
92
digraph G {
@@ -95,7 +99,7 @@ RabbitMQ uses a weird jargon, but it's simple once you'll get it. For example:
95
99
{% enddot %}
96
100
97
101
* _Consuming_ has a simmilar meaning to receiving . _Consumer_ is a program
98
- that mostly waits to receive messages.
102
+ that mostly waits to receive messages . On our drawings it ' s shown with "C" :
99
103
100
104
{% dot -Gsize= " 10,0.3" - Grankdir = LR % }
101
105
digraph G {
@@ -127,6 +131,9 @@ digraph G {
127
131
}
128
132
{% enddot %}
129
133
134
+ Producer sends messages to the " test" queue . The consumer receives
135
+ messages from that queue .
136
+
130
137
> #### RabbitMQ libraries
131
138
>
132
139
> RabbitMQ speaks AMQP protocol . To use Rabbit you ' ll need a library that
@@ -171,7 +178,8 @@ digraph G {
171
178
172
179
173
180
Our first program ` send.py ` will send a single message to the queue .
174
- The first thing we need to do is connect to RabbitMQ server .
181
+ The first thing we need to do is to establish a connection with
182
+ RabbitMQ server .
175
183
176
184
{% highlight python %}
177
185
#!/usr /bin /env python
@@ -183,28 +191,27 @@ The first thing we need to do is connect to RabbitMQ server.
183
191
channel = connection .channel ()
184
192
{% endhighlight %}
185
193
186
-
187
- Whenever we send a message we need to make sure the recipient queue exists .
188
- RabbitMQ will just trash the message if can ' t deliver it. So, we need to
189
- create a queue to which the message will be delivered . Let ' s name this queue
190
- _test_ :
194
+ We ' re connected now. Next, before sending we need to make sure the
195
+ recipient queue exists . If we send a message to non - existing location ,
196
+ RabbitMQ will just trash the message . Let ' s create a queue to which
197
+ the message will be delivered , let ' s name it _test_:
191
198
192
199
{% highlight python %}
193
200
channel .queue_declare (queue = ' test' )
194
201
{% endhighlight %}
195
202
196
203
197
204
At that point we ' re ready to send a message. Our first message will
198
- contain a string _Hello World ! _ and we want to send it to our _test_
199
- queue .
205
+ just contain a string _Hello World ! _ , we want to send it to our
206
+ _test_ queue .
200
207
201
- In RabbitMQ a message never goes directly to the queue , it always
208
+ In RabbitMQ a message never can be send directly to the queue , it always
202
209
needs to go through an _exchange_ . But let ' s not get dragged by the
203
- details - you can read more about _exchanges_ in third part of this
204
- tutorial . All we need to know now is how to use a default exchange
205
- identified by an empty string . That exchange is a special one that
210
+ details - you can read more about _exchanges_ in [ third part of this
211
+ tutorial ]({{ python_three_url }}) . All we need to know now is how to use a default exchange
212
+ identified by an empty string . This exchange is special - it
206
213
allows us to specify exactly to which queue the message should go .
207
- The queue name is specified by the ` routing_key ` variable :
214
+ The queue name needs to be specified in the ` routing_key ` parameter :
208
215
209
216
{% highlight python %}
210
217
channel .basic_publish (exchange = ' ' ,
@@ -236,28 +243,27 @@ digraph G {
236
243
Our second program `receive.py` will receive messages from the queue and print
237
244
them on the screen.
238
245
239
- The code responsible for connecting to Rabbit is the same as the previous example.
240
- You can copy the first 7 lines .
246
+ Again, first we need to connect to RabbitMQ server. The code
247
+ responsible for connecting to Rabbit is the same as previously .
241
248
242
- # ... connection code is the same, copy first 7 lines from send.py ...
243
-
244
- Just like before, in the beginning we must make sure that the
245
- queue exists. Creating a queue using `queue_declare` is idempotent - you can
246
- run the command as many times you like, and only one queue will be created.
249
+ Next step, just like before, is to make sure that the
250
+ queue exists. Creating a queue using `queue_declare` is idempotent - we can
251
+ run the command as many times you like, and only one will be created.
247
252
248
253
{% highlight python %}
249
254
channel.queue_declare(queue='test')
250
255
{% endhighlight %}
251
256
252
- You may ask why to declare queue again - we have already declared it
253
- in our previous code. We could have avoided that if we always run the
254
- `send.py` program before this one. But we're not sure yet which
257
+ You may ask why to declare the queue again - we have already declared it
258
+ in our previous code. We could have avoided that if we were sure
259
+ that the queue already exists. For example if `send.py` program was
260
+ runned before. But we're not yet sure which
255
261
program to run as first. In such case it's a good practice to repeat
256
262
declaring the queue in both programs.
257
263
258
264
> #### Listing queues
259
265
>
260
- > Sometimes you may want to see what queues does RabbitMQ store and how many
266
+ > You may want to see what queues does RabbitMQ store and how many
261
267
> messages are in them. You can do it using the `rabbitmqctl` tool:
262
268
>
263
269
> $ sudo rabbitmqctl list_queues
@@ -267,18 +273,20 @@ declaring the queue in both programs.
267
273
268
274
269
275
270
- Receiving messages from the queue is a bit more complex . Whenever we receive
271
- a message , a ` callback ` function is called. In our case
272
- this function will print on the screen the contents of the message.
276
+ Receiving messages from the queue is more complex . It works by subscribing
277
+ a ` callback ` function to a queue. Whenever we receive
278
+ a message, this `callback` function is called by the Pika library.
279
+ In our case this function will print on the screen the contents of
280
+ the message.
273
281
274
282
{% highlight python % }
275
283
def callback (ch , method , header , body ):
276
284
print " [x] Received %.20r" % (body ,)
277
285
{% endhighlight %}
278
286
279
287
280
- Next , we need to tell RabbitMQ that this particular callback function is
281
- interested in messages from our _test_ queue:
288
+ Next , we need to tell RabbitMQ that this particular callback function should
289
+ receive messages from our _test_ queue:
282
290
283
291
{% highlight python %}
284
292
channel .basic_consume (callback ,
@@ -290,6 +298,8 @@ For that command to succeed we must be sure that a queue which we want
290
298
to subscribe to exists . Fortunately we ' re confident about that - we' ve
291
299
created a queue above - using ` queue_declare ` .
292
300
301
+ The ` no_ack ` parameter will be described [later on ]({{ python_two_url }}).
302
+
293
303
And finally , we enter a never-ending loop that waits for data and runs callbacks
294
304
whenever necessary.
295
305
@@ -320,7 +330,7 @@ Full code for `send.py`:
320
330
body = ' Hello World!' )
321
331
print " [x] Sent 'Hello World!'"
322
332
{% endhighlight %}
323
- [(full send .py source )]({{ examples_url }}/ python / send .py )
333
+ [(send .py source )]({{ examples_url }}/ python / send .py )
324
334
325
335
326
336
Full ` receive.py ` code :
@@ -347,8 +357,7 @@ Full `receive.py` code:
347
357
348
358
pika .asyncore_loop ()
349
359
{% endhighlight %}
350
-
351
- [(full receive .py source )]({{ examples_url }}/ python / receive .py )
360
+ [(receive .py source )]({{ examples_url }}/ python / receive .py )
352
361
353
362
354
363
Now we can try out our programs . First , let' s send a message using our
@@ -364,9 +373,9 @@ Let's receive it:
364
373
[x ] Received ' Hello World!'
365
374
366
375
Hurray ! We were able to send our first message through RabbitMQ . As you might
367
- have noticed , the ` receive.py ` program didn ' t exit. It will stay ready to
368
- receive further messages. Try to run ` send.py ` in a new terminal!
376
+ have noticed , the ` receive.py ` program doesn ' t exit. It will stay ready to
377
+ receive further messages. Try to r
5458
un ` send.py ` again in a new terminal!
369
378
370
379
We' ve learned how to send and receive a message from a named
371
- queue. It' s time to move on to part 2 of this tutorial and build a
372
- simple _task queue_.
380
+ queue. It' s time to move on to [ part 2]({{ python_two_url }} )
381
+ and build a simple _task queue_.
0 commit comments