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 """ LOGGER.info("Connecting to %s", self._server_details.host) credentials = PlainCredentials(self._server_details.username, self._server_details.password) connection_params = ConnectionParameters( host=self._server_details.host, port=self._server_details.port, virtual_host=self._server_details.vhost, credentials=credentials, ) if self._server_details.uses_ssl: cafile = os.getenv("REQUESTS_CA_BUNDLE") ssl_context = ssl.create_default_context(cafile=cafile) connection_params.ssl_options = SSLOptions(ssl_context) return SelectConnection( parameters=connection_params, on_open_callback=self.on_connection_open, on_open_error_callback=self.on_connection_open_error, on_close_callback=self.on_connection_closed, )
def start(self): """Start worker.""" connected = False # Define connection while (not connected): try: self._creds = self._amqp_url.split('amqp://')[1].split("@")[0] parameters = URLParameters(self._amqp_url) self._host = parameters.host self._port = "15672" self._vhost = parameters.virtual_host self._blocking_connection = BlockingConnection(parameters) self._connection = SelectConnection( URLParameters(self._amqp_url), on_open_callback=self.on_open_connection) self._connection.ioloop.start() connected = True # Catch a Keyboard Interrupt to make sure that the connection is closed cleanly except KeyboardInterrupt: # Gracefully close the connection self._connection.close() # Start the IOLoop again so Pika can communicate, it will stop on its own when the connection is closed self._connection.ioloop.start() except: if (self._debug): print( " [!] RabbitMQ Host Unrecheable. Reconnecting in {} seconds..." .format(self._reconection_time)) time.sleep(self._reconection_time)
def consume(self): try: self.connection = SelectConnection(self.parameters, self._on_connected) self.connection.ioloop.start() except Exception as e: self.logger.error('{} {}'.format(self.consumer_id, str(e))) self.connection.close() self.connection.ioloop.start()
def connect(self): """ Creating connection object """ url = self.get_url() logger.info('Connecting to %s', url) return SelectConnection( URLParameters(url), self.on_connection_open, stop_ioloop_on_close=False )
def _init_connection(self, connection_parameters: ConnectionParameters) -> BaseConnection: """Initializes a BaseConnection to be used by the listener. Args: connection_parameters: A set up ConnectionParameters instance. Returns: The set up BaseConnection instance. """ connection = SelectConnection(connection_parameters, on_open_callback=self.on_connected) return connection
def __init__(self, host, port, virtual_host, username, password, exchange, routing_key, message): self.channel = None self.exchange = exchange self.routing_key = routing_key self.message = message credentials = PlainCredentials( username, password, erase_on_connect=False) parameters = ConnectionParameters( host, port, virtual_host, credentials=credentials) self.connection = SelectConnection( parameters=parameters, on_open_callback=self.on_connect)
def connect(self, host=None): """ Connection with Rabbit. """ if not host: _, host = list(json.loads(sd_rabbit.rabbit_nodes).items())[0] return SelectConnection(ConnectionParameters( host=host, port=5672, virtual_host=VIRTUAL_HOST, credentials=sd_rabbit.rabbit_credential), on_open_callback=self.on_connected, on_close_callback=self.on_closed)
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 """ url = self.get_url() logger.info('Connecting to %s', url) return SelectConnection( parameters=URLParameters(url), on_open_callback=self.on_connection_open, on_open_error_callback=self.on_connection_open_error, on_close_callback=self.on_connection_closed)
def open_connection(self): """Opens a connection to RabbitMQ This method immediately returns the connection object. However, whether the connection was successful is not know until a callback is invoked (either on_open_callback or on_open_error_callback). Returns: The SelectConnection object """ return SelectConnection( parameters=self._connection_parameters, on_open_callback=self.on_connection_open, on_close_callback=self.on_connection_closed, on_open_error_callback=self.on_connection_closed, )
def run(self): credentials = pika.PlainCredentials(self.username, self.password) ssl_options = None port = self.port if self.use_ssl: port = self.ssl_port if self.use_ssl and self.ssl_auth: ssl_options = { "ca_certs": self.cacertfile, "certfile": self.certfile, "keyfile": self.keyfile, "cert_reqs": CERT_REQUIRED } credentials = ExternalCredentials() try: parameters = pika.ConnectionParameters(host=self.endpoint, port=port, virtual_host=self.vhost, credentials=credentials, ssl=self.use_ssl, ssl_options=ssl_options) except TypeError: parameters = pika.ConnectionParameters(host=self.endpoint, port=port, virtual_host=self.vhost, credentials=credentials) self.logger.debug("Connecting to %s@%s:%s on vhost %s" % (self.username, self.endpoint, port, self.vhost)) try: SelectPoller.TIMEOUT = .1 connection = SelectConnection(parameters) connection.add_on_close_callback(self._on_closed()) connection.callbacks.add(0, '_on_connection_open', self._on_connected(), one_shot=True) self.connection = connection connection.ioloop.start() except IOError, err: if not self.manual_close: self.logger.error("Connection error: {0}".format(err))
def _connect(self): """ Assigns a new pika.SelectConnection to the connection and starts its ioloop to begin connecting. """ if self.connection_parameters is not None: LOGGER.info(f"starting connection towards: " f"{self.connection_parameters.host}:" f"{self.connection_parameters.port}") else: LOGGER.info("starting connection towards: 127.0.0.1:5672") self._connection = SelectConnection( parameters=self.connection_parameters, on_open_callback=self.on_connection_open, on_open_error_callback=self.on_connection_open_error, on_close_callback=self.on_connection_closed) self._connection.ioloop.start()
def test_publish(publisher, monkeypatch): with monkeypatch.context() as m: publisher._channel = Channel(SelectConnection(), 1, lambda: 1) m.setattr(publisher._channel, '_state', Channel.OPEN) mock_publish = Mock() m.setattr(publisher._channel, 'basic_publish', mock_publish) item = {'url': 'https://www.example.com/page'} publisher._publish(item) mock_publish.assert_called_once_with( exchange='exchange', routing_key='exchange.queue', body=json.dumps(item, ensure_ascii=False), properties=BasicProperties(app_id='app', content_type='application/json', delivery_mode=1), mandatory=True)
def _connect(self): # type: (Consumer) -> SelectConnection """ Connect to RabbitMQ :return pika.adapters.SelectConnection """ param = pika.URLParameters(self.amqp_url) logger.info('Connecting to: {host}:{port}/{vhost}; SSL: {ssl}'.format( host=param.host, port=param.port, vhost=param.virtual_host, ssl=param.ssl)) return SelectConnection( parameters=param, on_open_callback=self._on_connection_open, on_open_error_callback=self._on_open_connection_error, on_close_callback=self._on_connection_closed, stop_ioloop_on_close=False)
def start(self): """Start worker.""" # Define connection while True: try: self._connection = SelectConnection( URLParameters(self._amqp_url), on_open_callback=self.on_open_connection) self._connection.ioloop.start() # Catch a Keyboard Interrupt to make sure that the connection is closed cleanly except KeyboardInterrupt: # Gracefully close the connection self._connection.close() # Start the IOLoop again so Pika can communicate, it will stop on its own when the connection is closed self._connection.ioloop.start() except: if (self._debug): print( " [!] RabbitMQ Host Unreachable. Reconnecting in {} seconds..." .format(self._reconnection_time)) sleep(self._reconnection_time)
def get(self): queue_name = "s_connection" def on_channel(connection): connection.channel(on_declare) def on_declare(channel): channel.queue_declare(publisher(channel), queue_name) def publisher(channel): channel.basic_publish(exchange='', routing_key=queue_name, body='[%s]It is asyn publish') connection = SelectConnection(ConnectionParameters('localhost'), on_channel) # connection = BlockingConnection(ConnectionParameters()) # channel = connection.channel() # channel.queue_declare(queue=queue_name) # channel.basic_publish(exchange='', routing_key=queue_name, body='sss') self.write("[Tornado asyn produce] OK")
# Delete the row elif message['operation'] == 'delete': data = {'row_id': message['data']['row_id']} self.cursor.execute(DELETE_QUERY, data) # Ack the message channel.basic_ack(delivery_tag=method_frame.delivery_tag) if __name__ == '__main__': consumer = ExampleConsumer() # Connect to RabbitMQ host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1' connection = SelectConnection(ConnectionParameters(host), consumer._on_connected) # Loop until CTRL-C try: # Start our blocking loop connection.ioloop.start() except KeyboardInterrupt: # Close the connection connection.close() # Loop until the conneciton is closed connection.ioloop.start()
QUEUE = 'xxx' def on_connection_open(connection): connection.channel(on_channel_open) def on_channel_open(channel): global CHANNEL CHANNEL = channel CHANNEL.queue_declare(queue=QUEUE, callback=on_queue_declared) def on_queue_declared(frame): count = 0 while True: body = str(count) CHANNEL.basic_publish(exchange='', routing_key=QUEUE, body=body) print 'Sent %s' % body sleep(3) count += 1 parameters = ConnectionParameters() connection = SelectConnection(parameters, on_open_callback=on_connection_open) try: connection.ioloop.start() except KeyboardInterrupt: connection.close() connection.ioloop.start()
def __init__(self, options={}): self.options = options self.connection = SelectConnection(on_open_callback=self.on_connected)