【问题标题】:Rebus: how to stop/start message processing without disposing bus instance?Rebus:如何在不处理总线实例的情况下停止/启动消息处理?
【发布时间】:2018-04-12 09:12:19
【问题描述】:

我有一种情况,我需要在我的将 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 我找不到合适的方法来实现这一点。

【问题讨论】:

    标签: azureservicebus rebus


    【解决方案1】:

    这很容易,当你知道它时?:你可以简单地将工作线程数设置为 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);
    

    ?

    【讨论】:

    • 谢谢一堆,正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2021-12-05
    • 2014-09-17
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多