简体   繁体   English

在 SpringBoot 中调度 JMS 监听器

[英]Scheduling JMS listener in SpringBoot

I have a method in a Spring Boot application that listens to a queue in ActiveMQ.我在 Spring 启动应用程序中有一个方法,它侦听 ActiveMQ 中的队列。 I want to schedule the method so that it does not start listening to the queue on application startup and runs every X minutes.我想安排该方法,使其不会在应用程序启动时开始侦听队列并每 X 分钟运行一次。

Here is the method that I wrote to accomplish the task.这是我为完成任务而编写的方法。 I have disabled the JMSListener auto startup so that it does not start listening on when the application is started.我禁用了JMSListener自动启动,这样它就不会在应用程序启动时开始监听。

@Scheduled(fixedDelay = 1000, initialDelay = 1000)
@JmsListener(destination = "queueName")
public void receiveMessage(final Message jsonMessage) throws JMSException {
   System.out.println("Received message " + jsonMessage);

}

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
   DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
   factory.setConnectionFactory(connectionFactory());
   factory.setConcurrency("1-1");
   factory.setAutoStartup(false);
   return factory;
}

But when I run the application I get an exception which says that the scheduled method cannot have arguments:但是当我运行应用程序时,我得到一个异常,它说计划的方法不能有 arguments:

Encountered invalid @Scheduled method 'receiveMessage': Only no-arg methods may be annotated with @Scheduled

Is there a way I can schedule the JMSListener so that it starts after a delay on the application startup and is scheduled to run every X minutes and read messages from the queue?有没有办法可以安排JMSListener使其在应用程序启动延迟后启动,并安排为每 X 分钟运行一次并从队列中读取消息?

You can't use @Scheduled there.你不能在那里使用@Scheduled

Use the JmsListenerEndpointRegistry bean to start and stop the listener when needed.需要时使用JmsListenerEndpointRegistry bean 启动和停止侦听器。

@JmsListener(id = "foo" ...)


registry.getListenerContainer("foo").start();
...
registry.getListenerContainer("foo").stop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM