request response with activemq - always send double response.

Posted by Chris Valley on Stack Overflow See other posts from Stack Overflow or by Chris Valley
Published on 2011-01-05T08:42:47Z Indexed on 2011/01/05 8:54 UTC
Read the original article Hit count: 579

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about activemq