我在 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 分钟运行一次并从队列中读取消息?
你不能在那里使用
@Scheduled
。
在需要时使用
JmsListenerEndpointRegistry
bean 启动和停止侦听器。
@JmsListener(id = "foo" ...)
registry.getListenerContainer("foo").start();
...
registry.getListenerContainer("foo").stop();