Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable jms listener in local environment

How do i disable jms listener in my local environment? Since i am not connecting to MQ from the local enviroment, i donot want my console logs to get flooded with trying to connect MQ message. So i want to disable it locally.

I am using Spring boot and annotation based programming.I saw posts here suggesting to put autoStartup to false in properties but i cannot do that because i use spring boot. I have commented all the code related to jms right now but how do we make jms listener run only in UAT or Prod server not on local server?

My code:

// @JmsListener(destination = "TOBUS", containerFactory = "defaultJmsListenerContainerFactory")
public void onMessage(final TextMessage message) throws JMSException {//some code here}  

Thanks

like image 372
user2237700 Avatar asked Oct 11 '25 22:10

user2237700


2 Answers

spring.jms.listener.auto-startup=false

This worked for me

like image 124
Akash Yadagouda Avatar answered Oct 16 '25 05:10

Akash Yadagouda


The question is old but maybe can still interests someone.

There is at least two options to achieve this :

  • Using profiles : https://docs.spring.io/spring-boot/docs/3.0.x/reference/html/features.html#features.profiles
  • Using conditional annotation : https://docs.spring.io/spring-boot/docs/3.0.x/reference/html/features.html#features.developing-auto-configuration.condition-annotations

Is is not tell if this is already the case but first I would set the listening method in a separate bean. Appart from the fact that the proposed solutions work only with this kind of code organisation, it is better in term of Separation of Concerns/Responsability and then easier for implementing unit tests, the former implies the latter.

  • Using profiles : make your listener bean available only if a 'local' profile is not active.
    @Profile("!local")
    @Component
    public class MyBeanListener {
    
        @JmsListener(destination = "TOBUS", containerFactory = "defaultJmsListenerContainerFactory")
        public void onMessage(final TextMessage message) throws JMSException {//some code here}
    
    }

Then start your application with the profile 'local' defines.

  • Using conditional annotation : make your listener bean available unless a certain property is defined
    @ConditionalOnProperty(name = "jms.listener.enable", havingValue = "true", matchIfMissing = true)
    @Component
    public class MyBeanListener {
    
        @JmsListener(destination = "TOBUS", containerFactory = "defaultJmsListenerContainerFactory")
        public void onMessage(final TextMessage message) throws JMSException {//some code here}
        
    }

Then define a property "jms.listener.enable=false" only for your local environment (https://docs.spring.io/spring-boot/docs/3.0.x/reference/html/features.html#features.external-config)

NB : These annotations work at beans definition level (on @Configuration classes or on @Bean method factory), then setting them on the @JmsListener annotated method will have not effect.

like image 29
cerdoc Avatar answered Oct 16 '25 05:10

cerdoc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!