oslo.messaging example
Just a little example how to use oslo.messaging without much around it. You need to have a running rabbitmq (apt-get install rabbitmq-server on ubuntu ;)).
Add user testing to rabbitmq
# rabbitmqctl add_user testing test
# rabbitmqctl set_permissions -p / testing '.*' '.*' '.*'
Setting up a virtualenv and installing oslo.messaging into it. You need to have python-dev installed.
$ virtualenv testing
$ . testing/bin/activate
$ pip install --upgrade pip # to be sure, pip is able to manage wheel
$ pip install --upgrade setuptools # for entry point discovery
$ pip install oslo.messaging
consumer part:
#!/usr/bin/env python
# coding: utf-8
from oslo.config import cfg
from oslo import messaging
import logging
import eventlet
eventlet.monkey_patch()
logging.basicConfig()
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
log.setLevel(logging.INFO)
class NotificationHandler(object):
def info(self, ctxt, publisher_id, event_type, payload, metadata):
if publisher_id == 'testing':
log.info('Handled')
return messaging.NotificationResult.HANDLED
def warn(self, ctxt, publisher_id, event_type, payload, metadata):
log.info('WARN')
def error(self, ctxt, publisher_id, event_type, payload, metadata):
log.info('ERROR')
log.info('Configuring connection')
transport_url = 'rabbit://testing:test@10.0.80.12:5672/'
transport = messaging.get_transport(cfg.CONF, transport_url)
targets = [messaging.Target(topic='monitor')]
endpoints = [NotificationHandler()]
server = messaging.get_notification_listener(transport, targets, endpoints, allow_requeue=True, executor='eventlet')
log.info('Starting up server')
server.start()
log.info('Waiting for something')
server.wait()
producer part:
#!/usr/bin/env python
# coding: utf-8
from oslo.config import cfg
from oslo import messaging
import logging
logging.basicConfig()
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
log.setLevel(logging.INFO)
transport_url = 'rabbit://testing:test@10.0.80.12:5672/'
transport = messaging.get_transport(cfg.CONF, transport_url)
driver = 'messaging'
notifier = messaging.Notifier(transport, driver=driver, publisher_id='testing', topic='monitor')
notifier.info({'some': 'context'}, 'just.testing', {'heavy': 'payload'})