Ejemplo n.º 1
0
    def create_connection(self, for_listening=False):
        """Create and return connection to any available host.

        :return: created connection
        :raise: ConnectionException if all hosts are not reachable
        """
        host_count = len(self._connection_host_param_list)
        connection_attempts = host_count

        pika_next_connection_num = self._next_connection_num()

        while connection_attempts > 0:
            try:
                return self.create_host_connection(pika_next_connection_num,
                                                   for_listening)
            except pika_pool.Connection.connectivity_errors as e:
                LOG.warning("Can't establish connection to host. %s", e)
            except pika_drv_exc.HostConnectionNotAllowedException as e:
                LOG.warning("Connection to host is not allowed. %s", e)

            connection_attempts -= 1
            pika_next_connection_num += 1
            pika_next_connection_num %= host_count

        raise pika_drv_exc.EstablishConnectionException(
            "Can not establish connection to any configured RabbitMQ host: " +
            str(self._connection_host_param_list))
    def create_connection(self, for_listening=False):
        """Create and return connection to any available host.

        :return: created connection
        :raise: ConnectionException if all hosts are not reachable
        """

        with self._connection_lock:

            host_count = len(self._host_list)
            connection_attempts = host_count

            while connection_attempts > 0:
                self._cur_connection_host_num += 1
                self._cur_connection_host_num %= host_count
                try:
                    return self._create_host_connection(
                        self._cur_connection_host_num, for_listening
                    )
                except pika_drv_cmns.PIKA_CONNECTIVITY_ERRORS as e:
                    LOG.warning("Can't establish connection to host. %s", e)
                except pika_drv_exc.HostConnectionNotAllowedException as e:
                    LOG.warning("Connection to host is not allowed. %s", e)

                connection_attempts -= 1

            raise pika_drv_exc.EstablishConnectionException(
                "Can not establish connection to any configured RabbitMQ "
                "host: " + str(self._host_list)
            )