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.
    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?


回答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.



来源:https://stackoverflow.com/questions/34394435/activemq-do-i-need-to-re-subscribe-to-a-queue-after-the-listener-event-fires

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!