def start_consumer(broker_host, broker_port, username, password, activate_prefetch=None): """ Start the consumer IOLoop """ global connection # global activate_prefetch_count # # activate_prefetch_count = activate_prefetch credentials = pika.PlainCredentials(username=username, password=password) parameters = pika.ConnectionParameters(host=broker_host, port=broker_port, credentials=credentials) try: connection = SelectConnection(parameters, on_connected) logger.info('Connected to broker: {}:{}'.format( broker_host, broker_port)) connection.ioloop.start() except socket.gaierror, e: logger.error("Socket.gaierror! Error: {}".format(e)) if connection: connection.close()
class MQ_Client(): def __init__(self, host, queue, messages): self.host = host self.messages = messages self.queue = queue self.connection = None self.channel = None def on_connected(self, connection): connection.channel(self.on_channel_open) def on_channel_open(self, channel_): self.channel = channel_ self.channel.queue_declare(queue=self.queue, durable=True, exclusive=False, auto_delete=False, callback=self.on_queue_declared) def on_queue_declared(self, frame): for message in self.messages: self.channel.basic_publish(exchange='', routing_key=self.queue, body=json.dumps(message), properties=pika.BasicProperties( content_type="text/plain", delivery_mode=1)) # Close our connection self.connection.close() # def do_send(self, st=0, iname=''): # parameters = pika.URLParameters(BROKER_URL) # # credentials = pika.credentials.PlainCredentials(BROKER_USER, BROKER_PASSWORD) # # parameters = pika.ConnectionParameters(self.host, credentials=credentials) # logger.debug("do_send[%s -- takes(before connection)]: %s seconds." % (iname, (time.time()-st), )) # self.connection = SelectConnection(parameters, self.on_connected) # logger.debug("do_send[%s -- takes(after connection)]: %s seconds." % (iname, (time.time()-st), )) # try: # logger.debug("do_send[%s -- takes(before ioloop.start)]: %s seconds." % (iname, (time.time()-st), )) # self.connection.ioloop.start() # logger.debug("do_send[%s -- takes(after ioloop.start)]: %s seconds." % (iname, (time.time()-st), )) # except KeyboardInterrupt: # self.connection.close() # self.connection.ioloop.start() def do_send(self): parameters = pika.URLParameters(BROKER_URL) # credentials = pika.credentials.PlainCredentials(BROKER_USER, BROKER_PASSWORD) # parameters = pika.ConnectionParameters(self.host, credentials=credentials) self.connection = SelectConnection(parameters, self.on_connected) try: self.connection.ioloop.start() except KeyboardInterrupt: self.connection.close() self.connection.ioloop.start()
def do_send(self): parameters = pika.URLParameters(BROKER_URL) # credentials = pika.credentials.PlainCredentials(BROKER_USER, BROKER_PASSWORD) # parameters = pika.ConnectionParameters(self.host, credentials=credentials) self.connection = SelectConnection(parameters, self.on_connected) try: self.connection.ioloop.start() except KeyboardInterrupt: self.connection.close() self.connection.ioloop.start()
def connect_to_mq(server, vhost, user, passwd, ioloop=False, on_open_callback=None): partes = urlparse.urlparse(server) result = None if partes.scheme == '' or partes.scheme == 'rabbitmq': #Legado, usando RabbitMQ server = partes.netloc or partes.path cred = PlainCredentials(user, passwd) if not ioloop: result = BlockingConnection( ConnectionParameters(str(server), virtual_host=vhost, credentials=cred)) else: result = SelectConnection( ConnectionParameters(str(server), virtual_host=vhost, credentials=cred), on_open_callback) elif partes.scheme == 'nsq': result = NsqConnection(partes.netloc, vhost[1:] if len(vhost) > 1 else '') return result
def start_consumer(broker_host, broker_port, username, password, activate_prefetch=None): """ Start the consumer IOLoop """ global connection # global activate_prefetch_count # # activate_prefetch_count = activate_prefetch credentials = pika.PlainCredentials(username=username, password=password) parameters = pika.ConnectionParameters(host=broker_host, port=broker_port, credentials=credentials) try: connection = SelectConnection(parameters, on_connected) logger.info('Connected to broker: {}:{}'.format(broker_host, broker_port)) connection.ioloop.start() except socket.gaierror, e: logger.error("Socket.gaierror! Error: {}".format(e)) if connection: connection.close()