1

SimpleMessageListenerContainer选项外,不会为临时队列创建消费者。我不会使用这里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)它正在工作。但它创建了一个持久队列。所以当我直接使用临时队列回复目的地时,它不起作用。

4

3 回答 3

0

ActiveMQTempQueue直接从该NMSReplyTo.ToString方法创建对象可能会在此处引起您的问题,因为该ToString方法不能保证返回一个可以从中创建匹配目标的值。由于您不知道发件人是否指定了临时目的地或普通目的地,因此它的编码也很糟糕。正确的做法是使用会话的创建消费者方法使用NSMReplyTo目标原样创建一个新消费者。

这是一个来自 NMS 项目的简单请求响应测试用例,它与 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);
                }
            }
        }
    }
}
于 2012-05-02T11:27:57.460 回答
0

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

于 2012-05-05T03:03:08.680 回答
0
  1. 与其使用 C#,不如用 java 编写代码,因为它是 ActiveMQ 的最佳套件。在此处阅读在 java 中使用临时队列的示例。
  2. 然后将其编译为 JAR 文件,您可以通过 IKVM.NET 将其导入您的 c# 代码中,如此处所述
  3. 希望它能与这个一起工作。

注意:你必须知道你不能在不同的会话中使用临时队列。

于 2012-05-10T11:58:32.627 回答