from queues import queue_user_write from kombu.mixins import ConsumerMixin class C(ConsumerMixin): def __init__(self, connection): self.connection = connection return def get_consumers(self, Consumer, channel): return [Consumer(queue_user_write, callbacks=[self.on_message])] def on_message(self, body, message): print("user_write: RECEIVED MSG - body: %r" % (body, )) print("user_write: RECEIVED MSG - message: %r" % (message, )) message.ack() return if __name__ == "__main__": from kombu import BrokerConnection from kombu.utils.debug import setup_logging setup_logging(loglevel="DEBUG") with BrokerConnection("amqp://*****:*****@localhost:5672//") as connection: try: C(connection).run() except KeyboardInterrupt: print("bye bye")
def make(): with BrokerConnection('localhost') as conn: with conn.SimpleQueue(self.queue) as queue: while True: payload = (yield) queue.put(payload)
def __init__(self, connection: RmqConnection, logger:logging.Logger=None) -> None: self._amqp_uri = connection.amqp_uri self._cnx = BrokerConnection(hostname=connection.amqp_uri) self._exchange = connection.exchange self._logger = logger or logging.getLogger(__name__)
def send(self, request, **kwargs): parsed_url = self.__get_parsed_url(request) with BrokerConnection(parsed_url.broker_url, transport_options={'region': 'sa-east-1'}) as conn: return self._send(conn, request, parsed_url, **kwargs)
} } if __name__ == '__main__': parser = argparse.ArgumentParser(description='Utility to send messages to RMQ..') parser.add_argument("exchange_name", type=str, help="name of the exchange to publish message to") parser.add_argument("routing_key", type=str, help="routing key used to route message to appropriate queue") args = parser.parse_args() setup_logging(loglevel='DEBUG', loggers=['']) topic_exchange = Exchange(name=args.exchange_name, type='topic', durable=False) with BrokerConnection('amqp://*****:*****@localhost:5672//') as connection: with connection.Producer(exchange=topic_exchange,routing_key=args.routing_key, serializer='json') as producer: try: print "\nMessage format taken from https://wiki.openstack.org/wiki/NotificationEventExamples..\n" print "Message to be published: " + str(SAMPLE_NOTIFICATION_PAYLOAD) print "\nPublishing message to exchange %s with routing key %s" % (args.exchange_name, args.routing_key) producer.publish(SAMPLE_NOTIFICATION_PAYLOAD) except Exception as ex: print str(ex) print "\nMessage sent."
def my_monitor(): connection = BrokerConnection('amqp://*****:*****@localhost:5672//') import json def on_task_succeeded(event): #print "TASK SUCCEEDED! ", event result = event['result'] #print "RESULT: ", result #result = json.loads(result) # print "RESULT --------------------------", result, " and TYPE OF RESULTTTTTTTTTTTTTTTTTTTTTTTTTTT", type(result) # print "SUBMISSION ID:------------------------------", result['submission_id'] # url_str = build_url(result['submission_id'], result['file_id']) # response = requests.put(url_str, data=serialize(result), headers={'Content-Type' : 'application/json'}) # print "SENT PUT REQUEST. RESPONSE RECEIVED: ", response ## def on_task_failed(event): #print "TASK FAILED!", event exception = event['exception'] def on_update(event): #print "TASK UPDATE!!!!!", event result = event['result'] #print "TYPE OF RESULT IN UPDATE CASEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE", type(result) # url_str = build_url(result['submission_id'], result['file_id']) # response = requests.put(url_str, data=serialize(result), headers={'Content-Type' : 'application/json'}) # print "SENT PUT REQUEST. RESPONSE RECEIVED: ", response def on_event(event): #print "EVENT HAPPENED: ", event pass def on_custom(event): #print "EVENT TYPE: ", event['type'] #if event['type'] != "worker-heartbeat": print("CUSTOM - TASK!", event) def on_worker_online(event): print("worker came onlineeeeee!!! Event: ", str(event)) #try_interval = 3 while True: try: #try_interval *= 2 with connection as conn: recv = EventReceiver(conn, handlers={'task-failed' : on_task_failed, 'task-succeeded' : on_task_succeeded, 'task-sent' : on_event, 'task-received' : on_event, 'task-revoked' : on_event, 'task-retried' : on_event, 'task-started' : on_event, 'task-update' : on_update, 'worker-online' : on_worker_online #'task-update' : on_custom, #'*' : on_custom }) #handlers={'*': on_event}) #print "PRINT FROM Monitor: ", ( vars(recv.consumer) ) recv.capture(limit=None, timeout=None) #try_interval = 3 except (KeyboardInterrupt, SystemExit): print("EXCEPTION KEYBOARD INTERRUPT") sys.exit()
"send messages to (see AMQP for details). Defaults to " "\"%s\"" % config.QUEUE_NAME) parser.add_option("-u", "--userid", help="User ID to use for the queue " "connection (see AMQP for details). Defaults to " "\"%s\"" % config.QUEUE_USER) parser.add_option("-p", "--password", help="Password to use for the " "queue connection (see AMQP for details). Defaults to " "\"%s\"" % config.QUEUE_PASSWORD) parser.add_option("-v", "--vhost", help="Virtual host to connect to " "on the queue host (see AMQP for details). Defaults to " "\"%s\"" % config.QUEUE_VHOST) parser.set_defaults(exchange=config.EXCHANGE, queue=config.QUEUE_NAME, userid=config.QUEUE_USER, password=config.QUEUE_PASSWORD, vhost=config.QUEUE_VHOST) (options, args) = parser.parse_args() if len(args) < 1: sys.stderr.write("Must provide a queue host!\n") sys.exit(1) exchange = Exchange(options.exchange, type="fanout") queue = Queue(options.queue, exchange) connection = BrokerConnection(hostname=args[0], userid=options.userid, password=options.password, virtual_host=options.vhost) channel = connection.channel() consumer = Consumer(channel, queue, callbacks=[demo_listen_and_print]) consumer.consume() connection.drain_events()
fileConfig(config_file_name) # set the basic session maker without zope or autoflush engine = configure_engine(settings, False, autoflush=False, max_overflow=20) # @event.listens_for(engine, "checkin") def show_checkin(*args): global pool_counter pool_counter -= 1 print "checkin pool: %d in %s" % (pool_counter, currentThread()) print_stack() # @event.listens_for(engine, "checkout") def show_checkout(*args): global pool_counter pool_counter += 1 print "checkout pool: %d in %s" % (pool_counter, currentThread()) print_stack() configure(registry, 'source_reader') url = (settings.get('celery_tasks.broker') or settings.get('celery_tasks.imap.broker')) with BrokerConnection(url) as conn: sourcedispatcher = SourceDispatcher(conn) def shutdown(*args): sourcedispatcher.shutdown() signal.signal(signal.SIGTERM, shutdown) try: sourcedispatcher.run() except KeyboardInterrupt: shutdown()
def includeme(config): global _producer_connection, _exchange setup_logging(loglevel='DEBUG') url = (config.registry.settings.get('celery_tasks.broker') or config.registry.settings.get('celery_tasks.imap.broker')) _producer_connection = BrokerConnection(url)
js = json.loads(js) #load messagge xml = "<event>" + json2xml(js) + "</event>" #pass to xml format if not os.path.exists( trace_source + '/' + exchange ): #verify if trace source folder exist, ~/openstack_work/trace/exchange/... os.makedirs(trace_source + '/' + exchange) thread.start_new_thread(event_to_lttng, ( trace_source + '/' + exchange + '/' + rk + '.xml', xml, )) if __name__ == "__main__": print "starting trace listener on rabbitmq" try: parser = argparse.ArgumentParser(description='') parser.add_argument('-c', '--conf', help='Input file name', required=True) #parser.add_argument('-o','--output',help='Output file name', required=True) args = parser.parse_args() _temp = __import__(args.conf, globals(), locals(), ['object'], -1) BROKER_URI = _temp.BROKER_URI exchanges_list = _temp.exchanges_list["exchanges"] trace_source = _temp.trace_source NotificationsDump(BrokerConnection(BROKER_URI), exchanges_list).run() except KeyboardInterrupt: print "\n\n trace file in: " + trace_source sys.exit(0)
def __init__(self, destination, exchange): self._ensure_options = self.RETRY_OPTIONS.copy() self._amqp_uri = destination self._cnx = BrokerConnection(hostname=self._amqp_uri) self._exchange = exchange
def __init__(self, destination, exchange, logger=None): self._amqp_uri = destination self._cnx = BrokerConnection(hostname=self._amqp_uri) self._exchange = exchange self._logger = logger or logging.getLogger(__name__)
def __init__(self, connection_string, exchange): self._connection = BrokerConnection(connection_string) self._connections = set([self._connection]) # set of connection for the heartbeat self._exchange = Exchange(exchange, durable=True, delivry_mode=2, type='topic') monitor_heartbeats(self._connections)
def setUp(self): if not self.abstract: self.connection = BrokerConnection(transport="memory") self.q = self.Queue(None, no_ack=True)
if lookup_addr is not None: dns_lib.del_hostname(nova_hostname) #db_lib.delete_instance(nova_hostname, nova_os_id) else: log.warning("instance {} not found in dns".format(nova_hostname)) log.warning("nothing to do") return False log.info("event of type {} processed".format(event_type)) return True else: log.debug("Instance will not use dns for {}".format(event_type)) return False if __name__ == "__main__": amqp_hosts = DNS_CONF["amqp_hosts"].split(",") BROKER_LIST = [] amqp_user = DNS_CONF["amqp_user"] amqp_password = DNS_CONF["amqp_password"] fs = DNS_CONF["failover_strategy"] for amqp_host in amqp_hosts: broker_uri = "amqp://{}:{}@{}//".format(amqp_user,amqp_password,amqp_host) BROKER_LIST.append(broker_uri) log.info("Connecting to broker {}".format(BROKER_LIST)) with BrokerConnection(BROKER_LIST, failover_strategy=fs ) as connection: try: DnsUpdater(connection).run() except Exception, e: log.error(repr(e)) #TODO use oslo for config, log, db(?), messaging(?)
def setUp(self): self.connection = BrokerConnection(transport=Transport)
EXCHANGE_NAME="nova" ROUTING_KEY="notifications.info" QUEUE_NAME="nova_dump_queue" BROKER_URI="amqp://*****:*****@10.111.45.51//" log.basicConfig(stream=sys.stdout, level=log.DEBUG) class NotificationsDump(ConsumerMixin): def __init__(self, connection): self.connection = connection return def get_consumers(self, consumer, channel): exchange = Exchange(EXCHANGE_NAME, type="topic", durable=False) queue = Queue(QUEUE_NAME, exchange, routing_key = ROUTING_KEY, durable=False, auto_delete=True, no_ack=True) return [ consumer(queue, callbacks = [ self.on_message ]) ] def on_message(self, body, message): log.info('Body: %r' % body) log.info('---------------') if __name__ == "__main__": log.info("Connecting to broker {}".format(BROKER_URI)) with BrokerConnection(BROKER_URI) as connection: NotificationsDump(connection).run() #https://medium.com/python-pandemonium/building-robust-rabbitmq-consumers-with-python-and-kombu-part-1-ccd660d17271
# -*- coding: utf-8 -*- ######################################################################### # # Copyright (C) 2017 OSGeo # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ######################################################################### from django.conf import settings from kombu import BrokerConnection # run in-memory if broker is not available # see producer code for synchronous queue url = settings.ASYNC_SIGNALS_BROKER_URL or 'memory://' connection = BrokerConnection(url)
""" Example that sends a single message and exits using the simple interface. You can use `simple_receive.py` (or `complete_receive.py`) to receive the message sent. """ from kombu import BrokerConnection #: Create connection #: If hostname, userid, password and virtual_host is not specified #: the values below are the default, but listed here so it can #: be easily changed. connection = BrokerConnection(hostname="localhost", userid="guest", password="******", virtual_host="/") #: SimpleQueue mimics the interface of the Python Queue module. #: First argument can either be a queue name or a kombu.Queue object. #: If a name, then the queue will be declared with the name as the queue #: name, exchange name and routing key. queue = connection.SimpleQueue("kombu_demo") queue.put({"hello": "world"}, serializer="json", compression="zlib")
#!/usr/bin/python from __future__ import with_statement import sys import os import ConfigParser from kombu import BrokerConnection from kombu.utils.debug import setup_logging setup_logging(loglevel="INFO") config_file = '/etc/logix/logix.conf' if os.path.isfile( '/etc/logix/logix.conf') else os.getenv('LOGIX_CONF') if not os.path.isfile(config_file): print "Config file %s not found" % config_file sys.exit(1) config = ConfigParser.RawConfigParser() config.read(config_file) with BrokerConnection(config.get('transport', 'url')) as conn: with conn.SimpleQueue(config.get('transport', 'queue'), serializer="json") as queue: message = queue.get(block=True, timeout=10) if message: print message.payload
def setUp(self): self.conn = BrokerConnection("memory://") self.channel = self.conn.channel() self.channel.queues.clear() self.conn.connection.state.clear()
def __init__(self, config): self.connection = BrokerConnection(**config) with producers[self.connection].acquire(block=False) as producer: for queue in task_queues: maybe_declare(queue, producer.channel)