【发布时间】:2017-10-13 04:09:27
【问题描述】:
我正在用 Kombu 实现一个 rpc 框架,以便与 rabbit 一起使用 我想使用rabbitmq's direct reply-to feature,但我找不到用kombu 实现它的方法。
客户端需要在生成消息之前在“amq.rabbitmq.reply-to”队列上消费,并且生产者和消费者应该使用相同的通道。 我还需要使用生产者池(或某种连接池),因为客户端是在线程环境中创建的。
到目前为止,我有这段代码,rabbitmq 不会抱怨 no producer with PRECONDITION 错误(如果我删除消费者部分,它确实会抱怨),但生产者不会产生任何东西!
class KombuRpcClient(RpcClientBase):
def __init__(self, params):
self.future = Queue.Queue()
self.logger = logger
if isinstance(params, RpcConnectionProperties):
self.rpc_connection_properties = params
else:
self.rpc_connection_properties = RpcConnectionProperties(
host=params.get('host'),
port=5672,
username=params.get('username'),
password=params.get('password'),
vhost=params.get('vhost') if params.has_key('vhost') else '/'
)
self.amqp_url = self.rpc_connection_properties.get_kombu_transport_url('pyamqp')
self.reply_queue = KombuQueue('direct_reply', exchange=default_exchange, routing_key='amq.rabbitmq.reply-to')
def call(self, exchange, key, msg, no_response=False, timeout=5):
connection = Connection(self.amqp_url)
if exchange is not None:
key = exchange + ':' + key
with producers_pool[connection].acquire(block=True) as producer:
with producer.channel.Consumer(queues=[self.reply_queue], no_ack=True, callbacks=[self._on_message],
accept=['ujson']) as consumer:
producer.publish(
msg,
exchange=default_exchange,
routing_key=key,
immediate=True,
serializer='ujson', reply_to=self.reply_queue.routing_key)
consumer.consume()
pass
res = self.future.get(block=True, timeout=timeout)
print res
def cast(self, exchange, key, msg):
pass
def _on_message(self, body, message):
print body
self.future.put(body)
【问题讨论】: