Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does spring look for @EnableJms methods if I don't have class marked @EnableJms annotation?

I am reading official get started article about how to start spring-jms application

https://spring.io/guides/gs/messaging-jms/

@EnableJms triggers the discovery of methods annotated with @JmsListener, creating the message listener container under the covers.

But my application sees @JmsListener methods without @EnableJms annotation.

Maybe something else force spring search the @EnableJms methods. I want to know it.

project srtucture:

enter image description here

Listener:

@Component
public class Listener {

    @JmsListener(destination = "my_queue_new")
    public void receive(Email email){
        System.out.println(email);
    }
    @JmsListener(destination = "my_topic_new", containerFactory = "myFactory")
    public void receiveTopic(Email email){
        System.out.println(email);
    }
}

RabbitJmsApplication:

@SpringBootApplication
//@EnableJms  I've commented it especially, behaviour was not changed.
public class RabbitJmsApplication {

    public static void main(String[] args) {
        SpringApplication.run(RabbitJmsApplication.class, args);
    }

    @Bean
    public RMQConnectionFactory connectionFactory() {
        return new RMQConnectionFactory();
    }

    @Bean
    public JmsListenerContainerFactory<?> myFactory(DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        // This provides all boot's default to this factory, including the message converter
        configurer.configure(factory, connectionFactory);
        // You could still override some of Boot's default if necessary.
        factory.setPubSubDomain(true);
        return factory;
    }
}
like image 798
gstackoverflow Avatar asked Aug 21 '17 11:08

gstackoverflow


1 Answers

Thanks for the feedback. This is a little bit confusing indeed and I've created an issue to improve the sample.

@EnableJms is a framework signal to start processing listeners and it has to be explicit because the framework has no way to know that you want to use JMS.

Spring Boot, on the other hand, can take default decisions for you based on the context. If you have the necessary bits to create a ConnectionFactory it will do so. Down the road, if we detect that a ConnectionFactory is available, we'll automatically enable the processing of JMS listeners.

like image 51
Stephane Nicoll Avatar answered Sep 23 '22 23:09

Stephane Nicoll