Apache activemq artemis v.2.28.0:如何根据需要设置消息是否具有过期时间?

huangapple go评论85阅读模式
英文:

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:

  1. public void send(JmsTemplate jmsTemplate, String queueName, String textMessage, long timeToLive) throws JMSException {
  2. jmsTemplate.setTimeToLive(timeToLive);
  3. jmsTemplate.convertAndSend(queueName, textMessage, new MessagePostProcessor() {
  4. public Message postProcessMessage(Message message) throws JMSException {
  5. message.setJMSExpiration(timeToLive);
  6. return message;
  7. }
  8. });
  9. }

I have defined the following configuration in the broker.xml file:

  1. <address-setting match="#">
  2. <dead-letter-address>DLQ</dead-letter-address>
  3. <expiry-address>ExpiryQueue</expiry-address>
  4. <redelivery-delay>0</redelivery-delay>
  5. <!-- with -1 only the global-max-size is in use for limiting -->
  6. <max-size-bytes>-1</max-size-bytes>
  7. <message-counter-history-day-limit>10</message-counter-history-day-limit>
  8. <address-full-policy>PAGE</address-full-policy>
  9. <auto-create-queues>true</auto-create-queues>
  10. <auto-create-addresses>true</auto-create-addresses>
  11. <auto-create-jms-queues>true</auto-create-jms-queues>
  12. <auto-create-jms-topics>true</auto-create-jms-topics>
  13. </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:

  1. <address-setting match="#">
  2. <dead-letter-address>DLQ</dead-letter-address>
  3. <expiry-address>ExpiryQueue</expiry-address>
  4. <expiry-delay>1000</expiry-delay>
  5. <redelivery-delay>0</redelivery-delay>
  6. <!-- with -1 only the global-max-size is in use for limiting -->
  7. <max-size-bytes>-1</max-size-bytes>
  8. <message-counter-history-day-limit>10</message-counter-history-day-limit>
  9. <address-full-policy>PAGE</address-full-policy>
  10. <auto-create-queues>true</auto-create-queues>
  11. <auto-create-addresses>true</auto-create-addresses>
  12. <auto-create-jms-queues>true</auto-create-jms-queues>
  13. <auto-create-jms-topics>true</auto-create-jms-topics>
  14. </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:

  1. public void send(JmsTemplate jmsTemplate, String queueName, String textMessage, long timeToLive) throws JMSException {
  2. jmsTemplate.setTimeToLive(timeToLive);
  3. jmsTemplate.convertAndSend(queueName, textMessage, new MessagePostProcessor() {
  4. public Message postProcessMessage(Message message) throws JMSException {
  5. message.setJMSExpiration(timeToLive);
  6. return message;
  7. }
  8. });
  9. }

I have defined the following configuration in the broker.xml file:

  1. &lt;address-setting match=&quot;#&quot;&gt;
  2. &lt;dead-letter-address&gt;DLQ&lt;/dead-letter-address&gt;
  3. &lt;expiry-address&gt;ExpiryQueue&lt;/expiry-address&gt;
  4. &lt;redelivery-delay&gt;0&lt;/redelivery-delay&gt;
  5. &lt;!-- with -1 only the global-max-size is in use for limiting --&gt;
  6. &lt;max-size-bytes&gt;-1&lt;/max-size-bytes&gt;
  7. &lt;message-counter-history-day-limit&gt;10&lt;/message-counter-history-day-limit&gt;
  8. &lt;address-full-policy&gt;PAGE&lt;/address-full-policy&gt;
  9. &lt;auto-create-queues&gt;true&lt;/auto-create-queues&gt;
  10. &lt;auto-create-addresses&gt;true&lt;/auto-create-addresses&gt;
  11. &lt;auto-create-jms-queues&gt;true&lt;/auto-create-jms-queues&gt;
  12. &lt;auto-create-jms-topics&gt;true&lt;/auto-create-jms-topics&gt;
  13. &lt;/address-setting&gt;

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:

  1. &lt;address-setting match=&quot;#&quot;&gt;
  2. &lt;dead-letter-address&gt;DLQ&lt;/dead-letter-address&gt;
  3. &lt;expiry-address&gt;ExpiryQueue&lt;/expiry-address&gt;
  4. &lt;expiry-delay&gt;1000&lt;/expiry-delay&gt;
  5. &lt;redelivery-delay&gt;0&lt;/redelivery-delay&gt;
  6. &lt;!-- with -1 only the global-max-size is in use for limiting --&gt;
  7. &lt;max-size-bytes&gt;-1&lt;/max-size-bytes&gt;
  8. &lt;message-counter-history-day-limit&gt;10&lt;/message-counter-history-day-limit&gt;
  9. &lt;address-full-policy&gt;PAGE&lt;/address-full-policy&gt;
  10. &lt;auto-create-queues&gt;true&lt;/auto-create-queues&gt;
  11. &lt;auto-create-addresses&gt;true&lt;/auto-create-addresses&gt;
  12. &lt;auto-create-jms-queues&gt;true&lt;/auto-create-jms-queues&gt;
  13. &lt;auto-create-jms-topics&gt;true&lt;/auto-create-jms-topics&gt;
  14. &lt;/address-setting&gt;
  15. &lt;/address-settings&gt;

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提供程序在发送其实现不是自己的消息时设置此字段。

相反,你应该使用相应的 MessageProducerJMSProducer 上的 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.

huangapple
  • 本文由 发表于 2023年4月19日 15:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定