def _run(self): # register queues and declare all of exchange and queue for item in self._rpc_class_list: item().declare() for (type, queue_name, exchange_name, routing_key, callback) in self.queue._rpc_class_list: if type == ExchangeType.DEFAULT: if not queue_name: # If queue name is empty, then declare a temporary queue queue_name = self.temporary_queue_declare() else: self._channel.queue_declare(queue=queue_name, auto_delete=True) self.basic_consuming(queue_name, callback) if type == ExchangeType.FANOUT or type == ExchangeType.DIRECT or type == ExchangeType.TOPIC: if not queue_name: # If queue name is empty, then declare a temporary queue queue_name = self.temporary_queue_declare() else: self._channel.queue_declare(queue=queue_name) self.exchange_bind_to_queue(type, exchange_name, routing_key, queue_name) # Consume the queue self.basic_consuming(queue_name, callback) logger.info(" * The flask RabbitMQ application is consuming") t = threading.Thread(target = self.consuming) t.start()
def send_sync(self, body, exchange, key): """ 发送并同步接受回复消息 :return: """ callback_queue = self.declare_queue(exclusive=True, auto_delete=True) # 得到随机回调队列名 self._channel.basic_consume( self.on_response, # 客户端消费回调队列 no_ack=True, queue=callback_queue) corr_id = str(uuid.uuid4()) # 生成客户端请求id self.data[corr_id] = { 'isAccept': False, 'result': None, 'callbackQueue': callback_queue } self._channel.basic_publish( # 发送数据给服务端 exchange=exchange, routing_key=key, body=body, properties=pika.BasicProperties( reply_to=callback_queue, correlation_id=corr_id, )) while not self.data[corr_id]['isAccept']: # 判断是否接收到服务端返回的消息 self._connection.process_data_events() time.sleep(0.3) continue logger.info("Got the RPC server response => {}".format( self.data[corr_id]['result'])) return self.data[corr_id]['result']
def on_response(self, ch, method, props, body): """ 所有的RPC请求回调都会调用该方法,在该方法内修改对应corr_id已经接受消息的isAccept值和返回结果 """ logger.info("on response => {}".format(body)) corr_id = props.correlation_id # 从props得到服务端返回的客户度传入的corr_id值 self.accept(corr_id, body)
def run(self): # 进行注册和声明 for item in self._rpc_class_list: item().declare() for (type, queue_name, exchange_name, routing_key, callback) in self.queue._rpc_class_list: if type == ExchangeType.DEFAULT: self.declare_default_consuming(queue_name=queue_name, callback=callback) if type == ExchangeType.TOPIC: self.declare_consuming(queue_name=queue_name, exchange_name=exchange_name, routing_key=routing_key, callback=callback) logger.info("consuming...") t = threading.Thread(target=self.consuming) t.start()
def _run(self): # register queues and declare all of exchange and queue for item in self._rpc_class_list: item().declare() for (type, queue_name, exchange_name, routing_key, callback) in self.queue._rpc_class_list: if type == ExchangeType.DEFAULT: self.declare_default_consuming(queue_name=queue_name, callback=callback) if type == ExchangeType.TOPIC: self.declare_consuming(queue_name=queue_name, exchange_name=exchange_name, routing_key=routing_key, callback=callback) logger.info(" * The flask RabbitMQ application is consuming") t = threading.Thread(target=self.consuming) t.start()
def __call__(self, queue_name, type='default', exchange_name=None, routing_key=None): """ 当Queue对象被调用时,如@queue()执行的操作 :param queue_name: :param type: 交换机的类型 :param exchange_name: :param routing_key: :return: """ logger.info("Queue callback called") def _(func): self._rpc_class_list.append( (type, queue_name, exchange_name, routing_key, func)) return _
def send_sync(self, body, key=None, timeout=5): if not key: raise Exception("The routing key is not present.") corr_id = str(uuid.uuid4()) # generate correlation id callback_queue = self.temporary_queue_declare() # 得到随机回调队列名 self.data[corr_id] = { 'isAccept': False, 'result': None, 'reply_queue_name': callback_queue } # Client consume reply_queue # self._channel.basic_consume(self.on_response, # no_ack=True, # queue=callback_queue) # update for python3.6 basic_consumer args order self._channel.basic_consume(callback_queue, self.on_response, no_ack=True) # send message to queue that server is consuming self._channel.basic_publish( exchange='', routing_key=key, body=body, properties=pika.BasicProperties( reply_to=callback_queue, correlation_id=corr_id, ) ) end = time.time() + timeout while time.time() < end: if self.data[corr_id]['isAccept']: # 判断是否接收到服务端返回的消息 logger.info("Got the RPC server response => {}".format(self.data[corr_id]['result'])) return self.data[corr_id]['result'] else: self._connection.process_data_events() time.sleep(0.3) continue # 超时处理 logger.error("Get the response timeout.") return None