在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
1条答案
按热度按时间bbuxkriu1#
我还应该为上面的问题提供连接和通道示例。
解决方案是使用
createConfirmChannel
而不是createChannel
。createConfirmChannel
返回的Channel
提供了一个waitForConfirms
方法,该方法等待confirm
服务器接收到所有消息。更深入的解释: