I just started with the ActiveMQ thing and got several questions.
I should send messages using ActiveMQ
What I did for now :
public class ActiveMQSender
{
private readonly Uri connectionUri;
private readonly IConnectionFactory connectionFactory;
private readonly string destinationName;
public ActiveMQSender()
{
this.connectionUri = new Uri("activemq:tcp://localhost:61616");
this.connectionFactory = new NMSConnectionFactory(this.connectionUri);
this.destinationName = "queue://testQ";
}
public void Send(string msg)
{
using (var connection = this.connectionFactory.CreateConnection())
using (var session = connection.CreateSession())
{
var destination = SessionUtil.GetDestination(session, this.destinationName);
using (var producer = session.CreateProducer(destination))
{
connection.Start();
var message = session.CreateTextMessage(msg);
producer.Send(message);
}
}
}
}
There will be only one instance of this class which will be injected as a constructor parameter.
I am affraid of overhead for connection, session and producer creation, because messages will be sent frequently (often than one message per 10 seconds) Should I reuse connection, session or producer instances, and how should I react to the connection failures? What is the common pattern in such scenarios?