def _setup_connection(self): """Returns True if a valid connection exists already, or if one can be created.""" if self.conn: return True id_conf = read_conf(ID_CONF_FILE_NAME) # The indentity.yaml file contains either a singular string variable # 'rabbit_host', or a comma separated list in the plural variable # 'rabbit_hosts' host = None hosts = id_conf.get('rabbit_hosts', None) if hosts is not None: host = hosts.split(",")[0] else: host = id_conf.get('rabbit_host', None) if host is None: log.warning("no host info in configuration, can't set up rabbit.") return False try: url = "amqp://{}:{}@{}/{}".format(id_conf['rabbit_userid'], id_conf['rabbit_password'], host, id_conf['rabbit_virtual_host']) ssl = None if 'rabbit_use_ssl' in id_conf: if 'ssl_ca' in id_conf: cacert = CACERT_FILE else: cacert = SYSTEM_CACERT_FILE try: os.makedirs('/usr/local/share/ca-certificates') except os.error: # ignore existence of already created directory pass with open('/usr/local/share/ca-certificates/' 'glance-simplestreams-sync.crt', 'w') as f: f.write( base64.b64decode(id_conf['kombu_ssl_ca_certs'])) subprocess.check_call( ['/usr/sbin/update-ca-certificates', '--fresh']) ssl = {'ca_certs': cacert} self.conn = kombu.BrokerConnection(url, ssl=ssl) self.exchange = kombu.Exchange("glance-simplestreams-sync-status") status_queue = kombu.Queue("glance-simplestreams-sync-status", exchange=self.exchange) status_queue(self.conn.channel()).declare() except: log.exception("Exception during kombu setup") return False return True
def setUp(self): self.amqp_handler = logs.AMQPHandler(level=logging.DEBUG) self.amqp_handler.set_job_id(None) self.log = logging.getLogger(self.LOGGER_NAME) self.log.setLevel(logging.DEBUG) self.log.addHandler(self.amqp_handler) cfg = config.get_section('amqp') self.connection = kombu.BrokerConnection(hostname=cfg.get('host'), userid=cfg['user'], password=cfg['password'], virtual_host=cfg['vhost']) self.channel = self.connection.channel() self.exchange = kombu.entity.Exchange(cfg['exchange'], type='topic', channel=self.channel) self.queue = kombu.entity.Queue(exchange=self.exchange, channel=self.channel, routing_key=self.ROUTING_KEY, exclusive=True) self.queue.queue_declare() self.queue.queue_bind() self.consumer = kombu.messaging.Consumer(self.channel, self.queue, no_ack=True, auto_declare=False) self.producer = kombu.messaging.Producer(self.channel, self.exchange, serializer='json')
def __init__(self, hostname, port, userid, password, vhost): import kombu self._conn_proto = kombu.BrokerConnection(hostname=hostname, port=port, userid=userid, password=password, virtual_host=vhost) self._connection_pool = self._conn_proto.Pool(preload=1, limit=None) self.reset()
def __init__(self, amqp_host, amqp_user, amqp_password, amqp_vhost, amqp_port, ssl): # create connection self._conn = kombu.BrokerConnection(hostname=amqp_host, userid=amqp_user, password=amqp_password, virtual_host=amqp_vhost, port=amqp_port, ssl=ssl)
def setUp(self): cfg = config.get_section('amqp') self.connection = kombu.BrokerConnection(hostname=cfg.get('host'), userid=cfg['user'], password=cfg['password'], virtual_host=cfg['vhost']) self.channel = self.connection.channel() self.exchange = kombu.entity.Exchange(cfg['exchange'], type='topic', channel=self.channel) self.producer = kombu.messaging.Producer(self.channel, exchange=self.exchange, serializer="json")
def amqp_connect(): """ Connect to amqp broker with kombu using default configuration and return connection, channel and exchange as tuple. """ cfg = config.get_section("amqp") connection = kombu.BrokerConnection(hostname=cfg['host'], userid=cfg['user'], password=cfg['password'], virtual_host=cfg['vhost']) channel = connection.channel() exchange = kombu.entity.Exchange(cfg['exchange'], type='topic', channel=channel) exchange.declare() return connection, channel, exchange
def test_hazard_curves_task(self): # Test the `hazard_curves` task, but execute it as a normal function # (for purposes of test coverage). hc = self.job.hazard_calculation self.calc.pre_execute() # Update job status to move on to the execution phase. self.job.is_running = True self.job.status = 'executing' self.job.save() src_prog = models.SourceProgress.objects.filter( is_complete=False, lt_realization__hazard_calculation=hc).latest('id') src_id = src_prog.parsed_source.id lt_rlz = src_prog.lt_realization exchange, conn_args = base.exchange_and_conn_args() routing_key = base.ROUTING_KEY_FMT % dict(job_id=self.job.id) task_signal_queue = kombu.Queue('tasks.job.%s' % self.job.id, exchange=exchange, routing_key=routing_key, durable=False, auto_delete=True) def test_callback(body, message): self.assertEqual(dict(job_id=self.job.id, num_items=1), body) message.ack() with kombu.BrokerConnection(**conn_args) as conn: task_signal_queue(conn.channel()).declare() with conn.Consumer(task_signal_queue, callbacks=[test_callback]): # call the task as a normal function core.hazard_curves( self.job.id, [src_id], lt_rlz.id, logictree.LogicTreeProcessor.from_hc( self.job.hazard_calculation)) # wait for the completion signal conn.drain_events() # refresh the source_progress record and make sure it is marked as # complete src_prog = models.SourceProgress.objects.get(id=src_prog.id) self.assertTrue(src_prog.is_complete)
def main(): if len(sys.argv) < 4: print_help() return address = sys.argv[1] exchange_name = sys.argv[2] message = sys.argv[3] connection = kombu.BrokerConnection(address) exchange = kombu.Exchange(exchange_name) channel = connection.channel() producer = kombu.Producer(channel, exchange=exchange, serializer="json") producer.publish(message) producer.close() connection.close()
def _connect(glance_api_cfg): """Create the connection the AMQP. We use BrokerConnection rather than Connection as RHEL 6 has an ancient version of kombu library. """ conn = kombu.BrokerConnection(hostname=glance_api_cfg['host'], port=glance_api_cfg['port'], userid=glance_api_cfg['userid'], password=glance_api_cfg['password'], virtual_host=glance_api_cfg['virtual_host']) exchange = kombu.Exchange(glance_api_cfg['exchange'], type='topic', durable=False, channel=conn.channel()) return conn, exchange
def connect(self, name): self.conn = kombu.BrokerConnection(hostname="localhost", port=5672, userid='guest', password='******', virtual_host="/") self.exchange = kombu.Exchange(name, type='topic', durable=False, channel=self.conn.channel()) #print self.exchange routing_key = name self.queue = kombu.Queue(name=routing_key, routing_key=routing_key, exchange=self.exchange, channel=self.conn.channel(), durable=False) self.queue.declare()
def _make_connection(amqp_host, amqp_port, amqp_user, amqp_password, amqp_vhost): """Create connection. This method creates object representing the connection to RabbitMQ. :param amqp_host: Address of RabbitMQ server. :param amqp_user: Username for connecting to RabbitMQ. :param amqp_password: Password matching the given username. :param amqp_vhost: Virtual host to connect to. :param amqp_port: Port of RabbitMQ server. :return: New connection to RabbitMQ. """ return kombu.BrokerConnection(hostname=amqp_host, userid=amqp_user, password=amqp_password, virtual_host=amqp_vhost, port=amqp_port)
def _setup_connection(self): """Returns True if a valid connection exists already, or if one can be created.""" if self.conn: return True id_conf = read_conf(ID_CONF_FILE_NAME) # The indentity.yaml file contains either a singular string variable # 'rabbit_host', or a comma separated list in the plural variable # 'rabbit_hosts' host = None hosts = id_conf.get('rabbit_hosts', None) if hosts is not None: host = hosts.split(",")[0] else: host = id_conf.get('rabbit_host', None) if host is None: log.warning("no host info in configuration, can't set up rabbit.") return False try: url = "amqp://{}:{}@{}/{}".format(id_conf['rabbit_userid'], id_conf['rabbit_password'], host, id_conf['rabbit_virtual_host']) self.conn = kombu.BrokerConnection(url) self.exchange = kombu.Exchange("glance-simplestreams-sync-status") status_queue = kombu.Queue("glance-simplestreams-sync-status", exchange=self.exchange) status_queue(self.conn.channel()).declare() except: log.exception("Exception during kombu setup") return False return True
def test_signal_task_complete(self): job_id = 7 num_sources = 10 def test_callback(body, message): self.assertEqual(dict(job_id=job_id, num_items=num_sources), body) message.ack() exchange, conn_args = base.exchange_and_conn_args() routing_key = base.ROUTING_KEY_FMT % dict(job_id=job_id) task_signal_queue = kombu.Queue('tasks.job.%s' % job_id, exchange=exchange, routing_key=routing_key, durable=False, auto_delete=True) with kombu.BrokerConnection(**conn_args) as conn: task_signal_queue(conn.channel()).declare() with conn.Consumer(task_signal_queue, callbacks=[test_callback]): # send the signal: base.signal_task_complete(job_id=job_id, num_items=num_sources) conn.drain_events()
import kombu from nae.common import cfg cfg.parse_config() CONF = cfg.CONF params = {} params.setdefault('hostname', CONF.rabbit_host) params.setdefault('port', int(CONF.rabbit_port)) params.setdefault('userid', CONF.rabbit_userid) params.setdefault('password', CONF.rabbit_password) params.setdefault('virtual_host', CONF.rabbit_virtual_host) connection = kombu.BrokerConnection(**params) connection.connect() channel = connection.channel() topic = "compute.test" producer = kombu.Producer(channel, exchange=CONF.control_exchange, routing_key=topic) body = {} while True: print 'send msg', body producer.publish(body, routing_key=topic, exchange=CONF.control_exchange)
def create_connection(self): return kombu.BrokerConnection( self.uri, transport_options={'confirm_publish': True}, )
#!/usr/bin/python #File: receiver.py import kombu connection = kombu.BrokerConnection('librabbitmq://localhost') simple_queue = connection.SimpleQueue('test') print '[*] waiting for messages, if exit press CTRL+C' def message_process(body, message): print '[receive]: %s' % body message.ack() simple_queue.consumer.callbacks = [message_process] simple_queue.get() simple_queue.close()