应用程序异常关闭时删除RabbitMQ Queue

Delete RabbitMQ Queue when Application is closed abnormally

我正在使用 .NET RabbitMQ Client 库。我想开发一个消息系统。 为此,我已经实现了 RabbitMQ 的所有必要方法。此外,我开发了 dispose() 用于删除队列。我在 FormClosing 事件中调用此方法,因此当用户通过单击关闭按钮关闭应用程序时将触发此方法,逻辑队列将被删除。到目前为止,所有代码都运行良好。

但我的问题是

I'm not able to delete a queue at the time of closing the application without clicking on close button (let's just say closing application from task manager or from command prompt using taskkill command or any abnormally reasons) as this dispose() will not be triggered. At this time, queue will not be deleted until I delete it from management portal manually.

所以我的问题是,

How can I know that the application is closed and that orphan queue can be deleted?

RabbitMQ 公开基于 REST 的管理API,使您能够处理队列创建/删除。

你可以做的是让你的应用程序在启动时查询管理 API 以查看是否有任何队列因为之前的意外关闭而被遗弃,或者你可以有一个完全不同的服务来负责那(这是一个设计选择)。

您可以通过EasyNetQ Management API查询您的管理API:

managementClient.DeleteQueue(queue);

您可以找到完整的文档 here

编辑:

读完之后,也许排他或自动删除队列就足够了?

Exclusive (used by only one connection and the queue will be deleted when that connection closes)

Auto-delete (queue is deleted when last consumer unsubscribes)