コード例 #1
0
 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.")
コード例 #2
0
 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.")
コード例 #3
0
 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.")
コード例 #4
0
ファイル: message.py プロジェクト: mssawant/cortx-utils
 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}")
コード例 #5
0
 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]
コード例 #6
0
 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}")
コード例 #7
0
 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}")
コード例 #8
0
 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}")