服务端:
import functools import threading import traceback import pika class RabbitMQServer(object): def __init__(self,queue_name): self.queue_name = queue_name self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) def callback(self, ch, method, properties, body): self.connection.add_callback_threadsafe(functools.partial(self.work, ch, method, properties, body)) def work(self, channel,method, properties, body): try: print(f"received message {body}") response = "hello from server" channel.basic_publish(exchange='test', routing_key=properties.reply_to, properties=pika.BasicProperties(correlation_id=properties.correlation_id),body=response) channel.basic_ack(delivery_tag=method.delivery_tag) except Exception as e: traceback.print_exc() print("thread exeception",e) def consume(self): while True: try: channel = self.connection.channel() channel.queue_declare(queue=self.queue_name) channel.exchange_declare(exchange='test',exchange_type='topic') channel.queue_bind(exchange='test',queue=self.queue_name, routing_key=self.queue_name) channel.basic_consume(queue=self.queue_name, on_message_callback=self.callback,auto_ack=False) print(f"Server is listening for messages on queue: {self.queue_name}") channel.start_consuming() except Exception as e: traceback.print_exc() print("thread processing exception") channel.close() if __name__ == "__main__": queue_name = "server_queue" server = RabbitMQServer(queue_name) server.consume()
客户端:
import pika import uuid class RabbitMQClient: def __init__(self, server_queue): self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) self.channel = self.connection.channel() self.server_queue = server_queue result = self.channel.queue_declare(queue='',exclusive=True) self.callback_queue = result.method.queue self.channel.exchange_declare(exchange='test',exchange_type='topic') self.channel.queue_bind(exchange='test',queue=self.callback_queue,routing_key=self.callback_queue) self.channel.basic_consume(queue=self.callback_queue,on_message_callback=self.on_response, auto_ack=True) def on_response(self, ch, method,properpies,body): print("11111") if self.corrid == properpies.correlation_id: print("0000000") self.response = body print("22222") def publish(self,message): channel = self.connection.channel() channel.queue_declare(queue=self.server_queue) channel.exchange_declare(exchange='test', exchange_type='topic') channel.queue_bind(exchange='test',queue=self.server_queue, routing_key=self.server_queue) channel.confirm_delivery() self.response = None self.corrid = str(uuid.uuid4()) self.channel.basic_publish(exchange='test', routing_key=self.server_queue, properties=pika.BasicProperties( reply_to=self.callback_queue, correlation_id=self.corrid ),body=message) while self.response is None: self.connection.process_data_events() return self.response if __name__ == "__main__": server_queue = "server_queue" client = RabbitMQClient(server_queue) responsee = client.publish("hello from client") print(f"Received response: {responsee}")