Spring JMS(ActiveMQ)延迟消息传递 [英] Spring JMS(ActiveMQ) delayed delivery of messages

查看:1222
本文介绍了Spring JMS(ActiveMQ)延迟消息传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试对某些JMS消息设置延迟,以便仅在x时间之后将消息添加到侦听器接收到的队列/中.到目前为止,我们已经尝试了两种无效的方法.

We're trying to set a delay on some JMS messages, so that a message will only be added to the queue/ received by the listener after x time. So far we've tried 2 approaches that didn't work.

1)根据spring文档,我们可以在JMSTemplate上设置传递延迟.这是我们尝试的示例代码:

1) According to the spring documentation, we can set the delivery delay on the JMSTemplate. This is the sample code we tried:

@Autowired
private JmsTemplate jmsTemplate;

...
long deliveryDelay = ...;
this.jmsTemplate.setDeliveryDelay(deliveryDelay);
this.jmsTemplate.convertAndSend(
                    queue.getName(),
                    event);
...

但是,即使我们的spring jms版本是4.0.5,我们也会遇到以下异常:

However, we get the following exception, even though our spring jms version is 4.0.5:

java.lang.IllegalStateException: setDeliveryDelay requires JMS 2.0

2)我们还尝试了设置消息本身的延迟,但是看起来延迟被忽略了,无论如何,消息还是立即被传递了.

2) We also tried setting the delay on the message itself, but it looks like the delay was ignored, and the message was delivered immediately anyway.

@Component
public class MyMessageConverter implements MessageConverter {

...

@Override
public Message toMessage(Object eventObject, Session session) throws JMSException, MessageConversionException {

...
long deliveryDelay = ...;
objectMessage.setLongProperty(
                  ScheduledMessage.AMQ_SCHEDULED_DELAY,
                  deliveryDelay);
return objectMessage;
}
}

spring xml中的jmsTemplate定义:

The jmsTemplate definition in the spring xml:

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="cachingConnectionFactory" />
    <property name="messageConverter" ref="myMessageConverter" />
    <property name="sessionTransacted" value="true" />
</bean>

有人对问题出在哪里有任何建议/关于如何实现延迟消息传递的其他想法吗? 谢谢!

Does anyone has any suggestions on what the problems are / other ideas on how to achieve delayed messaging? Thanks!

推荐答案

注释给出了答案.默认情况下,计划消息支持是禁用的.您必须按照文档所述在代理XML配置文件中启用它. a>页面.

The comments give the answer. By default scheduled message support is disabled. You must enabled it in the broker XML configuration file as mentioned on the documentation page.

启用了调度程序支持的示例Broker标签:

An example Broker tag with scheduler support enabled:

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true">

这篇关于Spring JMS(ActiveMQ)延迟消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