0

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.
    connection.Start();
    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;
    semaphore.Set();
    // 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?

reckface
  • 5,678
  • 4
  • 36
  • 62

1 Answers1

1

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.

Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • Thanks. I've changed it to hold the connection/session and consumer as fields of my receiver class which implements IDisposable. I clean up these fields during dispose. Does this mean the Listener event will fire for each message without a while loop? – reckface Dec 21 '15 at 14:56
  • If you code it correctly is should, that is the whole point of using the async listener pattern. Best way to know is to ask the computer. – Tim Bish Dec 21 '15 at 15:39