-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbit_monitor.py
38 lines (27 loc) · 1.28 KB
/
rabbit_monitor.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
import pika
import logging
import params as param
logger = logging.getLogger(__name__)
# RabbitMQ : Controller is 'Consumer' and would 'Consume(=Receive)' message
class Consumer:
def __init__(self):
self.rabbit_ip = param.mq_address
def receive_message(self, strmark):
connection = pika.BlockingConnection(pika.ConnectionParameters(host=self.rabbit_ip))
try:
channel = connection.channel()
# create channel to send message
queue_state = channel.queue_declare(queue=strmark, durable=True, passive=True)
queue_empty = queue_state.method.message_count == 0
logger.info('>>> Waiting for messages from Channel ' + strmark + '.')
def callback(ch, method, properties, body):
logger.info('>>> Received %s ' % (body, ))
while queue_empty:
method, properties, body = channel.basic_get(queue=strmark, no_ack=True)
callback(channel, method, properties, body)
logger.info('>>> Finish receiving messages from Channel ' + strmark + '.')
connection.close()
return True
except Exception as e:
logger.error('>>> Something wrong with RabbitMQ ...')
logger.error('Errors : ', e.args)