Reputation: 143
I have a requirement to be able to turn off/turn on subscribers. The idea being that messages can be sent but if a subscriber is turned off, they just don't process messages in their queue. Once turned back on, they begin processing messages in their queue.
I have looked at the pub/sub and request/reply examples, as well as pulled the source code for Rebus to look for any functionality that would answer this question. There is a Defer method where a delay in time can be applied to sending the message, but the delay I need is based upon turning a subscriber on and off.
Main question, is there some way of pausing a subscriber?
(just one subscriber, not all subscribers, so I don't believe the SetWorkerThreads(0) technique would work)
UPDATE The answer IS using _activator.Bus.Advanced.Workers.SetNumberOfWorkers(0)
like so:
public static class Subscriber1
{
private static BuiltinHandlerActivator _activator;
public static void Start(ILogger logger)
{
_activator = new BuiltinHandlerActivator();
_activator.Register(() => new Subscriber1MessageHandler());
Configure.With(_activator)
.Transport(t => t.UseMsmq("subscriber1"))
.Logging(l => l.Serilog(logger))
.Routing(r => r.TypeBased().Map<Subscriber1Message>("thepublisher"))
.Start();
_activator.Bus.Subscribe<Subscriber1Message>().Wait();
}
public static void Pause()
{
_activator.Bus.Advanced.Workers.SetNumberOfWorkers(0);
}
public static void Resume()
{
_activator.Bus.Advanced.Workers.SetNumberOfWorkers(1);
}
}
Upvotes: 2
Views: 104
Reputation: 18628
I don't understand why you say
just one subscriber, not all subscribers, so I don't believe the SetWorkerThreads(0) technique would work
because that would be my suggestion: To simply turn down the number of active worker threads to zero for that particular subscriber.
This way, it would stop receiving messages from its queue, and then message processing could be resumed by setting the number of workers back to some number greater than zero.
Btw. the syntax is like this:
bus.Advanced.Workers.SetNumberOfWorkers(0);
and it blocks until the number of workers has been adjusted.
Upvotes: 0