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

1

6

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;
    }
}
Laughry answered 21/8, 2017 at 11:31 Comment(9)
enableJms is required. Have you tried rebuilding and running application again.Rosebud
@Sangam Belose I tried click build/rebuild in idea. Now I will try to make invalidate cacheLaughry
@Sangam - it still working. If you have a desire to help me, I can share all sources with you.Laughry
Spring Boot detects the existence of JMS and automatically enables JMS processing. The statement holds true only for non Spring Boot applications.Tufthunter
@M. Deinum, How does spring detects the existence of JMS?Laughry
Spring doesn't detect anything Spring Boot does the detection. The existence of the @EnableJms annotation on the class path will trigger the JmsAnnotationDrivenConfiguration from Spring Boot. Which will automatically register the needed components.Tufthunter
@M. Deinum, I am sorry for inaccuracy. How does spring-boot detects the existence of JMS? It sees on dependencies?Laughry
As it does with other features it detects if certain classes/api classes are on the class path.Tufthunter
@M. Deinum, thanks for clarificationLaughry
C
8

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.

Cornew answered 30/8, 2017 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.