第 16 章 配置消息过期
如果已发送的消息在指定时间后未传送给消费者,则可在服务器上将其设置为在服务器上过期。这些过期的消息可在以后使用,以进行进一步检查。
使用 Core API 设置消息过期
使用核心 API,您可以使用 setExpiration
方法在消息上设置过期时间。
// The message will expire 5 seconds from now message.setExpiration(System.currentTimeMillis() + 5000);
// The message will expire 5 seconds from now
message.setExpiration(System.currentTimeMillis() + 5000);
使用 JMS 设置消息过期
您可以将发送消息时使用的 JMS MessageProducer
的时间设置为 live。您可以使用 setTimeToLive
方法以毫秒为单位指定这个值。
// Messages sent by this producer will be retained for 5 seconds before expiring producer.setTimeToLive(5000);
// Messages sent by this producer will be retained for 5 seconds before expiring
producer.setTimeToLive(5000);
您还可以通过设置生产者的 发送
方法的生存时间,根据每个消息指定消息到期时间。
// The last parameter of the send method is the time to live, in milliseconds producer.send(message, DeliveryMode.PERSISTENT, 0, 5000)
// The last parameter of the send method is the time to live, in milliseconds
producer.send(message, DeliveryMode.PERSISTENT, 0, 5000)
从到期地址使用的消息具有以下属性:
_AMQ_ORIG_ADDRESS
包含已过期消息的原始地址
的字符串
属性。_AMQ_ACTUAL_EXPIRY
包含过期消息实际到期时间的
Long
属性。
16.1. 到期地址 复制链接链接已复制到粘贴板!
您可以通过设置到期地址来指定发送过期消息的位置。如果消息过期且未指定到期地址,则消息将从队列中删除并丢弃。
您可以使用管理 CLI 为 address
。在以下示例中,-setting
设置 expiration-addressjms.queue.exampleQueue
队列中的过期消息将发送到 jms.queue.expiryQueue
到期地址。
/subsystem=messaging-activemq/server=default/address-setting=jms.queue.exampleQueue:write-attribute(name=expiry-address,value=jms.queue.expiryQueue)
/subsystem=messaging-activemq/server=default/address-setting=jms.queue.exampleQueue:write-attribute(name=expiry-address,value=jms.queue.expiryQueue)