How can I Stop/start/Pause a @JmsListener (the clean way)

♀尐吖头ヾ 提交于 2019-11-29 02:04:37

Here is the solution I've found

@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";
}

}

There's a bean of type JmsListenerEndpointRegistry (name org.springframework.jms.config.internalJmsListenerEndpointRegistry).

You can access the JMS listener containers from the registry (all or by name) and call stop() on the one(s) you want; the container will stop after any in-process messages complete their processing.

   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();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!