我花了很多时间来寻找这样做。我在 python 中有一个示例代码,使用 pika lib 来展示如何使用传递模式发送消息,以防止在将消息发送到不存在的队列时等待响应(代理将忽略消息,因此不需要接收响应消息)
import pika
# Open a connection to RabbitMQ on localhost using all default parameters
connection = pika.BlockingConnection()
# Open the channel
channel = connection.channel()
# Declare the queue
channel.queue_declare(queue="test", durable=True, exclusive=False, auto_delete=False)
# Enabled delivery confirmations
channel.confirm_delivery()
# Send a message
if channel.basic_publish(exchange='test',
routing_key='test',
body='Hello World!',
properties=pika.BasicProperties(content_type='text/plain',
delivery_mode=1),
mandatory=True):
print('Message was published')
else:
print('Message was returned')
参考:
http: //pika.readthedocs.org/en/latest/examples/blocking_publish_mandatory.html