RabbitMQ (Python implementation) learning three: Routing (receive side receive fixed type message)

Source: Internet
Author: User
Tags message queue rabbitmq

1.1 About this blog

The front drops to broadcast messages to multiple consumer, this blog post is primarily a fixed-point transfer of messages, and each consumer gets a fixed type of message from the message queue.

1.2Bindings

In the previous code, we wrote this code:

Channel.queue_bind (Exchange=exchange_name,queue=queue_name)

A binding is the link between Exchange and the queue.

The binding can be appended with an additional parameter routing_key to avoid basic_publish confusion. Here is the specific code:

Channel.queue_bind (exchange=exchange_name,queue=queue_name,routing_key='black')

1.3Direct Exchange (Direct-type Exchange)

As mentioned in the previous blog post, the message is broadcast to all consumers, and now we want to classify the message according to our own needs, and send different messages to different consumer.

Before we set the type of Exchange to Fanout, this type of flexibility is not enough to broadcast only without brains.

Here we set the type of exchange to direct, at which point the message arrives at Exchange and, depending on the Routing_key, sends the message to the appropriate queue, which is customized to receive the message. The specific model is as follows:

        

As we can see from the diagram, Exchange has a type of direct, with two queue. Q1 is connected to exchange through the binding of Routing_key=orange, while Q2 is connected to exchange through two Binding,routing_key=gren and Routing_key=black.

Messages for Routing_key=orange are sent to Q1, and messages for Routing_key=black and Routing_key=green are sent to Q2, and other routing_key messages are discarded.

1.4 Code Implementation

First create an exchange with the following code

Channel.exchange_declare (Exchange ='direct_logs', type='direct' )

Next we are going to send a message:

Channel.basic_publish (exchange='direct_logs', Routing_key=severity,body=message)

Severity later, the program will refer to the value of ' info ', ' warning ', ' ERROR '

To subscribe to messages, we create binging to receive messages:

result = Channel.queue_declare (exclusive=True) queue_name=result.method.queuefor in  severities:    channel.queue_bind (Exchange='direct_logs', queue= queue_name,routing_key=severity)

1.5 Complete Code implementation

The model is as follows:

        

send.py Code:

#!/usr/bin/env pythonImportPikaImportsysconnection=Pika. Blockingconnection (Pika. Connectionparameters (Host='localhost')) Channel=Connection.channel () channel.exchange_declare (Exchange='Direct_logs', type='Direct') Severity= Sys.argv[1]ifLen (SYS.ARGV) > 1Else 'Info'  #manually enter the Routing_key to send the messageMessage =' '. Join (sys.argv[2:])or 'Hello world!'channel.basic_publish (Exchange='Direct_logs', Routing_key=severity, Body=message)Print "[x] Sent%r:%r"%(severity, message) Connection.close ()

receive.py Code implementation:

#!/usr/bin/env pythonImportPikaImportsysconnection=Pika. Blockingconnection (Pika. Connectionparameters (Host='localhost')) Channel=Connection.channel () channel.exchange_declare (Exchange='Direct_logs', type='Direct') Result= Channel.queue_declare (exclusive=True) queue_name=result.method.queueseverities= Sys.argv[1:]if  notseverities:Print>> Sys.stderr,"Usage:%s [INFO] [WARNING] [ERROR]"%(Sys.argv[0],) sys.exit (1) forSeverityinchSeverities:channel.queue_bind (Exchange='Direct_logs', Queue=queue_name, Routing_key=severity)Print '[*] waiting for logs. To exit Press CTRL + C'defCallback (ch, method, properties, body):Print "[x]%r:%r"%(Method.routing_key, Body,) Channel.basic_consume (callback, queue=queue_name, No_ack=True) channel.start_consuming ()

RabbitMQ (Python implementation) learning three: Routing (receive side receive fixed type message)

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.