I have questions regarding lifecycles of various Rebus’s object related to application requests for Publisher and Subscriber:
Please see below:
Publisher
1 Must the code below be initialised once and only once for the lifetime of the app?
For example, instance of BuiltinHandlerActivator below is initialised once, and used throughout the lifecycle of the app?
using (var activator = new BuiltinHandlerActivator())
{
Log.Logger = new LoggerConfiguration()
.WriteTo.ColoredConsole()
.ReadFrom.Configuration(SerilogConfiguration)
.Enrich.WithProperty("App Name", "Rebus Publisher")
.CreateLogger();
Configure.With(activator)
.Logging(l =>
{
l.Serilog(Log.Logger);
})
.Transport(t => t.UseAzureServiceBus(Consts.ServiceBusConnectionString, Consts.Publisher))
.Options(o =>
{
o.Decorate<IErrorHandler>(c => new MyErrorHandler(c.Get<IErrorHandler>()));
o.SimpleRetryStrategy(maxDeliveryAttempts: 1, errorQueueAddress: "poison");
})
.Start();
2 Can multiple threads call and use activator
below concurrently?
For example, if multiple requests are coming in, Azure Function runtime creates multiple instance of Function below, each of which process the requests.
activator.Bus.Publish("test").Wait();
Subscriber1
Similar questions apply to the publisher above
1 Must the code below be initialised once and only once for the lifetime of the app? For example, instance of BuiltinHandlerActivator below is initialised once, and used throughout the lifecycle of the app?
using (var activator = new BuiltinHandlerActivator())
{
activator.Register(() => new Handler(MessageContext.Current));
Configure.With(activator)
.Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))
.Transport(t => t.UseAzureServiceBus(Consts.ServiceBusConnectionString, Consts.Subscriber1))
.Routing(r => r.TypeBased().MapAssemblyOf<string>(Consts.Publisher))
.Options(o =>
{
//consumer only
o.SimpleRetryStrategy(maxDeliveryAttempts: 2, errorQueueAddress: "poison");
//consumer only
o.SetNumberOfWorkers(7);
o.SetMaxParallelism(10);
}).Start();
2 Can multiple threads call and use activator below concurrently?
For example, if multiple requests are coming in, Azure Function runtime creates multiple instance of Function below, each of which process the requests.
Please note that both SetNumberOfWorkers and SetMaxParallelism are used for Subscriber1.
activator.Bus.Subscribe<string>().Wait();
The bus instance is fully re-entrant and thus safe to use across threads.
Generally, you should create the bus instance only once, and then either
BuiltinHandlerActivator
, orwhen the application shuts down.
If you're in an Azure Function, you should use a "one-way client", which is just another word for a bus instance that is not capable of receiving messages – it's only capable of sending/publishing. You can read about the two bus modes here: Different bus modes
You can read more about instance policy and thread safety here: Thread safety and instance policies.