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

Seb picture Seb · Sep 15, 2015 · Viewed 13.5k times · Source

I am using Spring(boot) on my project and I access a JMS Queue (ActiveMQ) using :

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

And it works perfectly but I need to be able to stop/pause/start this bean programatically (a REST call or something like that)

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

any idea about that ?

thanks

Answer

Seb picture Seb · Oct 19, 2015

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

}