request response with activemq - always send double response.
- by Chris Valley
Hi,
I'm new at activeMq. I tried to create a simple request response like this.
public Listener(string destination)
{
// set factory
ConnectionFactory factory = new ConnectionFactory(URL);
IConnection connection;
try
{
connection = factory.CreateConnection();
connection.Start();
ISession session = connection.CreateSession();
// create consumer for designated destination
IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(destination));
consumer.Listener += new MessageListener(consumer_Listener);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw new Exception("Exception in Listening ", ex);
}
}
The OnMessage
static void consumer_Listener(IMessage message)
{
IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");
using (IConnection connection = factory.CreateConnection())
{
//Create the Session
using (ISession session = connection.CreateSession())
{
//Create the Producer for the topic/queue
// IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTempQueue(message.NMSDestination));
IMessageProducer producer = session.CreateProducer(message.NMSDestination);
// Create Response
// IMessage response = session.CreateMessage();
ITextMessage response = producer.CreateTextMessage("Replied from VS2010 Test");
//response.NMSReplyTo = new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("testQ1");
response.NMSCorrelationID = message.NMSCorrelationID;
if (message.NMSReplyTo != null)
{
producer.Send(message.NMSReplyTo, response);
Console.WriteLine("Receive: " + ((ITextMessage)message).NMSCorrelationID);
Console.WriteLine("Received from : " + message.NMSDestination.ToString());
Console.WriteLine("----------------------------------------------------");
}
}
}
}
Every time i tried to send a request to the listener, the response always send repeatedly. The first response will have NMSReplyTo properties while the other not.
My workaround to stop this situation by cheking the NMSReplyTo properties
if (message.NMSReplyTo != null)
{
producer.Send(message.NMSReplyTo, response);
Console.WriteLine("Receive: " + ((ITextMessage)message).NMSCorrelationID);
Console.WriteLine("Received from : " + message.NMSDestination.ToString());
Console.WriteLine("----------------------------------------------------");
}
In my understanding, this happened because there was a circular send response in the listener to the same Queue.
Could you guys help me how to fix this?
Many Thanks,
Chris