-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.py
56 lines (45 loc) · 1.87 KB
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pika
import sys
import time
# RabbitMQ configuration
RABBITMQ_HOST = 'localhost'
RABBITMQ_USER = 'guest'
RABBITMQ_PASSWORD = 'guest'
EXCHANGE_NAME = 'homing-pigeon'
ROUTING_KEY = '' # Leave empty or set appropriately
MESSAGE_COUNT = 100000 # Number of messages to send
MESSAGE_BODY = '{"meta": { "index" : { "_index" : "test", "_id" : "1" } },"data": { "field1" : "value1" }}'
def send_messages():
try:
# Connect to RabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST, credentials=pika.PlainCredentials(RABBITMQ_USER, RABBITMQ_PASSWORD)))
channel = connection.channel()
# Declare the exchange (optional if already declared)
channel.exchange_declare(exchange=EXCHANGE_NAME, exchange_type='fanout', durable=True)
print(f"Starting to send {MESSAGE_COUNT} messages to exchange: {EXCHANGE_NAME}")
for i in range(1, MESSAGE_COUNT + 1):
# Publish message
channel.basic_publish(
exchange=EXCHANGE_NAME,
routing_key=ROUTING_KEY,
body=MESSAGE_BODY,
properties=pika.BasicProperties(delivery_mode=2), # Make messages persistent
)
# Print progress every 10,000 messages
if i % 10000 == 0:
print(f"Sent {i} messages...")
print(f"Successfully sent {MESSAGE_COUNT} messages.")
except KeyboardInterrupt:
print("Process interrupted.")
sys.exit(0)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Ensure connection is closed
if 'connection' in locals() and connection.is_open:
connection.close()
if __name__ == '__main__':
start_time = time.time()
send_messages()
elapsed_time = time.time() - start_time
print(f"Time taken: {elapsed_time:.2f} seconds")