You can download this code by clicking the button below.
This code is now available for download.
This function connects to a RabbitMQ server, declares a queue, and sets a callback function to handle received messages. When a message arrives, it prints the content of the message.
Technology Stack : RabbitMQ, pika
Code Type : Python Function
Code Difficulty : Intermediate
import random
import pika
def random_rmq_function(queue_name):
# Connect to the RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare a queue
channel.queue_declare(queue=queue_name)
def callback(ch, method, properties, body):
print(f"Received {body}")
# Consume messages from the queue
channel.basic_consume(queue=queue_name, on_message_callback=callback)
# Start consuming
print("Waiting for messages. To exit press CTRL+C")
channel.start_consuming()
# Close the connection
connection.close()