ActiveMQ - Do I need to re-subscribe to a queue after the Listener event fires?

别说谁变了你拦得住时间么 提交于 2019-12-25 11:42:13


I am integrating with an ActiveMQ JMS system using the Apache.NMS library. For the response queue listener, it's not clear whether the consumer is disposed after a message is received.

Here are excerpts from the solution:

var destination = getDestination(session, Settings.Instance.OutboundQueue);
// Create a consumer and producer
using (var consumer = session.CreateConsumer(destination))
    // Start the connection so that messages will be processed.
    consumer.Listener += OnMessage;
    var receiveTimeout = TimeSpan.FromSeconds(Settings.Instance.Timeout);
    // Wait for the message
    semaphore.WaitOne((int)receiveTimeout.TotalMilliseconds, true);

// The Listener event
protected void OnMessage(IMessage receivedMsg)
    var message = receivedMsg as ITextMessage;
    // process the message 

Is the consumer durable?

Do you have to resubscribe after receiving a message?

Is this similar to other queuing/listener implementations (SQL Server service broker or the TCP/IP listener) where you need a while(true) loop to just keep the listener active?


Because of the way you've coded this, I believe (my .NET is rusty) you would need to create a new listener on each message as the using block will dispose of the consumer.

If you coded things such that the consumer was a member variable where it was saved away and only closed when you wanted to stop listening then you would not have this issue. The using block by it's nature will dispose of the resources that you ask it to manage.