Python pika.SelectConnection() Examples

The following are 30 code examples of pika.SelectConnection(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pika , or try the search function .
Example #1
Source File: amqp_async_consumer.py    From resilient-community-apps with MIT License 8 votes vote down vote up
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

        """
        try:
            log.info('Connecting to %s', self._url)
            return pika.SelectConnection(
                        pika.ConnectionParameters(
                            host=self._host,
                            port=self._port,
                            virtual_host=self._virtual_host,
                            credentials=pika.PlainCredentials(self._username, self._amqp_password),
                            heartbeat_interval=600,
                            blocked_connection_timeout=300),
                        self.on_connection_open,
                        stop_ioloop_on_close=False,
                        )
        except AMQPConnectionError:
            raise ValueError("Error connecting to the AMQP instance, double check credentials and any proxy settings") 
Example #2
Source File: PubSubConsumer.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
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

        """
        self.logger.info('Connecting to %s', self._prefix)
        account = 'opensuse'
        server = 'rabbit.opensuse.org'
        if self._prefix == 'suse':
            account = 'suse'
            server = 'rabbit.suse.de'
        credentials = pika.PlainCredentials(account, account)
        context = ssl.create_default_context()
        ssl_options = pika.SSLOptions(context, server)
        parameters = pika.ConnectionParameters(server, 5671, '/', credentials, ssl_options=ssl_options, socket_timeout=10)
        return pika.SelectConnection(parameters,
                                     on_open_callback=self.on_connection_open) 
Example #3
Source File: PubSubConsumer.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
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

        """
        self.logger.info('Connecting to %s', self._prefix)
        account = 'opensuse'
        server = 'rabbit.opensuse.org'
        if self._prefix == 'suse':
            account = 'suse'
            server = 'rabbit.suse.de'
        credentials = pika.PlainCredentials(account, account)
        context = ssl.create_default_context()
        ssl_options = pika.SSLOptions(context, server)
        parameters = pika.ConnectionParameters(server, 5671, '/', credentials, ssl_options=ssl_options, socket_timeout=10)
        return pika.SelectConnection(parameters,
                                     on_open_callback=self.on_connection_open) 
Example #4
Source File: rpc_server.py    From ab-2018 with GNU General Public License v3.0 6 votes vote down vote up
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

        """
        return pika.SelectConnection(
            pika.ConnectionParameters(
                host='localhost',
                virtual_host=self.VIRTUAL_HOST,
                credentials=self.CREDENTIALS
            ),
            self.on_connection_open,
            stop_ioloop_on_close=False
        ) 
Example #5
Source File: MessageInterface.py    From pilot with Apache License 2.0 6 votes vote down vote up
def open_select_connection(self,
                               on_open_callback              = None,
                               on_open_error_callback        = None,
                               on_close_callback             = None,
                               stop_ioloop_on_close          = True,
                              ):
        tolog("MQ ARGO: create select connection")
        self.create_connection_parameters()
        # open the connection
        if on_open_callback is not None:
            try:
                self.connection = pika.SelectConnection(self.parameters,
                                                        on_open_callback,
                                                        on_open_error_callback,
                                                        on_close_callback,
                                                        stop_ioloop_on_close,
                                                        )
            except:
                tolog('MQ ARGO: Exception received while trying to open select connection to message server: ' + str(sys.exc_info()))
                raise 
Example #6
Source File: pika.py    From rasa-for-botfront with Apache License 2.0 6 votes vote down vote up
def initialise_pika_select_connection(
    parameters: "Parameters",
    on_open_callback: Callable[["SelectConnection"], None],
    on_open_error_callback: Callable[["SelectConnection", Text], None],
) -> "SelectConnection":
    """Create a non-blocking Pika `SelectConnection`.

    Args:
        parameters: Parameters which should be used to connect.
        on_open_callback: Callback which is called when the connection was established.
        on_open_error_callback: Callback which is called when connecting to the broker
            failed.

    Returns:
        A callback-based connection to the RabbitMQ event broker.
    """
    import pika

    return pika.SelectConnection(
        parameters,
        on_open_callback=on_open_callback,
        on_open_error_callback=on_open_error_callback,
    ) 
Example #7
Source File: consumer.py    From yabmp with Apache License 2.0 5 votes vote down vote up
def run(self):
        """Run the example consumer by connecting to RabbitMQ and then
        starting the IOLoop to block and allow the SelectConnection to operate.

        """
        self._connection = self.connect()
        self._connection.ioloop.start() 
Example #8
Source File: PubSubConsumer.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def on_connection_open(self, unused_connection):
        """This method is called by pika once the connection to RabbitMQ has
        been established. It passes the handle to the connection object in
        case we need it, but in this case, we'll just mark it unused.

        :type unused_connection: pika.SelectConnection

        """
        self.logger.info('Connection opened')
        self.add_on_connection_close_callback()
        self.open_channel() 
Example #9
Source File: rpc_server.py    From ab-2018 with GNU General Public License v3.0 5 votes vote down vote up
def on_connection_open(self, host):
        """This method is called by pika once the connection to RabbitMQ has
        been established. It passes the handle to the connection object in
        case we need it, but in this case, we'll just mark it unused.

        :type unused_connection: pika.SelectConnection

        """
        print('Connection opened')
        self.add_on_connection_close_callback()
        self.open_channel() 
Example #10
Source File: rpc_server.py    From ab-2018 with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        """Run the example consumer by connecting to RabbitMQ and then
        starting the IOLoop to block and allow the SelectConnection to operate
        and, always connect to master node of queue.
        """
        self._connection = self.connect()
        self._connection.ioloop.start() 
Example #11
Source File: pika_rabbit.py    From FFXIVBOT with GNU General Public License v3.0 5 votes vote down vote up
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._url)
        parameters = pika.URLParameters(self._url)

        return pika.SelectConnection(
            parameters, self.on_connection_open, stop_ioloop_on_close=False
        ) 
