1

Except SimpleMessageListenerContainer option, the consumer is not created for temp queue. I will not use SimpleMessageListenerContainer for some issues faced here.

Following code is not working...(even the temp queue is not created)

                        using (IConnection connection = connectionFactory.CreateConnection())
                    using (ISession session = connection.CreateSession())
                    {
                        IDestination destination = SessionUtil.GetDestination(session, aQueueName);
                        var replyDestination = session.CreateTemporaryQueue();

                        // Create a consumer and producer
                        using (IMessageProducer producer = session.CreateProducer(destination))
                        {
                            // Start the connection so that messages will be processed.
                            connection.Start();

                            IBytesMessage request = session.CreateBytesMessage(aMsg);
                            request.NMSReplyTo = replyDestination;

                            IMessageConsumer consumer = session.CreateConsumer(replyDestination);
                            consumer.Listener += new MessageListener(this.OnAckRecieved);

                            // Send a message
                            producer.Send(request);
                            ack = this.autoEvent.WaitOne(this.msgConsumeTimeOut, true);

                            consumer.Close();
                            consumer.Dispose();
                            ConnectionFactoryUtils.GetTargetSession(session).DeleteDestination(replyDestination);
                        }
                        connection.Close();
                        session.Close();

Flollowing code is working:-but the queue seems to be a persistent queue not a temp queue

                        using (IConnection connection = connectionFactory.CreateConnection())
                    using (ISession session = connection.CreateSession())
                    {
                        IDestination destination = SessionUtil.GetDestination(session, aQueueName);
                        var replyDestination = session.CreateTemporaryQueue();

                        // Create a consumer and producer
                        using (IMessageProducer producer = session.CreateProducer(destination))
                        {
                            // Start the connection so that messages will be processed.
                            connection.Start();

                            IBytesMessage request = session.CreateBytesMessage(aMsg);
                            request.NMSReplyTo = replyDestination;

                            IDestination tempDestination = this.destinationResolver.ResolveDestinationName(session, request.NMSReplyTo.ToString());
                            IMessageConsumer consumer = session.CreateConsumer(tempDestination);
                            consumer.Listener += new MessageListener(this.OnAckRecieved);

                            // Send a message
                            producer.Send(request);
                            ack = this.autoEvent.WaitOne(this.msgConsumeTimeOut, true);

                            consumer.Close();
                            consumer.Dispose();
                            ConnectionFactoryUtils.GetTargetSession(session).DeleteDestination(tempDestination);
                        }
                        connection.Close();
                        session.Close();

With the above code(with use of NmsDestinationAccessor) it is working.but it creates a persistent queue. So when i use the temp queue reply destination directly,it is not working.

Community
  • 1
  • 1
Tamilmaran
  • 1,257
  • 1
  • 10
  • 21
  • what exactly do you mean by "not created", does CreateConsumer() throw any exception or just returns null? – Peter Zajic May 02 '12 at 07:28
  • There is no error at all. When i see on webconsole,Even the temp queue is not created for 2nd code.For 3rd code,only the consumer is not created. – Tamilmaran May 02 '12 at 07:50
  • Added an example NUnit test from the NMS project to show it in action. – Tim Bish May 04 '12 at 20:41

3 Answers3

0

Creating the ActiveMQTempQueue object directly from the NMSReplyTo.ToString method is probably causing you issues here as the ToString method is not guaranteed to return a value from which a matching destination can be created. Since you don't know whether the sender has specified a temp destination or a normal one its bad coding as well. The correct thing to do is simply create a new consumer using the session's create consumer method using the NSMReplyTo destination as is.

Here is a simple Request response test case from the NMS project that works with Apache.NMS.Stomp and Apache.NMS.ActiveMQ.

namespace Apache.NMS.Test
{
[TestFixture]
public class RequestResponseTest : NMSTestSupport
{
    protected static string DESTINATION_NAME = "RequestDestination";

    [Test]
    [Category("RequestResponse")]       
    public void TestRequestResponseMessaging()
    {
        using(IConnection connection = CreateConnection())
        {
            connection.Start();
            using(ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
            {
                IDestination destination = SessionUtil.GetDestination(session, DESTINATION_NAME);
                ITemporaryQueue replyTo = session.CreateTemporaryQueue();

                using(IMessageConsumer consumer = session.CreateConsumer(destination))
                using(IMessageProducer producer = session.CreateProducer(destination))
                {
                    IMessage request = session.CreateMessage();

                    request.NMSReplyTo = replyTo;

                    producer.Send(request);

                    request = consumer.Receive(TimeSpan.FromMilliseconds(3000));
                    Assert.IsNotNull(request);
                    Assert.IsNotNull(request.NMSReplyTo);

                    using(IMessageProducer responder = session.CreateProducer(request.NMSReplyTo))
                    {
                        IMessage response = session.CreateTextMessage("RESPONSE");                          
                        responder.Send(response);
                    }                       
                }

                using(IMessageConsumer consumer = session.CreateConsumer(replyTo))
                {
                    ITextMessage response = consumer.Receive(TimeSpan.FromMilliseconds(3000)) as ITextMessage;
                    Assert.IsNotNull(response);
                    Assert.AreEqual("RESPONSE", response.Text);
                }
            }
        }
    }
}
Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • Unable to say for sure without the complete code, the third code would create a consumer and then immediately after the using block the consumer would be disposed which would unsubscribe it from the broker. – Tim Bish May 02 '12 at 21:50
  • Differences from mine: 1)the connection.Start() is put before creating the consumer.2)connection.createSession is put before connection.Start(). could it be the reason? – Tamilmaran May 07 '12 at 15:10
  • the only thing connection.Start does is to tigger dispatching of messages to consumers. If things aren't working for you then the best thing to do is to create an NUnit test case and attach it to a new Jira issue at apache. – Tim Bish May 07 '12 at 17:47
0

A Temporary Queue only exists only while the connection that created it exists. In your sample code you are creating it before starting the connection, so I think its just erroring out silently because there is no active connection.

Thymine
  • 8,775
  • 2
  • 35
  • 47
  • Its legal to create them before calling start, that's the intention of the start method. You would normally create all destinations, producers and consumers prior to calling start. – Tim Bish May 05 '12 at 11:02
  • Okay, I haven't used temp queues and he said the temp queues were not being created, so I just guessed that might be the issue. In your test case they are created after the connection exists so thought that might be the key difference. – Thymine May 05 '12 at 19:58
0
  1. Rather than using C#, write the code in java as it is a best suite for ActiveMQ. Read here for examples using temp queue in java.
  2. And then Compile it to a JAR file and you can import it in your c# code via IKVM.NET as described here
  3. Hope it will work with this.

Note: You must know that you can not use temperory queue in different session.

Community
  • 1
  • 1
maxpayne
  • 122
  • 5