def disconnect(self): if self._inChannel is not None: self._inChannel.disconnect() return OperationSuccessful("Close operation successfull.") else: Log.error("Unable to connect to message bus broker.") raise ConnectionEstError( "Unable to connect to message bus broker.")
def acknowledge(self): if self._inChannel is not None: self._inChannel.acknowledge() return OperationSuccessful("Commit operation successfull.") else: Log.error("Unable to connect to message bus broker.") raise ConnectionEstError( "Unable to connect to message bus broker.")
def send_message_list(self, message: list, **kwargs): if self._outChannel is not None: self._outChannel.set_topic(kwargs.get(const.TOPIC)) for msg in message: self.send(msg) return OperationSuccessful("Successfully sent messages.") else: Log.error("Unable to connect to Kafka broker.") raise ConnectionEstError( "Unable to connect to message bus broker.")
def init(self): try: if self._message_bus_type == const.KAFKA: self._comm_obj = self._init_kafka_comm() elif self._message_bus_type == const.RMQ: self._comm_obj = self._init_rmq_comm() self._comm_obj.init() Log.debug( f"Initialized the communication channel for {self._comm_type}") except Exception as ex: Log.error(f"Unable to connect to message bus. {ex}") raise ConnectionEstError(f"Unable to connect to message bus. {ex}")
def recv(self, callback_fn=None, message=None, **kwargs): if self._inChannel is not None: try: self._inChannel.channel().subscribe(kwargs.get(const.TOPIC)) msg_list = self._inChannel.channel().consume(num_messages=100, timeout=1.0) except Exception as ex: Log.error(f"Fetching message from kafka broker failed. {ex}") raise MsgFetchError( f"No message fetched from kafka broker. {ex}") else: Log.error("Unable to connect to message bus broker.") raise ConnectionEstError( "Unable to connect to message bus broker.") return [msg.value().decode('utf-8') for msg in msg_list]
def connect(self): """ Initiate the connection with Kafka broker and open the necessary communication channel. """ try: conf = { 'bootstrap.servers': str(self._hosts), 'request.required.acks': 'all', 'max.in.flight.requests.per.connection': 1, 'client.id': self._client_id, 'transactional.id': uuid.uuid4(), 'enable.idempotence': True } self._channel = Producer(conf) self._channel.init_transactions() except Exception as ex: Log.error(f"Unable to connect to message bus broker. {ex}") raise ConnectionEstError( f"Unable to connect to message bus broker. {ex}")
def connect(self): """ Initiate the connection with Kafka broker and open the necessary communication channel. """ try: conf = { 'bootstrap.servers': str(self._hosts), 'group.id': self._group_id, 'group.instance.id': self._consumer_name, 'isolation.level': 'read_committed', 'auto.offset.reset': 'earliest', 'enable.auto.commit': False } self._channel = Consumer(conf) Log.info( f"message bus consumer Channel initialized. Group : {self._group_id}" ) except Exception as ex: Log.error(f"Unable to connect to message bus broker. {ex}") raise ConnectionEstError( f"Unable to connect to message bus broker. {ex}")
def init(self): """ Initialize the object usinf configuration params passed. Establish connection with Kafka broker. """ self._channel = None retry_count = 0 try: while self._channel is None and int( self._retry_counter) > retry_count: self.connect() if self._channel is None: Log.warn(f"message bus producer connection Failed. Retry Attempt: {retry_count+1}" \ f" in {2**retry_count} seconds") time.sleep(2**retry_count) retry_count += 1 else: Log.debug(f"message bus producer connection is Initialized."\ f"Attempts:{retry_count+1}") except Exception as ex: Log.error(f"message bus producer initialization failed. {ex}") raise ConnectionEstError( f"Unable to connect to message bus broker. {ex}")