DAS_2024_1/rogashova_ekaterina_lab_4/Publisher.py
2024-11-15 11:50:03 +04:00

25 lines
1021 B
Python

import pika
import json
import time
import random
def generate_event():
events = [
{"event": "order_placed", "order_id": random.randint(100, 999), "product_name": random.choice(["iPhone 14 Pro", "Samsung Galaxy S23 Ultra", "MacBook Pro"]), "customer_id": random.randint(1000, 9999)},
{"event": "product_viewed", "product_id": random.randint(1000, 9999), "product_name": random.choice(["iPhone 14 Pro", "Samsung Galaxy S23 Ultra", "MacBook Pro"])},
{"event": "user_registered", "user_id": random.randint(10000, 99999), "username": f"user_{random.randint(1, 100)}"}
]
return json.dumps(random.choice(events))
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='online_store_events', exchange_type='fanout')
while True:
message = generate_event()
channel.basic_publish(exchange='online_store_events', routing_key='', body=message)
print(f"Sent message: {message}")
time.sleep(1)
connection.close()