根据https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish,发布时,
如果指定了交换名称,但该交换不存在,则服务器将引发通道异常。
使用pika,只会在第二次将消息发送到同一通道上丢失的交易所时出现:
(env36x64) C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.4\sbin>rabbitmqctl.bat list_exchanges name
Listing exchanges for vhost / ...
amq.fanout
amq.match
amq.headers
amq.rabbitmq.trace
amq.topic
amq.direct
(env36x64) C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.4\sbin>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pika
>>> creds = pika.PlainCredentials('user', 'passw')
>>> params = pika.ConnectionParameters(host='ms7', port=5672, credentials=creds)
>>> conn = pika.BlockingConnection(params)
>>> ch = conn.channel()
>>> ch.basic_publish(exchange='invalid', routing_key='', body='hello')
>>> # No error. Not expected
>>> # Second attempt does raise exception, as expected
>>> ch.basic_publish(exchange='invalid', routing_key='', body='hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Administrator\Documents\env36x64\lib\site-packages\pika\adapters\blocking_connection.py", line 2239, in basic_publish
self._flush_output()
File "C:\Users\Administrator\Documents\env36x64\lib\site-packages\pika\adapters\blocking_connection.py", line 1331, in _flush_output
raise self._closing_reason # pylint: disable=E0702
pika.exceptions.ChannelClosedByBroker: (404, "NOT_FOUND - no exchange 'invalid' in vhost '/'")
答案 0 :(得分:1)
如果使用Wireshark进行数据包捕获,它将说明您所看到的行为。从RabbitMQ发送到您的测试应用的Channel.Close
是异步,因此您的第二个basic_publish
调用会在收到第一个Channel.Close
之前执行。 RabbitMQ或Pika中都没有错误。
注意: RabbitMQ团队监视rabbitmq-users
mailing list,并且有时仅在StackOverflow上回答问题。