【问题标题】:Always ensure only last 10 messages in ActiveMQ Topic始终确保 ActiveMQ 主题中只有最后 10 条消息
【发布时间】:2011-01-14 07:35:44
【问题描述】:

我们在 ActiveMQ 中遇到了一个问题,我们有大量消息没有丢弃主题。 主题设置为非持久性、非持久性。 我们的 Activemq.xml 文件是

<beans>

  <broker xmlns="http://activemq.apache.org/schema/core" useJmx="false" persistent="false">

<!--
    <persistenceAdapter>
      <journaledJDBC journalLogFiles="5" dataDirectory="../data"/>
    </persistenceAdapter>
-->

        <transportConnectors>
            <transportConnector uri="vm://localhost"/>
        </transportConnectors>

  </broker>

</beans>

而我们在messaging-config.xml中的主题定义是

<destination id="traceChannel">

    <properties>

        <network>
        <session-timeout>10</session-timeout>
    </network>

        <server>
            <message-time-to-live>10000</message-time-to-live>
            <durable>false</durable>
            <durable-store-manager>flex.messaging.durability.FileStoreManager</durable-store-manager>
        </server>

        <jms>
            <destination-type>Topic</destination-type>
            <message-type>javax.jms.ObjectMessage</message-type>
            <connection-factory>ConnectionFactory</connection-factory>
            <destination-jndi-name>dynamicTopics/traceTopic</destination-jndi-name>
            <delivery-mode>NON_PERSISTENT</delivery-mode>
            <message-priority>DEFAULT_PRIORITY</message-priority>
            <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
            <transacted-sessions>false</transacted-sessions>
            <initial-context-environment>
                <property>
                    <name>Context.INITIAL_CONTEXT_FACTORY</name>
                    <value>org.apache.activemq.jndi.ActiveMQInitialContextFactory</value>
                </property>
                <property>
                    <name>Context.PROVIDER_URL</name>
                    <value>tcp://localhost:61616</value>
                </property>
            </initial-context-environment>
        </jms>
    </properties>

    <channels>
        <channel ref="rtmps" />
    </channels>

    <adapter ref="trace" />

</destination>

我想要实现的是,任何时候只有最后 10 条消息在主题上,因为让它在一夜之间运行会导致超过 15 万条关于主题的消息,即使它应该只包含非常小的数量。

【问题讨论】:

    标签: java jms messaging activemq


    【解决方案1】:

    这篇文章中有些事情不太清楚

    • 根据我的解析和尝试添加这个.. 它需要更多的上下文,所以我在下面为那些可能不想经历 ActiveMQ 谷歌斗争的人添加了它..
    • 这是我的 activemq.xml 中的内容,我根本没有看到这项工作,所以我希望我能从其他人那里获得一些见解,他们可能能够为我指明正确的方向.

      <destinationPolicy>
        <policyMap>
          <policyEntries>
            <policyEntry topic=">"
                         topicPrefetch="10"/>
            <policyEntry topic=">"
                         producerFlowControl="false"/>
            <policyEntry topic=".>"
                         >
              <messageEvictionStrategy>
                <oldestMessageEvictionStrategy/>
              </messageEvictionStrategy>
      
              <pendingMessageLimitStrategy>
                <constantPendingMessageLimitStrategy limit="10"/>
              </pendingMessageLimitStrategy>
            </policyEntry>
          </policyEntries>
        </policyMap>
      </destinationPolicy>
      

    【讨论】:

      【解决方案2】:

      据我所知,发送到没有订阅者的非持久主题的消息应该被丢弃。只有当前注册的消费者才能获得消息副本。

      如何检查该主题是否包含这 150K 条消息?通过 JMX?

      不管您的非持久主题不应缓存这 150K 消息,您都可以使用代理策略限制每个消费者存储的消息量:

      <broker>
      ...    
        <pendingMessageLimitStrategy>
          <constantPendingMessageLimitStrategy limit="10"/>
        </pendingMessageLimitStrategy>
      ...
      </broker>
      

      【讨论】:

      【解决方案3】:

      不知道它是否有效,还没有机会测试它,但请在此处查看 constantPendingMessageLimitStrategy http://activemq.apache.org/slow-consumer-handling.html

      祝你好运 克劳迪奥

      【讨论】:

        【解决方案4】:

        我不确定您想要的内容是否可以存档。 你可以做几件事:

        首先,您可以为您的信息留出时间:

        public ITopicPublisher CreateTopicPublisher(string selector)
                {
                    try
                    {
                        IMessageProducer producer = m_session.CreateProducer(m_topic);
                        ActiveMQPublisher publisher = new ActiveMQPublisher(producer);
        
                        // here we put a time to live to 1min for eg
                        TimeSpan messageTTL = TimeSpan.FromMilliseconds(60000);
                        publisher.TimeToLive = messageTTL;
        
                        if (!String.IsNullOrEmpty(selector))
                        {
                            publisher.IsSelector = true;
                            publisher.Selector = selector;
                        }
                        return publisher;
                    }
                    catch (Exception ex)
                    {
                        Logger.Exception(ex);
                        throw;
                    }
                }
        

        早上你仍然会收到 150k 条消息,但是一旦有一个消费者连接到你的主题,过期的消息就会消失。

        您可能还想看看驱逐策略 (Eviction Strategy)

        编辑:我刚刚意识到一件事。你说“让它在一夜之间运行会导致超过 15 万条关于该主题的消息,即使它应该只包含非常小的数量。”在主题中,您没有必须接收消息的概念。如果没有人订阅某个主题,则删除那里发送的消息。仍然“排队的消息”将增加一。 如果您只想发送“最后 10 条消息”,也许您应该使用队列而不是主题?

        【讨论】:

          猜你喜欢
          • 2010-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-29
          • 2012-12-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多