在使用ActiveMQ的過程中,經常會發生生產者和消費者不對稱而導致的消費能力過剩的問題,這時就需要我們定時清除消息,需要在ActiveMQ服務的機器上找到相對應的ActiveMQ.xml配置文件,加上如下配置即可:
<broker xmlns="http://activemq.apache.org/schema/core" schedulePeriodForDestinationPurge="10000"> <destinationPolicy> <policyMap> <policyEntries> <policyEntry queue=">" gcInactiveDestinations="true" inactiveTimoutBeforeGC="30000"/> </policyEntries> </policyMap> </destinationPolicy> </broker>
實現定時自動清理無效的Topic和Queue需要設置三個屬性。
schedulePeriodForDestinationPurge執行清理任務的周期,
gcInactiveDestinations="true"啟用清理功能
inactiveTimoutBeforeGC="30000" Topic或Queue超時時間
在規定的時間內,無有效訂閱,沒有入隊記錄,超時后就會被清理