在Spring Boot中延迟activeMQ中的消息传递 [英] delay delivery of message in activeMQ in spring boot

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

问题描述

我想在'x'秒后的任何时间't'发送消息。

I want to send a message at any time 't' that will be received by the receiver after 'x' sec.

为此,我写了发件人代码

for doing so, I have written sender code

@Autowired
private JmsTemplate jmsTemplate;
private Queue queue = new ActiveMQQueue("topicName");

public void show(String message) {
    try {
        System.out.println("Sending message " + message);
        jmsTemplate.convertAndSend(queue, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws JMSException {
                System.out.println("postProcessMessage executed ");
                message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 3 * 60 * 1000);
                System.out.println("long time " + message
                        .getLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY));
                return message;
            }
        });
        System.out.println("Sending done " + message + " at " + System.currentTimeMillis());
    } catch (Exception er) {
        er.printStackTrace();
    }
}

和收件人代码

@JmsListener(destination = "topicName")
    public void reciever(String message) {
        System.out.println("receiving message " + message + " at " + System.currentTimeMillis());
    }

但是接收方即时收到的消息没有任何延迟。

But message received by receiver instant .without any delay.

输出为


发送消息为消息
postProcessMessage执行了较长的
时间180000
接收到的消息是1514391984964的消息

发送完成的是这条消息是1514391984970

Sending message this is a message
postProcessMessage executed long time 180000
receiving message this is a message at 1514391984964
Sending done this is a message at 1514391984970

配置文件是

@Bean
    JmsTemplate createJMSTemplate(ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(connectionFactory);
        return jmsTemplate;
    }

    @Bean
    ConnectionFactory myActiveMQConnectionFactory() {
        RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
        redeliveryPolicy.setBackOffMultiplier(1);
        redeliveryPolicy.setUseExponentialBackOff(false);
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        connectionFactory.setRedeliveryPolicy(redeliveryPolicy);
        NetworkConnector networkConnector = new DiscoveryNetworkConnector();
        networkConnector.setConsumerTTL(2);

        return connectionFactory;
    }


推荐答案

不支持延迟消息使用默认配置的activemq,首先应将其打开。

Delayed message is not supported by activemq with default config, you should turn it on at first.

添加scheduler在activemq.conf中支持

adding schedulerSupport in activemq.conf

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

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

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