【问题标题】:Iterate over non-persistent activemq expired messages in ActiveMQ.Advisory.Expired.Queue在 ActiveMQ.Advisory.Expired.Queue 中迭代非持久的 activemq 过期消息
【发布时间】:2017-01-30 12:36:40
【问题描述】:

我在 activemq 上构建一个应用程序,我从生产者那里发送一条消息,我的交付模式是 NON_PERSISTENT(我没有处理 PERSISTENT 交付模式,我知道它将存储在 DLQ 中——这不是我的设计) 并使用 producer.setTimeToLive(2000) 设置消息的生存时间。正如功能所说,消息将在 2 秒后过期。

我看到过期的消息在activeMQ管理控制台主题部分的ActiveMQ.Advisory.Expired.Queue中排队,即http://localhost:8161/admin/topics.jsp

我的问题是如何遍历 ActiveMQ.Advisory.Expired.Queue 以便我可以访问过期消息的 MessageID。任何代码示例都是很棒。

【问题讨论】:

    标签: java jms activemq


    【解决方案1】:

    订阅目标ActiveMQ.Advisory.Expired.Queue 就像任何主题一样,它会返回一个ActiveMQMessage。 通过 ActiveMQMessage 的 getDataStructure 方法可以获取 DataStructure 对象(ConsumerInfo、ProducerInfo、ConnectionInfo...)。

    文档http://activemq.apache.org/advisory-message.html

    示例:

    Destination advisoryDestination = AdvisorySupport.getExpiredQueueMessageAdvisoryTopic(destination)
    MessageConsumer consumer = session.createConsumer(advisoryDestination);
    consumer.setMessageListener(this);
    
    public void onMessage(Message msg){
        String messageId =   msg.getJMSMessageID();
        String orignalMessageId =   msg.getStringProperty(org.apache.activemq.advisory.AdvisorySupport.MSG_PROPERTY_MESSAGE_ID);
        if (msg instanceof ActiveMQMessage){
            try {
                 ActiveMQMessage aMsg =  (ActiveMQMessage)msg;
                 ProducerInfo prod = (ProducerInfo) aMsg.getDataStructure();
            } catch (JMSException e) {
                log.error("Failed to process message: " + msg);
            }
        }
    }
    

    【讨论】:

    • @Hassen Bennour 通过使用上面的代码,我可以接收消息监听器启动后过期的消息。我无法接收驻留在 DLQ 中的以前过期的消息。如何迭代并获取先前过期的消息?出于我的考虑,我也需要这些信息。
    • 您可以尝试在您的主题中添加此策略以使用队列而不是主题作为DLQ,这样消息在没有消费者在线的情况下不会丢失,关键是useQueueForTopicMessages <destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=">" > <deadLetterStrategy> <individualDeadLetterStrategy processNonPersistent="true" processExpired="true" useQueueForTopicMessages="true" /> </deadLetterStrategy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>
    • 请注意,通过使用 useQueueForTopicMessages,此用例无法获得答案代码,您需要像任何队列一样从 DLQ 队列中消费。
    猜你喜欢
    • 2018-09-22
    • 2015-08-22
    • 2019-02-14
    • 1970-01-01
    • 2011-10-12
    • 2016-12-30
    • 2019-09-06
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多