0
0
Fork 0
mirror of https://github.com/alerta/alerta-contrib.git synced 2025-03-16 21:43:30 +00:00
alerta_alerta-contrib/pubsub/example/example-amqp.py
2015-01-26 11:48:57 +00:00

42 lines
1 KiB
Python
Executable file

#!/usr/bin/env python
from kombu import BrokerConnection, Exchange, Queue
from kombu.mixins import ConsumerMixin
AMQP_URL = 'amqp://guest:guest@localhost:5672//'
AMQP_QUEUE = 'example'
class Worker(ConsumerMixin):
def __init__(self, connection):
self.connection = connection
self.exchange = Exchange(AMQP_QUEUE, type='direct', durable=True)
self.queue = Queue(AMQP_QUEUE, exchange=self.exchange, routing_key=AMQP_QUEUE)
def get_consumers(self, Consumer, channel):
return [
Consumer(queues=[self.queue], callbacks=[self.on_message], accept=['json']),
]
def on_message(self, body, message):
print("RECEIVED MESSAGE: %r" % (body, ))
message.ack()
def main():
from kombu.utils.debug import setup_logging
setup_logging(loglevel='DEBUG')
with BrokerConnection(AMQP_URL) as connection:
try:
Worker(connection).run()
except KeyboardInterrupt:
print('Exiting...')
if __name__ == '__main__':
main()