0
votes

I am working on spring boot application where rabbit credentials are read from external source. I need to terminate the application when there is problem reading the credentials from that source. 1. I have rabbit listener 2.Processor 3. and publisher wit manual Ack.

**

Problem

** When There is error I am calling ConfigurableApplicationContext.close(). But still my rabbit template is getting instantiated and trying to connect to rabbit mq.

Tried out solutions

  • I tried using this in my application @EnableAutoConfiguration(exclude=RabbitAutoConfiguration.class)
  • spring.autoconfigure.exclude=true
  • spring.rabbitmq.listener.simple.auto-startup=false None of the above option works for me. How do I exit the application Abort from the point of failure.

My code is as follows


@Configuration
public class RabbitMqConfig {

    @PostConstruct
    public void getAIMCredentails() {
        loadAimSecretsForRabbitConfig();
    }
 public void loadAimSecretsForRabbitConfig(ConfigurableApplicationContext ctx){
      // on error 
     cntx.close();
  }
}



     @Configuration
        public class TopicConfiguration {
             @Bean
            public CachingConnectionFactory cachingConnectionFactory() {
                CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
                cachingConnectionFactory.setAddresses(rabbitMqConfig.getPublisherAddresses());
                cachingConnectionFactory.setUsername(rabbitMqConfig.getPublisherUsername());
                cachingConnectionFactory.setPassword(rabbitMqConfig.getPublisherPassword());
                cachingConnectionFactory.setVirtualHost(rabbitMqConfig.getVhost());
                cachingConnectionFactory.createConnection();
                cachingConnectionFactory.setPublisherReturns(true);
                cachingConnectionFactory.setPublisherConfirms(true);
                cachingConnectionFactory.setConnectionNameStrategy(f -> "publisherConnection");
                return cachingConnectionFactory;
            }

            /**
             * Bean RabbitTemplate
             * @return RabbitTemplate
             */
            @Bean
            public RabbitTemplate template(
                    @Qualifier("cachingConnectionFactory") CachingConnectionFactory cachingConnectionFactory) {
                // some code Here 
                return rabbitTemplate;
            }

            /**
             * Bean Jackson2JsonMessageConverter
             * @return Jackson2JsonMessageConverter
             */
            @Bean
            public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
                return new Jackson2JsonMessageConverter();
            }
        } 

Edit1:

I am using spring-rabbit-2.1.5.RELEASE and spring-starter-amqp-2.1.4.RELEASE Rabbit template is called as below :


    @Component
    public class EPPQ2Subscriber {
        private static final Logger LOGGER = LoggerFactory.getLogger(EPPQ2Subscriber.class);
        //@RabbitListener(queues = "#{queue.getName()}") @TODO I wann to use this in later point in time.. !
        @Autowired
        RabbitMqConfig rabbitMqConfig;

        @Autowired
        AppConfig appConfig;

        @Autowired
        EPPQ2PublisherImpl eppQ2Publisher; //which intern calls the Topic config by auto-wiring

    }


    @Component
    public class EPPQ2PublisherImpl implements EPPQ2Publisher{
        @Autowired
        RabbitMqConfig rabbitMqConfig;

        @Autowired
        private RabbitTemplate rabbitTemplate;  // this is ref 

        private Channel channel;

    }

Logs: Error Occurred comment

2019-06-27 09:46:01,626 INFO scrubber.util.AIMPropertiesUtil [main] ---- Start createRequestProperties -----
2019-06-27 09:46:01,628 INFO scrubber.util.AIMPropertiesUtil [main] ---- End createRequestProperties -----
2019-06-27 09:46:01,630 INFO scrubber.service.AimSecretRequesterService [main] START requestContent will do 3 tries.
2019-06-27 09:46:02,688 ERROR scrubber.service.AimSecretRequesterService [main] System Error secret.management.SecretRequestException Get secret fails [Retrieve Secret Faile 2019-06-27 09:46:02,693 ERROR scrubber.service.AimSecretRequesterService [main] Error stack {}
secret.management.SecretRequestException: Get secret fails [Retrieve Secret Failed!]!

calling the cntx.close() //comment 2019-06-27 09:46:02,695 ERROR scrubber.configuration.RabbitMqConfig [main] ****** -------------------------------------------------------- *****
2019-06-27 09:46:02,695 ERROR scrubber.configuration.RabbitMqConfig [main] Unable to get the rabbit subscriber UserId secrete from PPM
2019-06-27 09:46:02,695 ERROR scrubber.configuration.RabbitMqConfig [main] Abort application. calling... shutdown Manger
2019-06-27 09:46:02,695 ERROR scrubber.configuration.RabbitMqConfig [main] ****** -------------------------------------------------------- *****

Templates Connection Factory trying to connect comment 2019-06-27 09:46:02,837 INFO org.springframework.amqp.rabbit.connection.AbstractConnectionFactory [main] Attempting to connect to: [NCBXMN.cb.domain.com:5672, Anbasjk.cb.domain.com:5672]

1

1 Answers

0
votes

You should also stop whatever is calling the RabbitTemplate.

What version are you using? With modern versions, the connection factory disallows creation of a connection after it has been destroy()ed - which happens when you close the context.

        if (this.stopped) {
            throw new AmqpApplicationContextClosedException(
                    "The ApplicationContext is closed and the ConnectionFactory can no longer create connections.");
        }

destroy() sets stopped if it is called after it receives a ContextClosedEvent.