【问题标题】:ActiveMQ - Do I need to re-subscribe to a queue after the Listener event fires?ActiveMQ - 在侦听器事件触发后,我是否需要重新订阅队列?
【发布时间】:2016-03-27 10:53:46
【问题描述】:

我正在使用 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) 循环来保持侦听器处于活动状态?

【问题讨论】:

    标签: c# jms activemq apache-nms


    【解决方案1】:

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

    如果您编写了这样的代码,即消费者是一个成员变量,它被保存并仅在您想停止收听时关闭,那么您就不会遇到这个问题。 using 块本质上会处理您要求它管理的资源。

    【讨论】:

    • 谢谢。我已将其更改为将连接/会话和使用者作为实现 IDisposable 的接收器类的字段。我在处置期间清理了这些字段。这是否意味着 Listener 事件将在没有 while 循环的情况下为每条消息触发?
    • 如果你编码正确就是应该的,这就是使用异步侦听器模式的重点。最好的方法是询问计算机。
    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2020-09-06
    • 2016-02-10
    • 1970-01-01
    • 2015-12-18
    相关资源
    最近更新 更多