超出ActiveMQ内存限制

时间:2015-02-19 13:43:08

标签: activemq

我尝试将ActiveMQ配置为以下行为:当代理超出其内存限制时,它应该将消息存储在持久性存储中。如果使用以下配置:

    BrokerService broker = new BrokerService();
    broker.setBrokerName("activemq");
    KahaDBPersistenceAdapter persistence = new KahaDBPersistenceAdapter();
    persistence.setDirectory(new File(config.getProperty("amq.persistenceDir", "amq")));
    broker.setPersistenceAdapter(persistence);
    broker.setVmConnectorURI(new URI("vm://activemq"));
    broker.getSystemUsage().getMemoryUsage().setLimit(64 * 1024 * 1024L);
    broker.getSystemUsage().getStoreUsage().setLimit(1024 * 1024 * 1024 * 100L);
    broker.getSystemUsage().getTempUsage().setLimit(1024 * 1024 * 1024 * 100L);
    PolicyEntry policyEntry = new PolicyEntry();
    policyEntry.setCursorMemoryHighWaterMark(50);
    policyEntry.setExpireMessagesPeriod(0L);
    policyEntry.setPendingDurableSubscriberPolicy(new StorePendingDurableSubscriberMessageStoragePolicy());
    policyEntry.setMemoryLimit(64 * 1024 * 1024L);
    policyEntry.setProducerFlowControl(false);
    broker.setDestinationPolicy(new PolicyMap());
    broker.getDestinationPolicy().setDefaultEntry(policyEntry);
    broker.setUseJmx(true);
    broker.setPersistent(true);
    broker.start();

然而,这不起作用。 ActiveMQ仍然需要消耗尽可能多的内存来存储完整队列。我还尝试删除PolicyEntry,导致代理在达到内存限制后停止生产者。我在文档中找不到任何关于我做错的事情。

1 个答案:

答案 0 :(得分:0)

我们使用storeCursor并设置内存限制如下...这会将所有队列的内存量限制为100MB ......

   <destinationPolicy>
        <policyMap>
            <policyEntries>
                <policyEntry queue=">" producerFlowControl="false" memoryLimit="100mb">
                    <pendingQueuePolicy>
                        <storeCursor/>
                    </pendingQueuePolicy>
                </policyEntry>
            </policyEntries>
        </policyMap>
    </destinationPolicy>

确保您设置了您的政策应适用的“目的地”...在我的XML示例中,这是使用queue=">"完成的,但您的示例是使用new PolicyMap() ...尝试呼叫policyEntry.setQueue(">")代替应用于所有队列或将特定目标添加到PolicyMap等。

查看此测试以获取完整示例...

https://github.com/apache/activemq/blob/master/activemq-unit-tests/src/test/java/org/apache/activemq/PerDestinationStoreLimitTest.java