File: publish-subscribe

package info (click to toggle)
rabbitmq-server 4.0.5-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,332 kB
  • sloc: erlang: 257,826; javascript: 22,466; sh: 3,037; makefile: 2,599; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (52 lines) | stat: -rwxr-xr-x 1,399 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

set -e

# Reset rabbitmq
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app

cat << EOF > receive_logs.py
#!/usr/bin/env python3
import pika

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue

channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(f" [x] {body}")

channel.basic_consume(
    queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()
EOF

# create 2 consumers
python3 receive_logs.py &
python3 receive_logs.py &

# the consumers are listed in the bindings.
# # rabbitmqctl list_bindings | grep logs
# logs	exchange	amq.gen-9cej1xIy-VQLf6d3UXvU1w	queue	amq.gen-9cej1xIy-VQLf6d3UXvU1w	[]
# logs	exchange	amq.gen-H-ky4z3jFxa5UxpigJAD0g	queue	amq.gen-H-ky4z3jFxa5UxpigJAD0g	[]
rabbitmqctl list_bindings # print for debug
# Check that JSON formatter works as well
rabbitmqctl list_bindings --formatter json
if [ $(rabbitmqctl list_bindings | grep "logs" | awk '{print $1}' | wc -l) -eq 2 ]; then
    echo "The number of consumers is 2."
else
    echo "The number of consumers is not 2."
    exit 1
fi