英文:
Apache activemq artemis v.2.28.0: How to set on demand whether a message has an expiration time or not?
问题
I'm learning how to use the Artemis messaging broker. In my example application, I'm applying a producer-consumer scheme. I want some messages that are sent to a queue to have an expiration time. My sample application is developed in Spring Boot. I use the following method to send messages:
public void send(JmsTemplate jmsTemplate, String queueName, String textMessage, long timeToLive) throws JMSException {
jmsTemplate.setTimeToLive(timeToLive);
jmsTemplate.convertAndSend(queueName, textMessage, new MessagePostProcessor() {
public Message postProcessMessage(Message message) throws JMSException {
message.setJMSExpiration(timeToLive);
return message;
}
});
}
I have defined the following configuration in the broker.xml file:
<address-setting match="#">
<dead-letter-address>DLQ</dead-letter-address>
<expiry-address>ExpiryQueue</expiry-address>
<redelivery-delay>0</redelivery-delay>
<!-- with -1 only the global-max-size is in use for limiting -->
<max-size-bytes>-1</max-size-bytes>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
<address-full-policy>PAGE</address-full-policy>
<auto-create-queues>true</auto-create-queues>
<auto-create-addresses>true</auto-create-addresses>
<auto-create-jms-queues>true</auto-create-jms-queues>
<auto-create-jms-topics>true</auto-create-jms-topics>
</address-setting>
With this configuration, I have checked through the management console that the messages have the Expiry field set to 0 even if I set a value other than 0 in the timeToLive field. I have modified the configuration of the broker.xml file to this one:
<address-setting match="#">
<dead-letter-address>DLQ</dead-letter-address>
<expiry-address>ExpiryQueue</expiry-address>
<expiry-delay>1000</expiry-delay>
<redelivery-delay>0</redelivery-delay>
<!-- with -1 only the global-max-size is in use for limiting -->
<max-size-bytes>-1</max-size-bytes>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
<address-full-policy>PAGE</address-full-policy>
<auto-create-queues>true</auto-create-queues>
<auto-create-addresses>true</auto-create-addresses>
<auto-create-jms-queues>true</auto-create-jms-queues>
<auto-create-jms-topics>true</auto-create-jms-topics>
</address-setting>
With this configuration, the opposite is true. If I set the timeToLive field to 0, the Expiry field does not show the value 0 and shows the timestamp of when the message will expire. I don't understand why.
Could you give me some orientation on how to solve this problem? Is it possible to set on demand whether a message has an associated expiration time or not?
Cheers
英文:
I'm learning how to use the Artemis messaging broker. In my example application, I'm applying a producer-consumer scheme. I want some messages that are sent to a queue to have an expiration time. My sample application is developed in Spring Boot. I use the following method to send messages:
public void send(JmsTemplate jmsTemplate, String queueName, String textMessage, long timeToLive) throws JMSException {
jmsTemplate.setTimeToLive(timeToLive);
jmsTemplate.convertAndSend(queueName, textMessage, new MessagePostProcessor() {
public Message postProcessMessage(Message message) throws JMSException {
message.setJMSExpiration(timeToLive);
return message;
}
});
}
I have defined the following configuration in the broker.xml file:
<address-setting match="#">
<dead-letter-address>DLQ</dead-letter-address>
<expiry-address>ExpiryQueue</expiry-address>
<redelivery-delay>0</redelivery-delay>
<!-- with -1 only the global-max-size is in use for limiting -->
<max-size-bytes>-1</max-size-bytes>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
<address-full-policy>PAGE</address-full-policy>
<auto-create-queues>true</auto-create-queues>
<auto-create-addresses>true</auto-create-addresses>
<auto-create-jms-queues>true</auto-create-jms-queues>
<auto-create-jms-topics>true</auto-create-jms-topics>
</address-setting>
With this configuration, I have checked through the management console that the messages have the Expiry field set to 0 even if I set a value other than 0 in the timeTolive field. I have modified the configuration of the broker.xml file to this one:
<address-setting match="#">
<dead-letter-address>DLQ</dead-letter-address>
<expiry-address>ExpiryQueue</expiry-address>
<expiry-delay>1000</expiry-delay>
<redelivery-delay>0</redelivery-delay>
<!-- with -1 only the global-max-size is in use for limiting -->
<max-size-bytes>-1</max-size-bytes>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
<address-full-policy>PAGE</address-full-policy>
<auto-create-queues>true</auto-create-queues>
<auto-create-addresses>true</auto-create-addresses>
<auto-create-jms-queues>true</auto-create-jms-queues>
<auto-create-jms-topics>true</auto-create-jms-topics>
</address-setting>
</address-settings>
With this configuration the opposite is true. If I set the timeToLive field to 0, the Expiry field does not show the value 0 and shows the timestamp of when the message will expire. I don't understand why.
Could you give me some orientation on how to solve this problem? Is it possible to set on demand whether a message has an associated expiration time or not?
Cheers
答案1
得分: 1
你正试图使用 setJMSExpiration()
方法,但这是不允许的,对消息没有任何影响。该方法的JavaDoc说明如下:
此方法仅供JMS提供程序在发送消息时设置此字段使用。客户端不能使用此消息来配置消息的过期时间。此方法是公开的,以允许JMS提供程序在发送其实现不是自己的消息时设置此字段。
相反,你应该使用相应的 MessageProducer
或 JMSProducer
上的 setTimeToLive()
方法。
英文:
You're attempting to use the setJMSExpiration()
method, but this is not allowed and will have no impact on the message. The JavaDoc for this method states:
> This method is for use by JMS providers only to set this field when a message is sent. This message cannot be used by clients to configure the expiration time of the message. This method is public to allow a JMS provider to set this field when sending a message whose implementation is not its own.
Instead you should use the setTimeToLive()
method on the corresponding MessageProducer
or JMSProducer
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论