0

I am writing a wrapper around EasyNetQ to publish messages to RabbitMQ. There are large number of messages that needs to be published. Should I use using pattern to publish message using IBus or should I declare IBus type var as a class level static var and dispose when application ends?

using (IBus bus = RabbitHutch.CreateBus(rabbitMQConnectionString))
{
 bus.publish("ss");
}

Thanks

Nisha Kant
  • 25
  • 3
  • This is primarily an opinion based question and I would say there is no right answer and depends on the context. Why not let the application choose whether to create a static bus or instantiate it otherwise? – Alen Genzić Feb 21 '17 at 15:07
  • There are large number of messages that needs to be published. I am mainly concerned about creating and disposing IBus instance to many times. On the other hand if I make IBus type var as static class level member, I am worried about concurrency issue. I can apply lock but that will impact performance – Nisha Kant Feb 21 '17 at 15:16
  • Why does concurrency worry you with a message queue in place? A message from a queue is de-queued only once by the subscribing application. Locking the bus instance wouldn't prevent the application to send the same message multiple times anyway. – Alen Genzić Feb 21 '17 at 15:25

1 Answers1

2

You should create a single IBus instance for your application and dispose when the application ends. It is meant to create a single connection to RabbitMQ and is thread-safe.

Wiebe Tijsma
  • 10,173
  • 5
  • 52
  • 68