Spring JMS(ActiveMQ) delayed delivery of messages

前提是你 提交于 2019-12-20 02:28:28

问题


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) 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);
...

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

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!


回答1:


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.

An example Broker tag with scheduler support enabled:

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



回答2:


Two things needs to be done to resolve this.

  • By default scheduled message support is disabled. We must enabled it in the broker XML configuration file.

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

  • Set the delay before sending the message.

    public void send(Object object) {
        log.info("put <" + object + ">");
        jmsTemplate.convertAndSend(QUEUE_NAME, object, m -> {
           m.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 10000);
           return m;
        });
    }
    



回答3:


Also according to method from ActiveMQ BrokerService class you should configure persistence to have ability to use scheduler functionality.

public boolean isSchedulerSupport() {
    return this.schedulerSupport && (isPersistent() || jobSchedulerStore != null);
}



回答4:


  jmsTemplate.convertAndSend(destination, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws JMSException {
                message.setIntProperty("JMS_OracleDelay", 200);
                return message;
            }
        });



回答5:


broker-url: vm://embedded?broker.persistent=true&broker.useShutdownHook=false&broker.schedulerSupport=true



回答6:


The JMS 2.0 is not supported in the activemq package. Try using artemis instead. Try replacing the package name from

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
        <version>1.5.6.RELEASE</version> 
    </dependency>

into

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-artemis</artifactId>
        <version>1.5.6.RELEASE</version> 
    </dependency>

and also add in the application.properties

    spring.artemis.mode=native
    spring.artemis.host=localhost
    spring.artemis.port=61616
    spring.artemis.user=admin
    spring.artemis.password=admin

Follow this article




回答7:


Documentation http://activemq.apache.org/delay-and-schedule-message-delivery.html

Example: after 10 sec received message by consumer

public void send(Object object) {
    log.info("put <" + object + ">");
    jmsTemplate.convertAndSend(QUEUE_NAME, object, m -> {
        m.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 10000);
        return m;
    });
}



回答8:


  1. I have added schedulerSupport=true in active mq configuration xml. Please dont forget to restart the active mq server after configuration changes. Once you restart and login to "Scheduled" tab on admin console of activemq, you would see scheduled message details.

  2. jmsTemplate.setDeliveryDelay did not work for me so I added the below piece of code : used

    jmsTemplate.convertAndSend(queueName, object, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws JMSException {
                message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, scheduledDelay);
                return message;
            }
        });

Please note : It was not working for me in the beginning. But what was needed was an restart on activeMQ server to reflect the changes in config



来源:https://stackoverflow.com/questions/25121094/spring-jmsactivemq-delayed-delivery-of-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!