def _init_connection(self) -> None: """Establish a connection to the RabbitMQ instance. Uses the follwing environment variable: - PIKA_RABBITMQ_HOST: Hostname of the running RabbitMQ instance to connect to. """ connection = BlockingConnection( ConnectionParameters(host=PIKA_RABBITMQ_HOST)) self.channel: BlockingChannel = connection.channel()
def setup(self): """ Establish connection to broker. """ try: self.connection = BlockingConnection(self.parameters) except AMQPConnectionError, e: logger.error("Could not connect to Message broker!") logger.error("Broker connection params: {}".format( self.parameters)) logger.error("Error: {}".format(e)) logger.error("Exiting.") sys.exit(1)
def create_connection(config: Mapping[str, Any]) -> BlockingConnection: host = config.get("host") port = config.get("port") or _DEFAULT_PORT username = config.get("username") password = config.get("password") virtual_host = config.get("virtual_host", "") ssl_enabled = config.get("ssl", False) amqp_protocol = "amqp" host_url = host if ssl_enabled: amqp_protocol = "amqps" if port: host_url = host + ":" + str(port) credentials = f"{username}:{password}@" if username and password else "" params = pika.URLParameters(f"{amqp_protocol}://{credentials}{host_url}/{virtual_host}") return BlockingConnection(params)
def start_rabbitmq() -> dict: connection = BlockingConnection(URLParameters(rabbitmq_connection_string)) channel: BlockingChannel = connection.channel() channel.basic_qos(prefetch_count=1) for queue in Queues: channel.queue_declare(queue.value, durable=True) rabbitmq = { 'connection': connection, 'channel': channel, } on_document_to_process_thread_handler_partial = partial( on_document_to_process_thread_handler, threads=threads) channel.basic_consume( queue=Queues.TO_PROCESS.value, on_message_callback=on_document_to_process_thread_handler_partial) return rabbitmq
def _connect(self): log.debug("Starting new connection to amqp://%s:%d/%s", self.__conn_params.host, self.__conn_params.port, self.__conn_params.virtual_host) self.connection = BlockingConnection(self.__conn_params) self._CHNUM += 1 log.debug("Opening channel %d", self._CHNUM) self.channel = self.connection.channel(self._CHNUM) self.channel.exchange_declare("crew.DLX", auto_delete=True, exchange_type="headers") self.channel.queue_declare(queue="crew.DLX", auto_delete=False) self.channel.queue_declare(queue=self._res_queue, exclusive=True, durable=False, auto_delete=True, arguments={"x-message-ttl": 60000}) self.channel.basic_qos(prefetch_count=1) self.channel.queue_bind( "crew.DLX", "crew.DLX", arguments={"x-original-sender": self._res_queue}) self.channel.basic_consume(self._on_dlx_received, queue="crew.DLX") self.channel.basic_consume(self._on_result, queue=self._res_queue) self.__connected = True t = Thread(target=self._consumer) t.daemon = True t.start() while not self.__connected: time.sleep(0.0001)
def __init__( self, queue: str, host: str = "localhost", port: int = 5672, channel: BlockingChannel = None, ): if not _has_pika: raise RuntimeError("Please install the python module: pika") if channel is None: self.conn = BlockingConnection( pika.ConnectionParameters(host=host, port=port)) self.channel = self.conn.channel() else: self.conn = None self.channel = channel self.queue = queue self.channel.queue_declare(queue=queue) # make sure deliveries self.channel.confirm_delivery()
def __init__(self): self._connection = BlockingConnection(ConnectionParameters(HOST))
def open(self) -> None: self._connection = BlockingConnection(ConnectionParameters(HOST))
def connect(connection_url): """ Create and return a fresh connection """ return BlockingConnection(URLParameters(connection_url))