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?

guness
  • 6,336
  • 7
  • 59
  • 88
bhasky
  • 73
  • 1
  • 5
  • here's what you need : http://stackoverflow.com/questions/32588352/how-can-i-stop-start-pause-a-jmslistener-the-clean-way/33214651#33214651 – Seb May 26 '16 at 14:44

1 Answers1

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • 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. – Marinos An Jan 12 '23 at 18:39
  • 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. – Gary Russell Jan 12 '23 at 18:50