import os
import pika
# Retrieve RabbitMQ URI from environment variables
rabbitmq_uri = os.getenv("RABBITMQ_URI")
# Create a connection to RabbitMQ
params = pika.URLParameters(rabbitmq_uri)
connection = pika.BlockingConnection(params)
# Create a channel
channel = connection.channel()
# Declare a queue (if it doesn’t exist, it will be created)
queue_name = "test_queue"
channel.queue_declare(queue=queue_name)
# Function to publish a message to the queue
def publish_message(message):
channel.basic_publish(exchange="", routing_key=queue_name, body=message)
print(f"Message sent: {message}")
# Example usage
publish_message("Hello, This is RabbitMQ on Liara!")
# Close the connection
connection.close()