azureservicebus - Rebus:如何在不处理总线实例的情况下停止/启动消息处理?

标签 azureservicebus rebus

我遇到这样一种情况,我需要在使用 Rebus 和 Azure 服务总线的应用程序实例中暂停消息处理。

应用程序的生命周期在应用程序启动时配置 IoC 容器,然后在应用程序准备好开始处理消息时配置并启动 Rebus,这会自动将 Rebus 及其依赖项添加到 IoC 容器中。

在应用程序生命周期的后期,它可能需要暂停消息处理,然后再次开始消息处理,而无需重新启动应用程序。

在我当前的实验中,我尝试在需要暂停消息处理时处置 IBus,但我不处置 IoC 容器 (Windsor),因为其他组件在暂停期间需要它。我得到的结果是 InvalidOperationException

System.InvalidOperationException 
Attempted to register primary -> Rebus.Config.Options, but a primary registration already exists: primary -> Rebus.Config.Options
   at Rebus.Injection.Injectionist.Register[TService](Func`2 resolverMethod, Boolean isDecorator, String description)
   at Rebus.Config.RebusConfigurer.Start()

重新配置并再次启动总线时,会重用相同的容器实例。由于一些 Rebus 依赖项已经在容器中注册,因此似乎会发生异常。所以我需要一些其他方法来停止消息处理而不处理 IBus 实例。

是否可以在应用程序的生命周期内暂停/启动消息处理?查看 IBus api 或 IAdvancedApi,我找不到合适的方法来实现这一目标。

最佳答案

这很容易,当你知道它时:你可以简单地将工作线程数设置为 0,这将停止消息处理:

// blocks until all currently executing message handlers have finished
// what they're currently up to
bus.Advanced.Workers.SetNumberOfWorkers(0);

然后您通过将它们添加回来再次恢复消息处理:

// you should probably get the number from a configuration
// value somewhere
bus.Advanced.Workers.SetNumberOfWorkers(3);

👍

关于azureservicebus - Rebus:如何在不处理总线实例的情况下停止/启动消息处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792550/

相关文章:

c# - Rebus:2 个进程中的 2 个处理程序。击球不一致且交替

rebus - 使用 T-SQL 将 Rebus 消息插入 SQL Server

Rebus OneWayClient 如果不存在则创建队列

c# - Windows 服务使用队列中的 Azure 服务总线消息

java - 使用 Spring Boot 和 AMQP 订阅 Azure 服务总线主题

c# - 服务总线 1.1 使用 WindowsAzure.ServiceBus dll 创建队列

rebus - 如何从 Rebus 检查错误队列

azureservicebus - 无法使用 Azure 服务总线和 rebus 进行发布订阅

message - Microsoft Azure 服务总线消息已完成

azure - 导出 Azure 服务总线配置导出