问题
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.
回答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.
- And then Compile it to a JAR file and you can import it in your c# code via IKVM.NET as described here
- Hope it will work with this.
Note: You must know that you can not use temperory queue in different session.
回答2:
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);
}
}
}
}
}
回答3:
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.
来源:https://stackoverflow.com/questions/10409242/why-is-consumer-not-created-for-activemq-temp-queue