除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)它正在工作。但它创建了一个持久队列。因此,当我直接使用临时队列回复目的地时,它无法正常工作。
答案 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)
注意:您必须知道不能在不同的会话中使用temperory队列。