[英]Spring Boot JMS AutoStartup
我正在尝试在我的 Spring Boot 应用程序中手动启动/停止 JMS 侦听器。 我目前正在对我的容器工厂使用以下配置:
@EnableJms
public class ConfigJms {
...
@Bean(name = "queueContainerFactory")
public JmsListenerContainerFactory<?> queueContainerFactory(ConnectionFactory cf) {
ActiveMQConnectionFactory amqCf = (ActiveMQConnectionFactory) cf;
amqCf.setTrustAllPackages(true);
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(amqCf);
**factory.setAutoStartup(false);**
return factory;
}
...
}
经过测试factory.setAutoStartup(false);
我很困惑,因为即使指示不为此工厂容器启动任何侦听器,侦听器也已经注册并在上下文启动时启动。
我使用jmsListenerEndpointRegistry
测试了这种情况。
jmsListenerEndpointRegistry.isAutoStartup() is true
并且jmsListenerEndpointRegistry. isRunning () is true
jmsListenerEndpointRegistry. isRunning () is true
在执行jmsListenerEndpointRegistry.start();
是否需要配置其他东西? 也许我忽略了覆盖一些自动配置。
编辑 1: JmsListenerEndpointRegistry 监听器的状态无效
我在我的 bean 中发现了一些不一致的地方:
jmsListenerEndpointRegistry.getListenerContainerIds().size()
始终为 0。 jmsListenerEndpointRegistry.isAutoStartup()
只是一个返回 true 的方法。
即使我用这样的注释注册了几个听众:
@JmsListener(containerFactory="queueContainerFactory", destination = "${dest}")
jmsListenerEndpointRegistry
不显示有关这些侦听器状态的信息,但它们在启动时连接到 ActiveMQ。 (检查 ActiveMQ 管理控制台)
编辑 2:即使自动启动设置为 false,@JmsListener 也会启动
我检查了每个容器的jmsListenerEndpointRegistry
,但我不知道这是一个错误还是我没有正确定义配置。 但是,我只是定义容器工厂,如前所述,将 AUTO-START 设置为 false,并且两个侦听器都已启动并使用消息(正在运行)。
从我的日志文件:
jmsListenerEndpointRegistry ID <org.springframework.jms.JmsListenerEndpointContainer#1>, Auto-Startup <false>, Running <true>
jmsListenerEndpointRegistry ID <org.springframework.jms.JmsListenerEndpointContainer#0>, Auto-Startup <false>, Running <true>
你一定有其他事情发生 - 我刚刚写了一个快速启动应用程序(1.4.1)并且容器没有启动......
@SpringBootApplication
public class So39654027Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(So39654027Application.class, args);
JmsListenerEndpointRegistry reg = context.getBean(JmsListenerEndpointRegistry.class);
MessageListenerContainer listenerContainer = reg.getListenerContainer("foo");
System.out.println(listenerContainer.isRunning());
}
@Bean(name = "queueContainerFactory")
public JmsListenerContainerFactory<?> queueContainerFactory(ConnectionFactory cf) {
ActiveMQConnectionFactory amqCf = (ActiveMQConnectionFactory) cf;
amqCf.setTrustAllPackages(true);
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(amqCf);
factory.setAutoStartup(false);
return factory;
}
@JmsListener(id="foo", destination = "so39654027", containerFactory = "queueContainerFactory")
public void listen(String foo) {
System.out.println(foo);
}
}
和...
2016-09-23 09:24:33.428 INFO 97907 --- [ main] com.example.So39654027Application : Started So39654027Application in 1.193 seconds (JVM running for 2.012)
false
我建议您在容器的start()
方法中使用调试器来查看它启动的原因。
顺序很重要,配置后 factory.setAutoStartup(autoStartup)。
@Bean
public JmsListenerContainerFactory<?> ShipmentListenerFactory(@Qualifier("GSUBCachingConnectionFactory") CachingConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
// Added ability to disable not start listener
boolean autoStartup = env.getProperty("app-env.CKPT_QUEUE_AUTO_START",Boolean.class,true);
log.info("[MQ] CKPT_QUEUE_AUTO_START:{}",autoStartup);
configurer.configure(factory, connectionFactory);
factory.setAutoStartup(autoStartup);
// You could still override some of Boot's default if necessary.
return factory;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.