How to pause and start consuming message using @Jm

2020-03-08 07:12发布

问题:

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?

回答1:

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.