How can I stop / start / pause @JmsListener (clean path)

I am using Spring (boot) in my project and I am accessing the JMS Queue (ActiveMQ) using:

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

And it works fine, but I need to be able to stop / pause / start this bean programmatically (REST call or something like that)

When I stop or pause this bean, I want to be sure that I have fully processed the current message.

any idea about this?

thanks

+5
source share
2 answers

Here is 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 you need; the container will be stopped after all incoming messages complete processing.

+4
source

Here is the solution I 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"; } } 
+4
source

All Articles