如何停止/启动/暂停@JmsListener(干净的方式)

Seb*_*Seb 9 jms spring-jms message-listener spring-boot

我在我的项目上使用Spring(启动)并使用以下命令访问JMS队列(ActiveMQ):

@JmsListener(destination = "mydestinationQueue")
public void processMessage(String content) {
    //do something
}
Run Code Online (Sandbox Code Playgroud)

它运行完美,但我需要能够以编程方式停止/暂停/启动此bean(REST调用或类似的东西)

当我停止或暂停这个bean时,我想确保完全处理当前消息.

有什么想法吗?

谢谢

Seb*_*Seb 9

这是我找到的解决方案

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

}
Run Code Online (Sandbox Code Playgroud)


Gar*_*ell 6

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

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

  • @Seb 有消息吗?5年过去了,我的胡子已经花白了。 (8认同)