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

3

16

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

Bonaventura answered 15/9, 2015 at 14:12 Comment(0)
B
17

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

}
Bonaventura answered 19/10, 2015 at 12:40 Comment(2)
Hi, thanks for sharing this.. Is it applicable for 2 listeners? Do you set the bean name manually?Teniers
An old topic, but still applies. The stopApp() method attempts to start a new application and then closes it, instead of of the currently running one. I believe SpringApplication.exit(context) is the one to use for this purpose.Olivas
B
14

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.

Batfowl answered 15/9, 2015 at 16:6 Comment(2)
thanks for your answer I'll give it a try and let you knowBonaventura
@Bonaventura Any news? 5 years have passed and my beard is getting grey.Illboding
H
0
   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();
    }
Heyerdahl answered 10/10, 2018 at 8:17 Comment(1)
What does this mean ? Please give a context to what are these variable supposed to mean.Hypnotism

© 2022 - 2024 — McMap. All rights reserved.