【问题标题】:How can I Stop/start/Pause a @JmsListener (the clean way)我怎样才能停止/启动/暂停@JmsListener(干净的方式)
【发布时间】:2015-12-11 20:16:43
【问题描述】:

我在我的项目中使用 Spring(boot),并使用以下方法访问 JMS 队列 (ActiveMQ):

@JmsListener(destination = "mydestinationQueue")
public void processMessage(String content) {
    //do something
}

它工作得很好,但我需要能够以编程方式停止/暂停/启动这个 bean(一个 REST 调用或类似的东西)

当我停止或暂停此 bean 时,我想确保已完全处理当前消息。

对此有什么想法吗?

谢谢

【问题讨论】:

    标签: spring-boot jms spring-jms message-listener


    【解决方案1】:

    这是我找到的解决方案

    @RestController
    @RequestMapping("/jms")
    public class JmsController {
    
         @Autowired
         ApplicationContext context;
    
    @RequestMapping(value="/halt", method= RequestMethod.GET)
    public @ResponseBody
    String haltJmsListener() {
        JmsListenerEndpointRegistry customRegistry =
                context.getBean("jmsRegistry", JmsListenerEndpointRegistry.class);
        customRegistry.stop();
        return "Jms Listener Stopped";
    }
    
    @RequestMapping(value="/restart", method=RequestMethod.GET)
    public @ResponseBody
    String reStartJmsListener() {
        JmsListenerEndpointRegistry customRegistry =
                context.getBean("jmsRegistry", JmsListenerEndpointRegistry.class);
        customRegistry.start();
        return "Jms Listener restarted";
    }
    
    @RequestMapping(value="/stopApp", method=RequestMethod.GET)
    public @ResponseBody
    String stopApp() {
        String[] args={};
        SpringApplication.run(FacturationApplicationFrontDaemon.class, args).close();
        return "stopped";
    }
    
    }
    

    【讨论】:

    • 嗨,谢谢分享。它适用于 2 位听众吗?是否手动设置 bean 名称?
    • 一个老话题,但仍然适用。 stopApp() 方法尝试启动一个新应用程序,然后关闭它,而不是当前正在运行的应用程序。我相信 SpringApplication.exit(context) 是用于此目的的。
    【解决方案2】:

    有一个 JmsListenerEndpointRegistry 类型的 bean(名称 org.springframework.jms.config.internalJmsListenerEndpointRegistry)。

    您可以从注册表(全部或按名称)访问 JMS 侦听器容器,并在所需的容器上调用 stop();任何正在处理的消息完成处理后,容器将停止。

    【讨论】:

    • 感谢您的回答,我会试一试告诉您
    • @Seb 有什么消息吗? 5年过去了,我的胡子越来越白了。
    【解决方案3】:
       private void stopJMSListener() {
           if(null == customRegistry){
               customRegistry = context.getBean(JmsListenerEndpointRegistry.class);
           }
            customRegistry.stop();
        }
    
       private void startJMSListener() {
           if(null == customRegistry){
            JmsListenerEndpointRegistry customRegistry = context.getBean(JmsListenerEndpointRegistry.class);
           }
            customRegistry.start();
        }
    

    【讨论】:

      猜你喜欢
      • 2014-06-05
      • 2011-09-06
      • 1970-01-01
      • 2016-07-19
      • 2020-10-26
      • 2018-11-25
      • 1970-01-01
      • 2020-06-20
      • 2011-12-05
      相关资源
      最近更新 更多