Amqplib的sendtoqueue()即使队列确实存在

发布于 2025-01-20 10:52:27 字数 695 浏览 6 评论 0原文

”的队列

名为“ Test_queue

我用Amqplib创建了一个 !'

也没有错误

即使名为“ Test_queue”的队列不存在如何在队列不存在时如何获得错误,

const amqp = require('amqplib');

const sendMsg=async ()=>{

    const connection = await amqp.connect('amqp://localhost');
    const ch = await connection.createConfirmChannel()  

    const msg= 'hello world'
    const QUEUE_NAME = 'test_queue'

    ch.sendToQueue(QUEUE_NAME, Buffer.from(msg),{},function(err, ok) {
        if (err !== null) {
            console.log(err);
        } else {
            console.log('sent message successfully!');
        }
    })

    // await ch.close();
    // await connection.close();
}

sendMsg();

I created a queue named 'test_queue' with amqplib

then I deleted the 'test_queue' from the admin page of rabbitmq (http://localhost:15672/#/queues)

but when I excute the following code, it shows 'sent message successfully!'

there is no error even if the queue named 'test_queue' does't exist

How to get an error when queue doesn't exist?

Thanks for any help!

const amqp = require('amqplib');

const sendMsg=async ()=>{

    const connection = await amqp.connect('amqp://localhost');
    const ch = await connection.createConfirmChannel()  

    const msg= 'hello world'
    const QUEUE_NAME = 'test_queue'

    ch.sendToQueue(QUEUE_NAME, Buffer.from(msg),{},function(err, ok) {
        if (err !== null) {
            console.log(err);
        } else {
            console.log('sent message successfully!');
        }
    })

    // await ch.close();
    // await connection.close();
}

sendMsg();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-01-27 10:52:27

如果将强制性标志添加到SendToqueue,则任何无法路由到队列的消息都将发送回发布者。因此,从您的示例中,您需要:

ch.sendToQueue(QUEUE_NAME, Buffer.from(msg),{mandatory: true},function(err, ok)

然后,您可以使用相同的频道对象处理返回:

ch.on('return', (message) => {
    console.log(`Unable to route message to ${QUEUE_NAME}`)
}

If you add the mandatory flag to sendToQueue any message which cannot be routed to a queue will be sent back to the publisher. So from your example you would need:

ch.sendToQueue(QUEUE_NAME, Buffer.from(msg),{mandatory: true},function(err, ok)

You can then handle the return using the same channel object:

ch.on('return', (message) => {
    console.log(`Unable to route message to ${QUEUE_NAME}`)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文