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

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

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:

&lt;address-setting match=&quot;#&quot;&gt;
	&lt;dead-letter-address&gt;DLQ&lt;/dead-letter-address&gt;
	&lt;expiry-address&gt;ExpiryQueue&lt;/expiry-address&gt;
	&lt;redelivery-delay&gt;0&lt;/redelivery-delay&gt;
	&lt;!-- with -1 only the global-max-size is in use for limiting --&gt;
	&lt;max-size-bytes&gt;-1&lt;/max-size-bytes&gt;
	&lt;message-counter-history-day-limit&gt;10&lt;/message-counter-history-day-limit&gt;
	&lt;address-full-policy&gt;PAGE&lt;/address-full-policy&gt;
	&lt;auto-create-queues&gt;true&lt;/auto-create-queues&gt;
	&lt;auto-create-addresses&gt;true&lt;/auto-create-addresses&gt;
	&lt;auto-create-jms-queues&gt;true&lt;/auto-create-jms-queues&gt;
	&lt;auto-create-jms-topics&gt;true&lt;/auto-create-jms-topics&gt;
&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:

&lt;address-setting match=&quot;#&quot;&gt;
	&lt;dead-letter-address&gt;DLQ&lt;/dead-letter-address&gt;
	&lt;expiry-address&gt;ExpiryQueue&lt;/expiry-address&gt;
	&lt;expiry-delay&gt;1000&lt;/expiry-delay&gt;
	&lt;redelivery-delay&gt;0&lt;/redelivery-delay&gt;
	&lt;!-- with -1 only the global-max-size is in use for limiting --&gt;
	&lt;max-size-bytes&gt;-1&lt;/max-size-bytes&gt;
	&lt;message-counter-history-day-limit&gt;10&lt;/message-counter-history-day-limit&gt;
	&lt;address-full-policy&gt;PAGE&lt;/address-full-policy&gt;
	&lt;auto-create-queues&gt;true&lt;/auto-create-queues&gt;
	&lt;auto-create-addresses&gt;true&lt;/auto-create-addresses&gt;
	&lt;auto-create-jms-queues&gt;true&lt;/auto-create-jms-queues&gt;
	&lt;auto-create-jms-topics&gt;true&lt;/auto-create-jms-topics&gt;
	&lt;/address-setting&gt;
&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:

确定