-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive_logs.py
31 lines (23 loc) · 1.12 KB
/
receive_logs.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
# this is the tutorials of RabbitMQ
# encoding=utf8
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
# The messages will be lost if no queue is bound to the exchange yet, but that's okay for us; if no consumer is listening yet we can safely discard the message.
channel.exchange_declare(exchange='logs',type='fanout')
# Temporary queues
# 1.let the server choose a random queue name for us. We can do this by not supplying the queue parameter to queue_declare:
# 2.once we disconnect the consumer the queue should be deleted,There's an exclusive flag for that:
# So it should be used like this
queue_declared_result = channel.queue_declare(exclusive=True)
queue_name = queue_declared_result.method.queue
print queue_name
# Bind the queue and exchange
channel.queue_bind(exchange='logs',queue=queue_name)
print ' [*] Waiting for logs. To exit press CTRL+C'
def callback(ch, method, properties, body):
print " [x] %r" % (body,)
channel.basic_consume(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()