I'm new to RabbitMQ and am trying to use the pika library to get messages. I can get this to work with basic_consume() but I want to use basic_get() instead because this will be running in an AWS lambda perodically instead of continuously running on a server. I can get it to work with basic_consume() but not with basic_get(). I'm sure it must be a beginners mistake but I can't figure it out.
#Setup the connection. This seems to be pretty standard and works regardless.
context = ssl.create_default_context(cafile="/path/to/my/cacert.pem")
ssl_options = pika.SSLOptions(context, EventsRoHost)
connection = pika.BlockingConnection(pika.ConnectionParameters(EventsRoHost, EventsRoPort, EventsRoVhost, pika.PlainCredentials(EventsRoUsername, EventsRoPassword), ssl_options=ssl_options))
channel = connection.channel()
#Use basic_get - THIS DOESN'T WORK (always returns 'No messages returned')
method, properties, body = channel.basic_get(queue_name)
if method:
print(method, properties, body)
channel.basic_ack(method.deliverty_tag)
else:
print('No message returned')
#Use basic_consume - THIS WORKS BUT I NEED A SERVERLESS OPTION
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=False)
try:
channel.start_consuming()
except KeyboardInterrupt:
print("Stopping...")
channel.stop_consuming()
connection.close()
The problem ended up being with the queue. After creating a new queue, I was able to get this to work as expected.