0

我正在使用 Apache.NMS 库与 ActiveMQ JMS 系统集成。对于响应队列监听器,消费者是否在收到消息后被释放是不清楚的。

以下是解决方案的摘录:

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 
}

消费者耐用吗?

收到消息后是否需要重新订阅?

这是否类似于其他队列/侦听器实现(SQL Server 服务代理或 TCP/IP 侦听器),您需要一个 while(true) 循环来保持侦听器处于活动状态?

4

1 回答 1

1

由于您编写此代码的方式,我相信(我的 .NET 生锈了)您需要在每条消息上创建一个新的侦听器,因为 using 块将处理消费者。

如果您对事物进行编码,使得消费者是一个成员变量,它被保存起来并且仅在您想停止收听时关闭,那么您就不会遇到这个问题。using 块本质上将处理您要求它管理的资源。

于 2015-12-21T14:05:33.147 回答