RABBITMQ Official Website Tutorial---Introduction

Source: Internet
Author: User

RABBITMQ is a message broker. The main idea is very simple: it receives and transmits messages. You can think of it as a post office: When you send an email to a mailbox, you're pretty sure the postman will eventually deliver the message to your recipient. Using this analogy RABBITMQ is a mailbox, a post office and a postman.


The main difference between RABBITMQ and the post office is that it does not process paper, but instead receives, stores, and transmits binary data-messages.


Generally speaking, RABBITMQ is the message in terms of terminology.


Producing means that there is no other thing to do except send. A program that sends a message is a producer. We will draw it with "P":

The queue is the name of a mailbox. It resides in the RABBITMQ. Although the message flows through RABBITMQ and your application, they can only be stored in a queue clock. The queue is not bound by any restrictions, it can store as much information as you like-it is equivalent to an unrestricted cache. Many producer can send messages to a queue clock, and many consumer can try to receive messages from the queue. The queue will be drawn as shown below:

Consuming is similar to the meaning of reception. Consumer is a program that waits a lot of times to receive messages. We use "C" to draw it:

Note that Producer,consumer and brokder do not need to reside on the same machine, but in most applications they are not on the same machine.


Hello world!

(using Pika 0.9.8 Python client)

Our "Hello world" is not too complicated-let's send a message, receive it and print it on the screen. In order to make such a program we need two programs: one to send a message and one to receive the message and print it.


Our design will be like this:

Producer sends a message to the "Hello" queue, consumer receives the message from that queue

RABBITMQ Library RABBITMQ described a protocol called AMQP. In order to use rabbit you will need a library that understands this protocol as well as Rabbit. Almost every programming language has an optional library like this. For Python is different and there are not many libraries to choose from: Py-amqplib TXAMQP Pika in this series of tutorials we will use Pika. You can install it using the PIP Package management tool: sudo pip install pika==0.9.8 installation relies on PIP and git-core packages, you may need to install them first. On Ubuntu: sudo apt-get install Python-pip git-core on Debian: sudo apt-get install python-setuptools git-coresudo easy_ Install PIP on Windows: Easy_install pippip Install pika==0.9.8


Send

Our first program, send.py, will send a message to the queue. The first thing we need to do is establish a connection with RABBITMQ:

#!/usr/bin/env pythonimport pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters ("localhost")) Channel = Connection.channel ()


We have now connected to a local broker-due to localhost. If we want to connect to a different broker machine then we need to simply specify its name or IP here.


Next, we need to make sure that the receive queue already exists before sending. If we send a message to a nonexistent address, RABBITMQ will discard the message. Let's create a queue that will be passed with the name "Hello":

Channel.queue_declare (queue= "Hello")


At that stage we were ready to send a message. Our first message will only contain a string Hello world! and we send it to our Hello queue.


In RABBITMQ a message may never be sent directly to the queue, it always needs to pass through an interchange. But we don't need to delve into it-you can read more interactive information in the third part of this tutorial. All we need to do is to use a swap ID with an empty string. This exchange is special-it allows us to specify exactly which queue the message should go into. The queue name needs to be specified in the Routing_key parameter:

Channel.basic_publish (exchange= ", routing_key=" Hello ", body=" Hello world! ") print "[x] Sent ' Hello world! '"

Before the program exits we need to be sure that the network cache has been refreshed and that our messages are actually passed to RABBITMQ. We can handle it through a smooth close connection.

Connection.close ()



Send does not take effect if this is the first time you are using RABBITMQ and you do not see "Sent" messages, then you may break your head and wonder what the possible errors are. Perhaps the broker is turned on when there is not enough disk space (which requires at least 1Gb of space by default) and therefore refuses to accept the message. Check the broker log file to be sure and, if necessary, reduce the limit. The configuration file documentation will show you how to set up Disk_free_limit.


Receive

Our second program is that receive.py will receive messages from the queue and print them on the screen.


Once again, we need to connect with the RABBITMQ server first. The connection code is the same as the previous one.


Next, just like before, make sure that the queue exists. Creating a queue using Queue_declare is idempotent-we only need to create this queue once to run this command many times.

Channel.queue_declare (queue= ' Hello ')

You might ask why we have to define a queue again-we've already defined it in the previous code. If we make sure that the queue already exists we can avoid creating it. For example, if the send.py program is running in front. But we're not sure which program to run first. It is a good practice to define a repeating queue in two programs like this example.

List queues you may want to see what queues are in the RABBITMQ and how many messages are in them. You can use the Rabbitmqctl tool like this: sudo rabbitmqctl list_queueslisting queues ... hello 0...done. Ignore sudo on Windows


It is complex to receive messages from the queue. It subscribes to the queue by subscribing to a callback function. Whenever we receive a message, this callback function is called through the Pika Library. In our example, this function will print the contents of the message on the screen.

DEF callback (ch, method, properties, body): print "[x] Received%r"% (body,)


Next, we need to tell RABBITMQ that this particular callback function should receive a message from our Hello queue:

Channel.basic_consume (callback, queue= ' Hello ', no_ack=true)


For a successful command we must make sure that we subscribe to a queue that already exists. Fortunately, we are confident about this-we have created a queue above-using Queue_declare.


The No_ack parameter is described later.


Finally, we enter a callback for the waiting data that is always polled.

Print "[*] waiting for messages. To exit Press CTRL + C "Channel.start_consuming ()


Put them together.

send.py all the code:

#!/usr/bin/env pythonimport pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost ')) channel = Connection.channel () channel.queue_declare (queue= ' Hello ') Channel.basic_publish (exchange= ", routing_key= ' hello ', body= ' Hello world! ') print "[x] Sent ' Hello world! '" Connection.close ()


receive.py Code:

#!/usr/bin/env pythonimport pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost ')) channel = Connection.channel () channel.queue_declare (queue= ' hello ') print "[ *] waiting for messages. To exit Press CTRL + C "DEF callback (ch, method, properties, body): print" [x] receieved%r "% (body,) channel.basic_cons Ume (callback, queue= ' Hello ', no_ack=true) channel.start_consuming ()


The execution of the following Python file is not translated, and I believe Python knows it or can read it.


RABBITMQ Official Website Tutorial---Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.