除了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),它正在工作,但它会创建一个持久队列。所以当我直接使用临时队列回复目的地时,它不起作用。
- 与其使用 C#,不如用 java 编写代码,因为它是 ActiveMQ 的最佳套件。阅读此处,了解在 java 中使用临时队列的示例。
- 然后将其编译为 JAR 文件,您可以通过 IKVM.NET 将其导入 c# 代码中,如此处所述
- 希望它能解决这个问题。
注意:您必须知道,您不能在不同的会话中使用温和队列。
NMSReplyTo.ToString
方法创建 ActiveMQTempQueue
对象可能会在这里引起问题,因为 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);
}
}
}
}
}
临时队列仅在创建它的连接存在时才存在。 在您的示例代码中,您在开始连接之前创建它,所以我认为它只是静默地出错,因为没有活动连接。