35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
import pika
|
||
import time
|
||
|
||
def process_inventory_event(ch, method, properties, body):
|
||
decoded_message = body.decode('utf-8')
|
||
print(f" [x] Processing Inventory Event: {decoded_message}")
|
||
time.sleep(2)
|
||
print(" [x] Done")
|
||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||
|
||
def main():
|
||
# Устанавливаем соединение с сервером RabbitMQ
|
||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||
channel = connection.channel()
|
||
|
||
# Объявляем exchange с типом 'fanout'
|
||
channel.exchange_declare(exchange='events', exchange_type='fanout')
|
||
|
||
# Создаем очередь с уникальным именем
|
||
result = channel.queue_declare(queue='', exclusive=True)
|
||
queue_name = result.method.queue
|
||
|
||
# Привязываем очередь к exchange
|
||
channel.queue_bind(exchange='events', queue=queue_name)
|
||
|
||
# Указываем, как обрабатывать сообщения при получении
|
||
channel.basic_consume(queue=queue_name, on_message_callback=process_inventory_event)
|
||
|
||
print(' [*] Waiting for Inventory Events. To exit press CTRL+C')
|
||
# Запускаем бесконечный цикл получения и обработки сообщений
|
||
channel.start_consuming()
|
||
|
||
if __name__ == '__main__':
|
||
main()
|