I'm migrating my Java application to .NET Core. In Java I was using Spring Boot and didn't care much about handling the opening and closing of sessions / consumers / etc (don't know if I should though n_n'). Migrating to .NET I'm having to do more hard work in managing these objects.
My scenario is simple:
Upon starting my application, I start a Connection
with a remote broker, a Session
, a Consumer
and register a MessageListener
to handle the messages.
The problem is my user wants to be able to active/deactive the listener and change the Queue Name whenever he wants to. Right know I'm only caching the Connection
object so upon receiving the "deactivate listener event" or "change queue name event" I can simply call connection.Dispose()
or connection.Close()
and re-start all the process of creating again. My doubts are:
.Close()
and .Dispose
interchangeable? Or do I need to call both?Connection
object? Will my session and consumer die as well or should I keep them cached and close altogether?Stop()
the connection instead of killing it and having to recreate it.Here's how I'm starting my listener:
_logger.LogWarning($"Starting ActiveMQ listener [{listener.Id}] ({ActiveMqHost})...");
_connection = _connectionFactory.CreateConnection(ActiveMqIntegrationsManager.ActiveMqUser, ActiveMqIntegrationsManager.ActiveMqPassword);
_connection.ClientId = $"{listener.Id}";
_connection.AcknowledgementMode = AcknowledgementMode.AutoAcknowledge;
var session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
var queue = session.GetQueue(queueName);
var consumer = session.CreateConsumer(queue, queueSelector);
consumer.Listener += handler.OnReceive;
_connection.ConnectionInterruptedListener += handler.OnInterrupt;
_connection.ExceptionListener += handler.OnException;
_connection.ConnectionResumedListener += handler.OnResume;
_connection.Start();
_logger.LogWarning($"[{listener.Id}] started successfully!");
You can interchange close and dispose calls or just use close and let the .NET runtime call dispose although not much really happens when a closed resource is disposed in the NMS client.
You can close a Connection instance and all its client resources will be closed implicitly so you don't need to micro manage them. Given you are stating that you need to change to listening on a different Queue then you do need to close down the consumer instance and create a new one. You don't need to close the Session or the Connection in that case since you will just turn around and create a new Connection and thus a new Session to manage the new MessageConsumer you would then be creating. However if you only want to stop deliveries for some time then a simple connection stop / start can accomplish that.
It all comes down to the amount of smarts you want to employ in your application. There is a significant performance overhead to creating and closing connections so if you can manage it then your application we be all the better for not needing to tear down and rebuild needlessly.