Search code examples
springspring-bootspring-integrationioc-containerspring-jms

Spring Boot JMS AutoStartup


I am trying to start/stop manually JMS listeners in my Spring Boot App. I am currently using the following configuration to my container factory:

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

After testing factory.setAutoStartup(false); I am quite confused because even indicating to do not start any listener for this factory container, the listeners are already registered and started when the context starts.

I tested this situation by using a jmsListenerEndpointRegistry.

jmsListenerEndpointRegistry.isAutoStartup() is true and jmsListenerEndpointRegistry. isRunning () is true before execute jmsListenerEndpointRegistry.start();

Is it necessary to configure anything else? Maybe I am omitting to override some auto-configuration.

EDIT 1: Invalid status of JmsListenerEndpointRegistry listeners

I detected a couple of inconsistences in my beans:

jmsListenerEndpointRegistry.getListenerContainerIds().size() is always 0. jmsListenerEndpointRegistry.isAutoStartup() is just a return true method.

Even if I register a couple of listeners with annotations like this:

@JmsListener(containerFactory="queueContainerFactory", destination = "${dest}")

jmsListenerEndpointRegistry does not show information about these listeners status but they are connected to ActiveMQ on startup. (Checking the ActiveMQ admin console)

EDIT 2: @JmsListener starts even auto-startup is set to false

I checked the jmsListenerEndpointRegistry for each container and I do not know if this is a bug or I am not correctly defining the configuration. However, I am just defining the container factory as explained before with AUTO-START set to false and the both listeners are started and consuming messages (running).

From my Log file:

jmsListenerEndpointRegistry ID <org.springframework.jms.JmsListenerEndpointContainer#1>, Auto-Startup <false>, Running <true>
jmsListenerEndpointRegistry ID <org.springframework.jms.JmsListenerEndpointContainer#0>, Auto-Startup <false>, Running <true>

Solution

  • You must have something else going on - I just wrote a quick boot app (1.4.1) and the container is not started...

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

    and...

    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
    

    I suggest you use a debugger in the container's start() method to see why it's being started.