根据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 '/'")
- 鼠兔1.0.1
- RabbitMQ服务器3.7.4
- Erlang 20.3
- Python 3.6.5语言
1条答案
按热度按时间mgdq6dx11#
如果您使用Wireshark执行数据包捕获,它可以解释您看到的行为。从RabbitMQ发送到测试应用的
Channel.Close
是 * 异步的 *,因此第二个basic_publish
调用会在第一个Channel.Close
调用收到之前执行。RabbitMQ和Pika都没有bug。