def send_request(self, request): self._response = None generic_request = GenericRPCRequest() generic_request.request_data.CopyFrom( ProtoUtil.message_to_any(message=request)) generic_request.timestamp = str(TimezoneUtil.cur_time_in_pst()) self._corr_id = generic_request.uuid = str(uuid.uuid4()) if self.RESPONSE_MESSAGE_TYPE: generic_request.message_type = ProtoUtil.infer_str_from_message_type( message_type=self.RESPONSE_MESSAGE_TYPE) self._SYS_LOGGER.info("Getting request of uuid [" + generic_request.uuid + '] in queue [' + self.get_queue_name() + '].') self._logger.info("Getting request of uuid [" + generic_request.uuid + '] in queue [' + self.get_queue_name() + '].') try: generic_request_str = ProtoUtil.message_to_string( proto_message=generic_request) self._channel.basic_publish( exchange='', routing_key=self._queue_name, properties=pika.BasicProperties(reply_to=self._callback_queue, correlation_id=self._corr_id), body=base64.b64encode(generic_request_str)) wait_start_time = TimezoneUtil.cur_time_in_pst() while not self._response: self._connection.process_data_events(time_limit=int( EnvUtil.get_pslx_env_variable('PSLX_QUEUE_TIMEOUT'))) if TimezoneUtil.cur_time_in_pst() - wait_start_time > \ datetime.timedelta(seconds=int(EnvUtil.get_pslx_env_variable('PSLX_QUEUE_TIMEOUT'))): break if not self.RESPONSE_MESSAGE_TYPE or self._response is None: return None else: return ProtoUtil.any_to_message( message_type=self.RESPONSE_MESSAGE_TYPE, any_message=self._response.response_data) except Exception as err: self._logger.error('Queue [' + self.get_queue_name() + "] send request with error " + str(err) + '.', publish=True) self._SYS_LOGGER.error('Queue [' + self.get_queue_name() + "] send request with error " + str(err) + '.')
def publish(self, message): self._emit_lock.acquire() try: message_str = ProtoUtil.message_to_string( proto_message=message ) self._channel.basic_publish( exchange=self._exchange_name, routing_key=self._topic_name, body=base64.b64encode(message_str), properties=pika.BasicProperties(delivery_mode=2) ) self._logger.info("Succeeded in publishing the data to exchange [" + self._exchange_name + "] with topic name [" + self._topic_name + '].') except Exception as err: self._logger.error("publish message failed with error " + str(err) + '.') finally: self._emit_lock.release()
def _process_message(self, ch, method, props, body): try: generic_request = ProtoUtil.string_to_message( message_type=GenericRPCRequest, string=base64.b64decode(body) ) self._logger.info("Getting request with uuid [" + generic_request.uuid + '] in consumer [' + self.get_consumer_name() + '].') response = self._queue.send_request(request=generic_request) response_str = ProtoUtil.message_to_string(proto_message=response) ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id=props.correlation_id), body=base64.b64encode(response_str)) ch.basic_ack(delivery_tag=method.delivery_tag) except Exception as err: self._logger.error("Consumer [" + self.get_consumer_name() + "] processing message with error: " + str(err) + '.')