在spring boot中延迟在activeMQ中传递消息

时间:2017-12-27 16:38:08

标签: spring-boot activemq

我希望在'x'秒之后接收器接收的任何时间't'发送消息。

为此,我写了发件人代码

@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());
    }

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

输出

  

发送消息这是一条消息,执行时间为postProcessMessage   时间180000
收到消息这是一条消息,电话号码是1514391984964
  发送完成时的消息是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;
    }

1 个答案:

答案 0 :(得分:0)

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

在activemq.conf中添加schedulerSupport

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