-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv.py
23 lines (18 loc) · 831 Bytes
/
recv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pika
import threading as t
# Connect to RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5000, '/', pika.PlainCredentials('user', 'password')))
channel = connection.channel()
# Declare the queue again in case it doesn't exist (idempotent)
channel.queue_declare(queue='hello')
# Define a callback function to handle incoming messages
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
# Tell RabbitMQ to call the callback function whenever a message is received
channel.basic_consume(queue='hello',
on_message_callback=callback,
auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
# Start consuming messages
t.Thread(target=channel.start_consuming).start()
print("Main thred exiting")