如何使用@JmsListener暂停和开始使用消息 [英] How to pause and start consuming message using @JmsListener

查看:665
本文介绍了如何使用@JmsListener暂停和开始使用消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot版本1.3.2。我正在使用@JmsListener消耗来自activemq的消息,以获取我使用JmsTemplate创建/产生的消息。下面是代码:

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

现在,当smtp出现连接失败错误时,我想暂停@JmsListener一段时间,然后再次开始使用该消息。对于使用@JmsListener的用例,我还没有看到更好的示例。由于我使用的是Spring Boot,因此我已在应用程序属性中添加了activemq连接参数,无需编写任何代码即可创建连接工厂,设置队列...等等,您能帮忙怎么做吗?

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?

推荐答案

获取对 JmsListenerEndpointRegistry bean的引用(例如 @Autowire )并调用 stop()-它会停止所有监听器。 start()将启动所有侦听器。

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

如果您有多个侦听器并且只想停止1,请给它 id 属性并使用 registry.getListenerContainer(id),然后停止/启动容器本身。

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.

这篇关于如何使用@JmsListener暂停和开始使用消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