rabbitmq 如何发布和断开连接,而不必求助于setTimeout?

ivqmmu1c  于 2023-04-20  发布在  RabbitMQ
关注(0)|答案(1)|浏览(151)

在RabbitMQ的文档中,他们给予了一个使用setTimeout向交换机发布消息,然后在500延迟后关闭连接的例子。我决定跳过setTimeout,但是如果我在运行publish方法后立即关闭连接,那么我的消息不会被发送到RabbitMQ服务器。想象一下:

channel.publish('MyExchange', 'MyRoutingKey', Buffer.from('{}'));
await channel.close();
await connection.close();

有没有比设置setTimeout更好的方法在发送消息后关闭连接?
Putting it all together部分的文档链接:https://www.rabbitmq.com/tutorials/tutorial-five-javascript.html

bbuxkriu

bbuxkriu1#

我还应该为上面的问题提供连接和通道示例。
解决方案是使用createConfirmChannel而不是createChannelcreateConfirmChannel返回的Channel提供了一个waitForConfirms方法,该方法等待confirm服务器接收到所有消息。
更深入的解释:

相关问题