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:
@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);
}
}
@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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With