在SpringBoot中安排JMS侦听器

我在Spring Boot应用程序中有一个侦听activeMQ中队列的方法。我想安排该方法,以便它不会在应用程序启动时开始侦听队列,而是每隔X分钟运行一次。

这是我为完成任务而编写的方法。我已禁用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;
}

但是当我运行应用程序时,出现一个异常,该异常表明计划的方法不能具有参数:

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

有没有办法安排JMSListener,使其在应用程序启动延迟后启动,并计划每隔X分钟运行一次并从队列中读取消息?

XXQJING 回答:在SpringBoot中安排JMS侦听器

您不能在那里使用@Scheduled

在需要时使用JmsListenerEndpointRegistry bean启动和停止侦听器。

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


registry.getListenerContainer("foo").start();
...
registry.getListenerContainer("foo").stop();
本文链接:https://www.f2er.com/3136574.html

大家都在问