import pika, socket
credentials = pika.PlainCredentials('xxxx', '1234')
hostname = socket.gethostname()
parameters = pika.ConnectionParameters(host=socket.gethostbyname(hostname), port=5672, virtual_host='/', credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"
channel.queue_declare(queue='hello')
if channel.basic_publish(exchange='', routing_key='hello', body='Hello World!', properties=msg_props):
print ("Message Acknowledged")
else:
print ("Message Lost")
print("[x] Sent 'Hello World!'")
connection.close()
ser_ack.py:
import pika, socket
credentials = pika.PlainCredentials('my_server', '1234')
hostname = socket.gethostname()
parameters = pika.ConnectionParameters(host=socket.gethostbyname(hostname), port=5672, virtual_host='/', credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
OUTPUT:
>python cli_ack.py
Message Lost # But it should be "Message Acknowledged"
[x] Sent 'Hello World!'
>python ser_ack.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received b'Hello World!' # Message is received, but the ack is not sent.
What is the correct consumer program in python? Thank you.