Automatically remove inactive durable consumers in ActiveMQ
For example, we have a durable topic consumer and some broker for that topic. As far as I understand that broker will continue to store every message (within some limits) even if that durable consumer is not active for a very lo开发者_运维百科ng time.
Is there some broker's policy in ActiveMQ to destroy (auto unsubscribe) inactive (for some specified time) durable consumers? So that I don't need to monitor if all consumers still valid and actually exist.
This is the options you need to set:
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulePeriodForDestinationPurge="10000" offlineDurableSubscriberTimeout="30000" offlineDurableSubscriberTaskSchedule="10000">
I would suggest you to use virtual topics instead of durable consumers.
When using virtual topic all your messages from producer are sent the same way you send them now. However when a new consumer registers with a unique consumer name - a new queue is created for him, and all messages sent to virtual topic are duplicated into the queue. That means each consumer has his own queue - which is much more convenient than durable topic subscription.
Then when you know for sure your consumer no longer exists - you can delete the queue with all pending messages in it. It can be done whether from code or manually through AMQ web console or jconsole.
EDIT: With virtual topic you get a new queue for every single consumer, therefore you can apply queue policy to delete it when inactive for a specified period of time.
In your activemq.xml set gcInactiveDestinations=true:
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry queue=">" producerFlowControl="false"
gcInactiveDestinations="true" inactiveTimoutBeforeGC="10000"/>
Read more: http://activemq.apache.org/delete-inactive-destinations.html
Above answer is correct but will not work until you don't add schedulePeriodForDestinationPurge="10000" at broker level. Every 10 seconds broker checks for inactive queues where no active consumer is connected and number of pending messages are zero. you can set it at higher value as well.
Using schedulePeriodForDestinationPurge policy is it possible to delete destination even though there are pending messages let say set a limit of 100 messages
精彩评论