为什么不为ActiveMQ Temp Queue创建消费者?

时间:2012-05-02 07:11:06

标签: c# activemq apache-nms

SimpleMessageListenerContainer选项外,不为临时队列创建使用者。 对于here.

面临的一些问题,我不会使用SimpleMessageListenerContainer

以下代码无效...(即使未创建临时队列)

                        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();

以下代码正在运行: - 但队列似乎是一个持久队列而不是临时队列

                        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();

使用上面的代码(使用NmsDestinationAccessor)它正在工作。但它创建了一个持久队列。因此,当我直接使用临时队列回复目的地时,它无法正常工作。

3 个答案:

答案 0 :(得分:0)

直接从ActiveMQTempQueue方法创建NMSReplyTo.ToString对象可能会导致您遇到问题,因为ToString方法无法保证返回可以创建匹配目标的值。由于您不知道发送方是指定了临时目的地还是普通目的地,因此也会编写错误的编码。正确的做法是使用会话的创建消费者方法创建一个新的消费者,使用NSMReplyTo目的地。

以下是来自NMS项目的一个简单的Request响应测试用例,它与Apache.NMS.Stomp和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);
                }
            }
        }
    }
}

答案 1 :(得分:0)

临时队列仅在创建它的连接存在时才存在。在您的示例代码中,您在开始连接之前创建它,因此我认为它只是默认错误,因为没有活动连接。

答案 2 :(得分:0)

  1. 不是使用C#,而是在java中编写代码,因为它是ActiveMQ的最佳套件。 阅读here for examples using temp queue in java.
  2. 然后将其编译为JAR文件,您可以通过IKVM.NET将其导入c#代码中,如here所述
  3. 希望它能与之合作。
  4. 注意:您必须知道不能在不同的会话中使用temperory队列。