Example #12
Source File: pika_rabbit.py    From FFXIVBOT with GNU General Public License v3.0 5 votes vote down vote up
def on_connection_open(self, unused_connection):
        """This method is called by pika once the connection to RabbitMQ has
        been established. It passes the handle to the connection object in
        case we need it, but in this case, we'll just mark it unused.

        :type unused_connection: pika.SelectConnection

        """
        LOGGER.info("Connection opened")
        self.add_on_connection_close_callback()
        self.open_channel() 
Example #13
Source File: pika_rabbit.py    From FFXIVBOT with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        """Run the example consumer by connecting to RabbitMQ and then
        starting the IOLoop to block and allow the SelectConnection to operate.

        """
        self._connection = self.connect()
        self._connection.ioloop.start() 
Example #14
Source File: delete_queue.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def connect(self):
        return pika.SelectConnection(pika.URLParameters(self._url), self.on_connection_open, stop_ioloop_on_close=True) 
Example #15
Source File: queue_manager.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def connect(self):
        return pika.SelectConnection(pika.URLParameters(self._url), self.on_connection_open, stop_ioloop_on_close=True) 
Example #16
Source File: pika.py    From rasa-for-botfront with Apache License 2.0 5 votes vote down vote up
def _on_open_connection(self, connection: "SelectConnection") -> None:
        logger.debug(f"RabbitMQ connection to '{self.host}' was established.")
        connection.channel(on_open_callback=self._on_channel_open) 
Example #17
Source File: amqp_async_consumer.py    From resilient-community-apps with MIT License 5 votes vote down vote up
def on_connection_open(self, unused_connection):
        """This method is called by pika once the connection to RabbitMQ has
        been established. It passes the handle to the connection object in
        case we need it, but in this case, we'll just mark it unused.

        :type unused_connection: pika.SelectConnection

        """
        log.info('Connection opened')
        self.add_on_connection_close_callback()
        self.open_channel() 
Example #18
Source File: amqp_async_consumer.py    From resilient-community-apps with MIT License 5 votes vote down vote up
def run(self):
        """Run the example consumer by connecting to RabbitMQ and then
        starting the IOLoop to block and allow the SelectConnection to operate.

        """
        try:
            log.error("Starting connection to AMQP Exchange")
            self._connection = self.connect()
            self._connection.ioloop.start()
        except Exception as e:
            log.error("Encountered Exception: {}. Attempting to stop consumer gracefully".format(str(e)))
            self.stop() 
Example #19
Source File: consumer.py    From yabmp with Apache License 2.0 5 votes vote down vote up
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 rabbitmq server')
        return pika.SelectConnection(self.parameters,
                                     self.on_connection_open,
                                     stop_ioloop_on_close=False) 
Example #20
Source File: PubSubConsumer.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def run(self, runtime=None):
        """Run the example consumer by connecting to RabbitMQ and then
        starting the IOLoop to block and allow the SelectConnection to operate.

        """
        if runtime:
            self._run_until = time.time() + runtime
        self._connection = self.connect()
        self._connection.ioloop.start() 
Example #21
Source File: PubSubConsumer.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def on_connection_open(self, unused_connection):
        """This method is called by pika once the connection to RabbitMQ has
        been established. It passes the handle to the connection object in
        case we need it, but in this case, we'll just mark it unused.

        :type unused_connection: pika.SelectConnection

        """
        self.logger.info('Connection opened')
        self.add_on_connection_close_callback()
        self.open_channel() 
Example #22
Source File: queue.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def connect(self):
        """
        From pika docs:

        This method connects to RabbitMQ, returning the connection handle.
        When the connection is established, the on_connection_open method
        will be invoked by pika.

        Returns:
            pika.SelectConnection
        """
        LOGGER.info('Connecting to %s', ascii_str(self._conn_params_dict['host']))
        if self.input_queue is not None or self.output_queue is not None:
            self._amqp_setup_timeout_callback_manager.deactivate()
            self._amqp_setup_timeout_callback_manager.activate()
        return pika.SelectConnection(
                pika.ConnectionParameters(**self._conn_params_dict),
                self.on_connection_open,
                self.on_connection_error_open,
                stop_ioloop_on_close=True,
        ) 
