How to pause and start consuming message using @JmsListener
Asked Answered
B

1

5

I am using spring boot version 1.3.2. I am using @JmsListener to consume message from activemq for the message that I created/produced using JmsTemplate. Here is the code:

@JmsListener(destination = "myqueue")
public void consumeMsg(Object requestBody)
    try {
        javaMailSender.send(requestBody);
    } catch (MailException ex) {
        LOG.error(ex.getLocalizedMessage(), ex);
        if(ex.getMessage().contains(SMTP_CONNECTION_FAILURE) && activeMqMsg.getIntProperty("RETRYCOUNT") == 1) {
            producer.send("myqueue",requestBody)
        }
        else {
            producer.send("manualqueue",requestBody)
        }
    }
}

now when there is a connection failure error from smtp, I want to pause the @JmsListener for SOME time and start again to consume the message. I have not seen a better example for this use case using @JmsListener. Since I am using spring boot, I have added activemq connection parameters in application properties, I do not need to write any code to create connection factory, setting queue...etc can you help out how to do this?

Bedivere answered 16/3, 2016 at 21:1 Comment(1)
here's what you need : #32588852Trapshooting
O
8

Get a reference to the JmsListenerEndpointRegistry bean (e.g. @Autowire) and call stop() - it will stop all listeners. start() will start all listeners.

If you have multiple listeners and only want to stop 1, give it an id attribute and use registry.getListenerContainer(id), then stop/start the container itself.

Oppugnant answered 16/3, 2016 at 21:30 Comment(2)
My listener callback method runs on a new message arriving at the queue but I cannot find any listener containers via JmsListenerEndpointRegistry.getListenerContainers. Not sure how I can debug to find out why.Seal
Don't ask new questions in comments on 7 year old answers. Most likely you are examining the registry too soon, before the containers are registered, or you qre not using @JmsListener; explicitly defined container beans do not appear in the registry, they are beans in the application context. Ask a new question showing code/configuration if you can't figure it out.Oppugnant

© 2022 - 2024 — McMap. All rights reserved.