ActiveMQ 在 Spring 引导中延迟传递消息

ActiveMQ delayed delivery of messages in Spring Boot

我的问题与 Spring JMS(ActiveMQ) delayed delivery of messages 非常相似,但与 spring-boot auto configurer

更相关

我正在尝试使用 jmsTemplate.setDeliveryDelay 方法,但它抛出一个 java.lang.IllegalStateException: setDeliveryDelay requires JMS 2.0

我试图从 http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 中找到正确的 属性,但找不到代理 schedulerSupport 选项。

目前我的application.properties是空的,我的JmsListenerContainerFactory定义如下

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

    configurer.configure(factory, connectionFactory);
    return factory;
}

而我的pom只包含

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

使用 1.4 中的 spring-boot-starter-parent。1.RELEASE

问题是:是否可以使用 SpringBoot 配置将 schedulerSupport 设置为 true?

如果需要,这是我的发件人

public void send(String message) {
    System.out.println("Im sending this message " + message);
    jmsTemplate.setDeliveryDelay(5000);
    jmsTemplate.convertAndSend(Beans.QUEUE_NAME, message);
}

和接收器

@JmsListener(destination = Beans.QUEUE_NAME, containerFactory = "myFactory")
public void receiveMessage(String message) {
    System.out.println("Received this message <" + message + ">");
}

提前致谢


更新:我试着把它放在消息属性中,就像文档 http://activemq.apache.org/delay-and-schedule-message-delivery.html 一样,但是它不起作用

@Bean
public MessageConverter messageConverter() {
    MessageConverter converter = new MessageConverter(){
        @Override
        public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
            if (!(object instanceof MyPojo)) {
                throw new MessageConversionException("not agreed Pojo!");
            }
            MyPojo pojo = (MyPojo) object;

            Message message = session.createTextMessage(pojo.getMessage());
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, pojo.getScheduledWait());
            return message;
        }
        @Override
        public Object fromMessage(Message message) throws JMSException, MessageConversionException {
            return message;
        }
    };
    return converter;
}

该模板正在尝试调用 JMS 2.0 传递延迟方法,但 ActiveMQ 客户端和代理仅支持 JMS 1.1,因此您会收到此错误。通过使用定义的值 here.

在消息中设置消息属性,您可以使用 ActiveMQ 支持计划的传递

目前还不完全清楚如何从 Spring 引导启用调度程序,但我的猜测是您需要提供自己的 Broker URI 来启用它,例如:

broker:(tcp://localhost:61616)?persistent=true&useJmx=false&schedulerSupport=true

我不知道是不是作弊,但对我来说解决问题的方法是将启动器从 activemq 更改为 artemis (https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-artemis)

显然,spring 默认将 Artemis 配置为 JMS 2.0 接口。这样您就可以访问 setDeliveryDelay 方法。