我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用pika.URLParameters()。
def teamspeak(self, msg): # Connect to rabbitmq parameters = pika.URLParameters('amqp://'+self.RMQUSER+':'+self.RMQPASS+'@'+self.RMQHOST+':'+self.RMQPORT+'/'+self.RMQVHOST+'?socket_timeout='+self.RMQSOCKETTIMEOUT) connection = pika.BlockingConnection(parameters) channel = connection.channel() channel.queue_declare(queue='teamspeak') if isinstance(msg, list): for m in msg: logger.info("Sending msg to teamspeak: "+str(m)) channel.basic_publish(exchange='',routing_key='teamspeak',body=json.dumps(m)) else: logger.info("Sending msg to teamspeak: "+str(msg)) channel.basic_publish(exchange='',routing_key='teamspeak',body=json.dumps(msg)) connection.close()
def __init__(self, config): self.config = config self.message_ttl = str(self.config.market_expiration_time * 1000) self.report_queue = self.config.report_queue self.params = pika.URLParameters(self.config.amqp_url) self.queue_args = self.config.queue_args self._connection = None self._channel = None ##app_config = configparser.ConfigParser() ##app_config.read('config') self.key = config.creds['settings']['aes_key']
def __init__(self, cloud_amqp_url, queue_name): self.cloud_amqp_url = cloud_amqp_url self.queue_name = queue_name self.params = pika.URLParameters(cloud_amqp_url) self.params.socket_timeout = 3 self.connection = pika.BlockingConnection(self.params) self.channel = self.connection.channel() self.channel.queue_declare(queue=queue_name) # send a message
def telegram(self, msg): """ REQUIRED msg params: chat_id: int, telegram user chat id message: string, message to be sent Optional: Markdown: bool, if markdown should be parsed or not disable_web_page_preview: bool, if preview should be sent or not Changes 1) picture: path of picture to be sent 2) sticker: file id of sticker to be sent (sends message after sticker) """ # Connect to rabbitmq parameters = pika.URLParameters('amqp://'+self.RMQUSER+':'+self.RMQPASS+'@'+self.RMQHOST+':'+self.RMQPORT+'/'+self.RMQVHOST+'?socket_timeout='+self.RMQSOCKETTIMEOUT) connection = pika.BlockingConnection(parameters) channel = connection.channel() channel.queue_declare(queue='telegram') if isinstance(msg, list): for m in msg: logger.info("Sending msg to telegram: "+str(m)) channel.basic_publish(exchange='',routing_key='telegram',body=json.dumps(m)) else: logger.info("Sending msg to telegram: "+str(msg)) channel.basic_publish(exchange='',routing_key='telegram',body=json.dumps(msg)) connection.close() ##################### # MAIN #####################
def init(): logger.info("Initializing queue manager") base = "amqp://{username}:{password}@{host}:{port}" params = pika.URLParameters(base.format(username=config.rabbit_mq_user, password=config.rabbit_mq_pass, host=config.rabbit_mq_url, port=config.rabbit_mq_port)) QueueWrapper.connection = pika.BlockingConnection(params) QueueWrapper.channel = QueueWrapper.connection.channel() QueueWrapper.channel.basic_qos(prefetch_count=1) QueueWrapper.existing_queues = set() QueueWrapper.existing_queues_lock = threading.Lock()
def _connect(self): try: logger.info('attempt to open connection', server='primary', category='rabbitmq') self.connection = BlockingConnection(URLParameters(self.rabbitmq_url)) except AMQPError as e: logger.error('unable to open connection', exc_info=e, server='primary', category='rabbitmq') try: logger.info('attempt to open connection', server='secondary', category='rabbitmq') self.connection = BlockingConnection(URLParameters(self.rabbitmq_secondary_url)) except AMQPError as err: logger.error('unable to open connection', exc_info=e, server='secondary', category='rabbitmq') raise err
def connect(self): """This method connects to RabbitMQ, returning the connection handle. When the connection is established, the on_connection_open method will be invoked by pika. :rtype: pika.SelectConnection """ log.info('Connecting to %s', self.amqp_url) return adapters.TornadoConnection(pika.URLParameters(self.amqp_url), self.on_connection_open)
def __init__(self, options): Bus.__init__(self) busaddr = options.rabbitaddr # Connects to the rabbitmq server busaddr += "/%2F?connection_attempts=200&heartbeat_interval=" +\ str(options.heartbeat) self.busaddr = busaddr params = pika.URLParameters(busaddr) log.info("Connecting to rabbitmq server at: " + str(busaddr)) b = False while not b: try: self.connection = pika.BlockingConnection(params) b = True except pika.exceptions.ConnectionClosed: log.warning("Cannot connect to rabbitmq at: " + str(busaddr) + ". Retrying..") time.sleep(0.5) # TODO: quit here (failed to connect) self.channel = self.connection.channel() signal.signal(signal.SIGTERM, self.sigterm_handler) #: Contains agent instance. This Bus implementation accepts only one #: agent. Agent must be run using separate RabbitBus() (bus slave) #: instances. self.agent = None self.main_thread_id = thread.get_ident() # TODO: check if key exists
def reconnect(self): b = False params = pika.URLParameters(self.busaddr) while not b: try: log.info("Connecting to rabbitmq server at: " + str(self.busaddr)) self.connection = pika.BlockingConnection(params) self.channel = self.connection.channel() self.queue_ret = self.channel.queue_declare(self.return_queue) self.return_queue = self.queue_ret.method.queue self.signal_exchange = self.channel.exchange_declare( exchange='rebus_signals', exchange_type='fanout') self.ret_signal_queue = self.channel.queue_declare( self.signal_queue, exclusive=True) self.signal_queue = self.ret_signal_queue.method.queue self.channel.queue_bind(exchange='rebus_signals', queue=self.signal_queue) self.channel.basic_consume(self.signal_handler, queue=self.signal_queue, no_ack=True) b = True except pika.exceptions.ConnectionClosed: log.info("Failed to reconnect to RabbitMQ. Retrying..") time.sleep(0.5)
def start(self): '''Start all of the exciting AMQPness.''' # Connect to RabbitMQ parameters = pika.URLParameters(self.url) connection = pika.SelectConnection(parameters, self.on_connected) # Main loop: try: connection.ioloop.start() except KeyboardInterrupt: # shut down gracefully connection.close() connection.ioloop.start()
def __init__(self, cloud_amqp_url, queue_name): self.cloud_amqp_url = cloud_amqp_url self.queue_name = queue_name self.params = pika.URLParameters(cloud_amqp_url) self.params.socket_timeout = 3 self.connection = pika.BlockingConnection(self.params) self.channel = self.connection.channel() self.channel.queue_declare(queue=queue_name) # Send a message
def connect(self): params = pika.URLParameters(os.environ['RABBITMQ_CONNECTION']) self._connection = pika.BlockingConnection(params) self._channel = self._connection.channel() self._connected = True
def connect(self, ): aps = pika.URLParameters(self.auri) while 1: try: rc = pika.BlockingConnection(aps) except: print "Retry connect to mq center for in seconds" time.sleep(5) continue break return rc
def __init__(self, *, url=None, middleware=None, **parameters): super().__init__(middleware=middleware) if url: self.parameters = pika.URLParameters(url) else: self.parameters = pika.ConnectionParameters(**parameters) self.connections = set() self.channels = set() self.queues = set() self.state = local()