Monday, August 24, 2020

Schedule or delay a message ActiveMQ - Spring boot Embedded broker

Embedded broker scheduler (delay a message) ActiveMQ Spring boot

Let take a scenario where we are simultaneously pushing data into message queue and database and my expectation is first it updates database and then message consumer  consume data (after some delay).
We can achieve this by scheduling the ActiveMQ or we can say schedule the delivery of message.
By default 'Scheduler' is not enabled and we need enable it manually and also we need local persistent to store messages.

Below are the steps to configure scheduler (or delay  messages ) in ActiveMQ (Embedded)

1.    Add the following dependencies in pom.xml

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-kahadb-store</artifactId>
            <scope>runtime</scope>
        </dependency>

2.    Enable broker persistent and scheduler support.
 
vm://embedded?broker.persistent=true&broker.useShutdownHook=false&broker.schedulerSupport=true


3. Add the delay time in millisecond inside message sender

public void sendToQue(MessageQueDTO myMessage) {

        jmsTemplate.convertAndSend(message_que, myMessage,m -> {

            m.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 100000);

            return m;

         }); 




Labels: , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home