RabbitMQ (Python implementation) learning two: Producer sending messages to multiple message queues queue (broadcast messages)

Source: Internet
Author: User
Tags message queue rabbitmq amq

1.1 Introduction to the contents of this section

This part we are going to send a message to multiple consumer, which is called "Publish/subscribe"

The way we do this is the sending side, sending a message, and at the same time, multiple receivers will receive the message and print it on the screen at the same time.

1.2exchange Introduction

In the previous blog post, we explained that the sender sends a message to the message queue and the receiving side obtains the message from the message queue. Now let's introduce the complete messaging model for RABBITMQ.

>producer: The application used to send messages

>queue: The cache used to store messages

>consumer: The application used to receive messages

The core of the messaging model is that producer never sends a message directly to the queue, but rather, it is a simple thing to pass the message to Exchange,exchange, on one side, he receives the message from producer and the other side transmits the message to the queue. Exchange sends a message to your queue, or to more than one queue, which is primarily determined by the type of exchange. The model diagram is as follows:

        

There are many type of exchange available, as follows: Direct, topic, headers, fanout. This blog for fanout explanation, follow-up blog for other types of explanation, let us create a exchange,type for fanout, named logs, the code is as follows:

Channel.exchange (exchange='logs', type='fanout')

For exchange of type fanout, it is very simple to understand that it will receive the message and broadcast it to all of the queues he knows, that is, all the queues that he has established to connect to. The previous post dropped to the command line to view the List_exchanges command as follows:

$: sudo rabbitmqctl list_exchanges Listing Exchanges ... logs      fanoutamq.direct      Directamq.topic       topicamq.fanout fanout       amq.headers     headers ... done.

For, you'll see a lot of amq.* exchange, which is set up by default, and when you don't build Exchange, the system defaults to the top few.

The Publish function for the message basic_publish () also changes to the following:

Channel.basic_publish (exchange='logs', routing_key=', Body=message)

1.3 Temporary queue

As you learned before, for a queue, you will have your own name (hello or something),

First of all:

result = Channel.queue_declare ()

Then, by Result.method.queue, the system will give the queue a random name.

If we want producer to disconnect from consumer, the queue is deleted, then it needs to be changed to the following code:

result = Channel.queue_declare (exclusive=true)

1.4Bingings (Bind queue to Exchange)

The model diagram is as follows:

        

We've created an exchange of type fanout, and now we're going to tell Exchange to send the message to our own defined queue, which is the binding for the connection between Exchange and queue, the code is as follows:

Channel.queue_bind (exchange='logs', Queue=result.method.queue)

At the command line, view the list of binding commands as follows:

$: sudo rabbitmqctl list_bindings

1.5 Final Code

The final model is as follows:

The send.py code is as follows:

ImportPikaImportsysconnection=Pika. Blockingconnection (Pika. Connectionparameters (Host='localhost')) Channel=Connection.channel () channel.exchange_declare (Exchange='logs', type='fanout') Message=' '. Join (sys.argv[1:])or "Info:hello world!"  #If the keyboard has input, the message is keyboard input, if the keyboard is not entered, messages message= "Info:hello world!" ;Channel.basic_publish (exchange='logs', routing_key="', Body=message)Print "[x] Sent%r"% (message,) Connection.close ()

receive.py Code

Importpikaconnection=Pika. Blockingconnection (Pika. Connectionparameters (Host='localhost')) Channel=Connection.channel () channel.exchange_declare (Exchange='logs', type='fanout') Result= Channel.queue_declare (exclusive=True) queue_name=Result.method.queuechannel.queue_bind (Exchange='logs', Queue=queue_name)Print '[*] waiting for logs. To exit Press CTRL + C'defCallback (ch, method, properties, body):Print "[x]%r"%(body,) Channel.basic_consume (callback, queue=queue_name, No_ack=True) channel.start_consuming ()

1.6 Code Testing

Open a command-line window and run send.py:

$: Python send.py   #(at this point you are transmitting the content Info:hello world! ) or  #message for the content you want to send

Open two command-line windows, run receive.py, two windows you will see the same message output:

$: Python receive.py

RabbitMQ (Python implementation) learning two: Producer sending messages to multiple message queues queue (broadcast messages)

Related Article

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.