Example #23
Source File: queue.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def on_connection_open(self, connection):
        """
        From pika docs:

        This method is called by pika once the connection to RabbitMQ has
        been established. It passes the handle to the connection object.

        Args:
            `connection`: pika.SelectConnection instance
        """
        LOGGER.info('Connection opened')
        self._connection.add_on_close_callback(self.on_connection_closed)
        self.open_channels() 
Example #24
Source File: RabbitPublisher.py    From ChaosTestingCode with MIT License 5 votes vote down vote up
def connect(self):
        self.connected_node = self.broker_manager.get_current_node(self.publisher_id)
        ip = self.broker_manager.get_node_ip(self.connected_node)
        port = self.broker_manager.get_publisher_port(self.connected_node)
        self.set_actor()
        console_out(f"Attempting to connect to {self.connected_node} {ip}:{port}", self.get_actor())
        parameters = pika.URLParameters(f"amqp://jack:jack@{ip}:{port}/%2F")
        return pika.SelectConnection(parameters,
                                     on_open_callback=self.on_connection_open,
                                     on_open_error_callback=self.on_connection_open_error,
                                     on_close_callback=self.on_connection_closed) 
Example #25
Source File: send-with-confirm.py    From ChaosTestingCode with MIT License 5 votes vote down vote up
def connect():
    global connection, curr_node, terminate
    print("Attempting to connect to " + nodes[curr_node])
    parameters = pika.URLParameters('amqp://jack:jack@' + nodes[curr_node] + ':5672/%2F')
    connection = pika.SelectConnection(parameters=parameters,
                                on_open_callback=on_open,
                                on_open_error_callback=reconnect,
                                on_close_callback=on_close)

    try:
        connection.ioloop.start()
    except KeyboardInterrupt:
        connection.close()
        connection.ioloop.stop()
        terminate = True 
Example #26
Source File: consumer.py    From django-carrot with Apache License 2.0 5 votes vote down vote up
def __init__(self, host: VirtualHost, queue: str, logger: logging.Logger, name: str, durable: bool = True,
                 queue_arguments: dict = None, exchange_arguments: dict = None):
        """
        :param host: the host the queue to consume from is attached to
        :type host: :class:`carrot.objects.VirtualHost`
        :param str queue: the queue to consume from
        :param logger: the logging object
        :param name: the name of the consumer

        """
        super().__init__()

        if queue_arguments is None:
            queue_arguments = {}

        if not exchange_arguments:
            exchange_arguments = {}

        self.failure_callbacks: List[Callable] = []
        self.name = name
        self.logger = logger
        self.queue = queue
        self.exchange = queue

        self.connection: pika.SelectConnection = None
        self.channel: pika.channel = None
        self.shutdown_requested = False
        self._consumer_tag = None
        self._url = str(host)

        self.queue_arguments = queue_arguments
        self.exchange_arguments = exchange_arguments
        self.durable = durable 
Example #27
Source File: consumer.py    From django-carrot with Apache License 2.0 5 votes vote down vote up
def connect(self) -> pika.SelectConnection:
        """
        Connects to the broker
        """
        self.logger.info('Connecting to %s', self._url)
        return pika.SelectConnection(pika.URLParameters(self._url), self.on_connection_open, stop_ioloop_on_close=False) 
Example #28
Source File: consumer.py    From django-carrot with Apache License 2.0 5 votes vote down vote up
def on_connection_open(self, connection: pika.SelectConnection) -> None:
        """
        Callback that gets called when the connection is opened. Adds callback in case of a closed connection, and
        establishes the connection channel

        The `connection` parameter here is not used, as `self.connection` is defined elsewhere, but is included so that
        the signature matches as per Pika's requirements
        """
        self.logger.info('Connection opened')
        self.connection.add_on_close_callback(self.on_connection_closed)
        self.connection.channel(on_open_callback=self.on_channel_open) 
Example #29
Source File: event_consumers.py    From SpoofcheckSelfTest with Apache License 2.0 5 votes vote down vote up
def connect(self, *args, **kwargs):
        """
        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.debug('Connecting to %s', self._url)
        LOGGER.debug('Got arguments: %r %r', args, kwargs)
        conn = adapters.TornadoConnection(pika.URLParameters(self._url),
                                          self.on_connection_open)
        self._connection = conn
        return conn 
Example #30
Source File: event_consumers.py    From SpoofcheckSelfTest with Apache License 2.0 5 votes vote down vote up
def on_connection_open(self, unused_connection):
        """
        This method is called by pika once the connection to RabbitMQ has
        been established. It passes the handle to the connection object in
        case we need it, but in this case, we'll just mark it unused.
        :type unused_connection: pika.SelectConnection
        """
        LOGGER.debug('Connection opened')
        self.add_on_connection_close_callback()
        self.open_channel()