Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of producer and Consumer at sender and receiver of RabbitMQ

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you the content of an example analysis of producers and consumers at the sender and receiver of RabbitMQ. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Rabbit_conn_send_producer.py

Import pika

Connection = pika.BlockingConnection (pika.ConnectionParameters (

'localhost')) # rabbit default port 5672 establishes a basic socket connection

Channel = connection.channel () # declares a pipe to send messages inside the pipe

# declare queue

Channel.queue_declare (queue='hello')

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.

Channel.basic_publish (exchange=''

Routing_key='hello',#queue name

Body='Hello wordings') # messages sent by body

Print ("[x] Sent 'Hello Worldwide'")

Connection.close ()

Rabbit_conn_recive_consumer.py

# _ * _ coding:utf-8_*_

_ _ author__ = 'Alex Li'

Import pika

Connection = pika.BlockingConnection (pika.ConnectionParameters (

'localhost')) # rabbit default port 5672 establishes a basic socket connection

Channel = connection.channel () # declares a pipeline to receive messages in the pipeline

# You may ask why we declare the queue again-we have already declared it in our previous code.

# We could avoid that if we were sure that the queue already exists. For example if send.py program

# was run before. But we're not yet sure which program to run first. In such cases it's a good

# practice to repeat declaring the queue in both programs.

# channel.queue_declare (queue='hello') # declare queue

Def callback (ch, method, properties, body): # processing messages

Print ("- >", ch,method,properties) # ch pipe memory object address method: information sent to queue

Print ("[x] Received r" body)

Channel.basic_consume (# consumption message

If callback,# receives a message, it calls the CALLBACK function to process the message.

Which queue does queue='hello',# receive messages from?

No_ack=True) # there is no need to confirm whether the message is received

Print ('[*] Waiting for messages. To exit press CTRL+C')

Channel.start_consuming () # starts to receive messages all the time, and there is no card owner

Rabbit_conn_recive_consumer_no_ack.py

# _ * _ coding:utf-8_*_

_ _ author__ = 'Alex Li'

Import pika,time

Connection = pika.BlockingConnection (pika.ConnectionParameters (

'localhost')) # rabbit default port 5672 establishes a basic socket connection

Channel = connection.channel () # declares a pipeline to receive messages in the pipeline

# You may ask why we declare the queue again-we have already declared it in our previous code.

# We could avoid that if we were sure that the queue already exists. For example if send.py program

# was run before. But we're not yet sure which program to run first. In such cases it's a good

# practice to repeat declaring the queue in both programs.

# channel.queue_declare (queue='hello') # declare queue

Def callback (ch, method, properties, body): # callback function

Print ("- >", ch,method,properties) # ch pipe memory object address method: information sent to queue

Time.sleep (5) # simulated message processing time

Print ("[x] Received r" body)

Channel.basic_consume (# consumption message

If callback,# receives a message, it calls the CALLBACK function to process the message.

Which queue does queue='hello',# receive messages from?

# no_ack=True) # if no acknowledgement does not confirm whether it is enabled, it will not send a message to the server if it does not confirm whether it is received or not. If the client does not receive the message, it will ignore it.

# if the server is closed, the server will confirm whether the message is received. If the message is not confirmed, the message will be retained all the time, and it will be automatically transferred to another client. As soon as the socket is cut off, the rabbitMQ will transfer the message to another client.

# the sender sends a message, which is received by the consumer receiver. After processing, the consumer receiver automatically sends a confirmation to the producer sender, saying that the message has been processed, and then the producer sender will delete the message from the queue. As long as it does not receive the acknowledgement, it will not delete it. If the producer sender does not receive the acknowledgement, it will forward the message to another consumer receiver.

)

Print ('[*] Waiting for messages. To exit press CTRL+C')

Channel.start_consuming () # starts to receive messages all the time, and there is no card owner

Thank you for reading! This is the end of the article on "sample analysis of producers and consumers at the sender and receiver of RabbitMQ". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report