RabbitMQ中assertQueue与sendToQueue方法的区别是什么?
Hey there! Let's break down what assertQueue and sendToQueue do in your RabbitMQ code, and clear up their key differences for you.
What is
assertQueue? Think of assertQueue as your "queue setup check"—it makes sure the queue you want to use exists and has the right properties:
- If the queue name you pass (like
'hello'in your code) doesn't exist on the RabbitMQ server yet, it will create the queue using the options you provide (here,{durable: false}means the queue won't survive a server restart). - If the queue already exists, it will verify that the existing queue's properties match what you're specifying. If there's a mismatch (for example, trying to assert a durable queue when the existing one is non-durable), it'll throw an error to alert you.
- You'll notice both your sender and receiver call
assertQueue—this is a best practice! It ensures both sides agree on the queue's configuration, so there's no confusion when sending or consuming messages.
What is
sendToQueue? sendToQueue is your message delivery workhorse—it's how you actually send messages to a queue:
- It takes two main things: the name of the queue you want to send to, and the message content (you're converting your string to a
Bufferhere because RabbitMQ expects message data in binary format). - Under the hood, this method uses RabbitMQ's default exchange to route the message directly to the queue with the matching name. No extra exchange setup needed for simple use cases like this.
- Once called, your message sits in the queue until a consumer (like your
receive.jsscript) comes along and picks it up.
Key Differences Between
assertQueue and sendToQueue Let's boil this down to the essentials:
- Core Purpose:
assertQueueis all about preparing the queue—it doesn't touch messages at all. It's about making sure the queue is ready to use.sendToQueueis about delivering messages—it can't create a reliable queue on its own (while RabbitMQ might auto-create a queue in some cases, this is risky because messages could be lost if no consumer is listening yet).
- Timing:
- Always call
assertQueuebeforesendToQueue(orconsume). Skipping this step can lead to lost messages if the queue doesn't exist when you try to send.
- Always call
- Server Impact:
assertQueueonly changes the server state if the queue needs to be created (or if properties don't match, which causes an error).sendToQueueadds a message to the queue's storage every time you run it—this is a direct, ongoing change to the server's message queue.
Your Example Code (Formatted for Clarity)
send.js
var amqp = require('amqplib/callback_api'); amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'hello'; var msg = 'Hello World! - '+i; ch.assertQueue(q, {durable: false}); ch.sendToQueue(q, new Buffer(msg)); console.log(" [x] Sent %s", msg); }); setTimeout(function() { conn.close(); process.exit(0) }, 1000); });
receive.js
var amqp = require('amqplib/callback_api'); amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'hello'; ch.assertQueue(q, {durable: false}); console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q); ch.consume(q, function(msg) { console.log(" [x] Received %s", msg.content.toString()); }, {noAck: true}); }); });
内容的提问来源于stack exchange,提问作者Harry
相关产品推荐
相关产品推荐

