本文讲述activemq的消息清理方法:
- 配置法
- 命令行
purge和delete有什么区别呢
我们看到管理界面的queue里有purge和delete,而且注意:只有queue有purge。
他们的区别很简单:
purge
: 只是清空队列里的消息,并把消息放入已经出队里delete
: 删除这个queue,在列表里会消失
为什么要删除过期的queue或topic?
因为,如果不删除不仅占空间,而且堆积太多会导致管理界面卡顿。
通过配置定时清理
官方文档:http://activemq.apache.org/delete-inactive-destinations
配置有以下几个:
schedulePeriodForDestinationPurge="60000"
,代表每60秒清理一次,默认为0gcInactiveDestinations="true"
,代表回收不活跃目标,默认是falseinactiveTimeoutBeforeGC="30000"
,当目标限制30秒删除,默认60秒
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" useJmx="true"
dataDirectory="${activemq.data}" schedulePeriodForDestinationPurge="60000">
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=">" >
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
<policyEntry queue=">" gcInactiveDestinations="true" inactiveTimeoutBeforeGC="30000"/>
</policyEntries>
</policyMap>
</destinationPolicy>
结果示例:
2020-04-07 16:09:16,619 | INFO | 0c2f8e32-3a4b-449a-807f-086012425188 Inactive for longer than 30000 ms - removing ... | org.apache.activemq.broker.region.Queue | ActiveMQ Broker[localhost] Scheduler
2020-04-07 16:09:16,620 | INFO | b3004cb2-d625-44e3-b18a-33e857a6b69d Inactive for longer than 30000 ms - removing ... | org.apache.activemq.broker.region.Queue | ActiveMQ Broker[localhost] Scheduler
2020-04-07 16:09:16,621 | INFO | 32d7e194-15ba-458b-a4f9-91a41d37fb32 Inactive for longer than 30000 ms - removing ... | org.apache.activemq.broker.region.Queue | ActiveMQ Broker[localhost] Scheduler
2020-04-07 16:09:16,622 | INFO | 8ffa6724-0ec9-4128-93bb-0955e0066a8c Inactive for longer than 30000 ms - removing ... | org.apache.activemq.broker.region.Queue | ActiveMQ Broker[localhost] Scheduler
命令行清理
purge是清理,比如清理一个queue的所有或部分消息
# bin/activemq purge 00015a7f-5bc4-4a76-aab3-366dee279320
...
INFO: Purging all messages in queue: 00015a7f-5bc4-4a76-aab3-366dee279320
正则匹配删除:可以选择性地清理某些queue/topic里满足条件地message:
Examples:
Main purge FOO.BAR
- Delete all the messages in queue FOO.BAR
Main purge --msgsel "JMSMessageID='*:10',JMSPriority>5" FOO.*
- Delete all the messages in the destinations that matches FOO.* and has a JMSMessageID in
the header field that matches the wildcard *:10, and has a JMSPriority field > 5 in the
queue FOO.BAR.
SLQ92 syntax is also supported.
* To use wildcard queries, the field must be a string and the query enclosed in ''
Use double quotes "" around the entire message selector string.
比如:清理000开始的queue的消息
# bin/activemq purge 000*
命令行我没找到删除功能,通过上面的配置可以定时移除。