就我們而言,我們選擇使用部署在ActiveMQ服務本身中的駱駝路線向特定目的地添加到期時間。
唯一要做的就是創建一個如下名稱的XML文件,例如: setJMSExpiration.xml
:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<camelContext id="camel-set-expiration" xmlns="http://camel.apache.org/schema/spring">
<!-- Copy route for each destination to expire -->
<route id="setJMSExpiration.my.queue.dlq">
<from uri="broker:queue:MY.QUEUE.DLQ"/>
<setHeader headerName="JMSExpiration">
<!-- Message will expire after 1 day -->
<spel>#{T(java.lang.System).currentTimeMillis() + 86400000}</spel>
</setHeader>
<to uri="broker:queue:MY.QUEUE.DLQ"/>
</route>
<route id="setJMSExpiration.another.queue">
<from uri="broker:queue:ANOTHER.QUEUE"/>
<setHeader headerName="JMSExpiration">
<!-- Message will expire after 5 days -->
<spel>#{T(java.lang.System).currentTimeMillis() + 432000000}</spel>
</setHeader>
<to uri="broker:queue:ANOTHER.QUEUE"/>
</route>
</camelContext>
</beans>
,並導入它在你的activemq.xml
配置:
<!-- Add default Expiration (file in the same directory) -->
<import resource="setJMSExpiration.xml"/>
或者您也可以提供特定per destination policies,如果你不想讓過期消息到達ActiveMQ.DLQ隊列。
<policyEntry queue="MY.QUEUE.DLQ">
<deadLetterStrategy>
<sharedDeadLetterStrategy processExpired="false" />
</deadLetterStrategy>
</policyEntry>
<policyEntry queue="ANOTHER.QUEUE">
<deadLetterStrategy>
<sharedDeadLetterStrategy processExpired="false" />
</deadLetterStrategy>
</policyEntry>
這種方式的唯一的限制就是你不能輕易使用通配符,因爲它是在這裏編碼(你可以,但它會通過在駱駝航線使用JMS目的地標題需要一些調整)。
我們嘗試讓生產者定義timeToLive(並儘可能強制它們),但並不總是可以強制他們更改其代碼,這樣可以最大限度地減少此類路由的數量。
看起來很有希望,我會給它一個鏡頭 – 2012-01-15 16:38:23