Spring 启动 JMS 自动启动

Spring Boot JMS AutoStartup

我正在尝试 start/stop 在我的 Spring 启动应用程序中手动 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 truejmsListenerEndpointRegistry. isRunning () is true 执行前 jmsListenerEndpointRegistry.start();

还需要配置什么吗?也许我忽略了覆盖一些自动配置。

编辑 1:JmsListenerEndpointRegistry 侦听器的状态无效

我在我的 bean 中发现了一些不一致的地方:

jmsListenerEndpointRegistry.getListenerContainerIds().size() 始终为 0。 jmsListenerEndpointRegistry.isAutoStartup() 只是一个 return 真正的方法。

即使我用这样的注解注册了几个监听器:

@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配置后(自动启动)。

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