Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveMQ delayed delivery of messages in Spring Boot

My question is really similar to Spring JMS(ActiveMQ) delayed delivery of messages but more correlated to the spring-boot auto configurer

I'm trying to use the jmsTemplate.setDeliveryDelay method, but it throws a java.lang.IllegalStateException: setDeliveryDelay requires JMS 2.0

I tried to find the right property from http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html but couldn't find the broker schedulerSupport option.

Currently, my application.properties is empty, and my JmsListenerContainerFactory is defined as follows

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

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

And my pom only contains

    <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>

With the spring-boot-starter-parent in 1.4.1.RELEASE

The question is: Is it possible to set the schedulerSupport to true using SpringBoot configurations?

In case it's needed, here are my Sender

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

and Receiver

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

thanks in advance


Update: I tried put it in the message properties, like the documentation http://activemq.apache.org/delay-and-schedule-message-delivery.html , but it doesn't work

@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;
}
like image 504
Allan Vital Avatar asked Oct 18 '25 21:10

Allan Vital


2 Answers

The template is trying to call the JMS 2.0 delivery delay methods but the ActiveMQ client and broker only support JMS 1.1 so you will get this error. You can use the ActiveMQ support for scheduled delivery by setting the message properties in the message using the values defined here.

It's not entirely clear how to enable the scheduler from Spring boot but my guess is that you need to provide your own Broker URI that enables it, something like:

broker:(tcp://localhost:61616)?persistent=true&useJmx=false&schedulerSupport=true
like image 57
Tim Bish Avatar answered Oct 20 '25 11:10

Tim Bish


I don't know if it's cheating, but what solved the problem for me was change the starter from activemq to artemis (https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-artemis)

Apparently, spring configures, by default, the Artemis to the JMS 2.0 interface. This way you have access to setDeliveryDelay method.

like image 22
Allan Vital Avatar answered Oct 20 '25 09:10

Allan Vital



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!