32 lines
952 B
Python
32 lines
952 B
Python
import pika
|
|
import os
|
|
import time
|
|
|
|
# Environment variable for RabbitMQ connection
|
|
rabbitmq_host = os.getenv("RABBITMQ_HOST", "localhost")
|
|
|
|
def main():
|
|
# Establish a connection to RabbitMQ
|
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitmq_host))
|
|
channel = connection.channel()
|
|
|
|
# Declare a queue to ensure it exists
|
|
channel.queue_declare(queue='test_queue')
|
|
|
|
# Send a test message
|
|
message = "Hello, RabbitMQ!"
|
|
channel.basic_publish(exchange='', routing_key='test_queue', body=message)
|
|
print(f" [x] Sent '{message}'")
|
|
|
|
# Close the connection
|
|
connection.close()
|
|
|
|
if __name__ == "__main__":
|
|
# Adding a retry mechanism for RabbitMQ connection in case it isn't ready
|
|
for _ in range(5):
|
|
try:
|
|
main()
|
|
break
|
|
except pika.exceptions.AMQPConnectionError:
|
|
print("RabbitMQ not ready, retrying in 5 seconds...")
|
|
time.sleep(5) |