就我们而言,我们选择使用部署在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