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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#!/bin/sh
# Reset rabbitmq
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
cat <<EOF >send.py
#!/usr/bin/env python3
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
EOF
cat <<EOF >receive.py
#!/usr/bin/env python3
import pika, sys, os
def main():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)
EOF
echo "sending the message now"
python3 send.py
# `rabbitmqctl list_queues` should look like
# Timeout: 60.0 seconds ...
# Listing queues for vhost / ...
# name messages
# hello 1
#
# Check that the hello queue exists and that there is 1 message
rabbitmqctl list_queues # print for debugging
if rabbitmqctl list_queues | grep hello | awk '{print $2}' | grep -q "^1$"; then
echo "The 'hello' queue exists with a single message."
else
if [ $? -eq 1 ]; then
echo "Error: 'hello' queue not found."
exit 1
else
echo "Error: An unknown error occurred."
exit 1
fi
fi
# should see
# [*] Waiting for messages. To exit press CTRL+C
# [x] Received b'Hello World!'
timeout 3s python3 receive.py
# now the hello queue should still exist, but have no messages like so:
# # rabbitmqctl list_queues
# Timeout: 60.0 seconds ...
# Listing queues for vhost / ...
# name messages
# hello 0
#
# Check that the hello queue exists and that there is no message
rabbitmqctl list_queues # print for debugging
if rabbitmqctl list_queues | grep hello | awk '{print $2}' | grep -q "^0$"; then
echo "The 'hello' queue exists with no messages."
else
if [ $? -eq 1 ]; then
echo "Error: 'hello' queue not found."
exit 1
else
echo "Error: An unknown error occurred."
exit 1
fi
fi
|