如何在不启动监听器容器且未调用模板的情况下终止Spring-Boot Rabbitmq应用程序

时间:2019-06-27 15:27:18

标签: spring-boot spring-amqp spring-rabbitmq spring-rabbit

我正在开发Spring Boot应用程序,该应用程序从外部源读取Rabbit凭证。当从该来源读取凭据时遇到问题,我需要终止应用程序。 1.我有兔子听众 2.处理器 3.以及发行人的机密手册Ack。

**

  

问题

** 当出现错误时,我正在调用ConfigurableApplicationContext.close()。 但是仍然我的Rabbit模板被实例化并尝试连接到Rabbit MQ。

  

尝试解决方案

  • 我尝试在应用程序中使用它    @EnableAutoConfiguration(exclude = RabbitAutoConfiguration.class)
  • spring.autoconfigure.exclude = true
  • spring.rabbitmq.listener.simple.auto-startup = false 以上选项均不适用于我。如何从故障点退出应用程序中止。

我的代码如下


@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:

我正在使用spring-rabbit-2.1.5.RELEASE和spring-starter-amqp-2.1.4.RELEASE Rabbit模板的名称如下:


    @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;

    }

  

日志:   发生错误评论

2019-06-27 09:46:01,626信息scrubber.util.AIMPropertiesUtil [main] ----启动createRequestProperties -----
2019-06-27 09:46:01,628 INFO scrubber.util.AIMPropertiesUtil [main] ----结束createRequestProperties -----
2019-06-27 09:46:01,630 INFO scrubber.service.AimSecretRequesterService [main] START requestContent将进行3次尝试。
2019-06-27 09:46:02,688错误scrubber.service.AimSecretRequesterService [main]系统错误secret.management.SecretRequestException获取秘密失败[检索秘密失败 2019-06-27 09:46:02,693错误scrubber.service.AimSecretRequesterService [main]错误堆栈{}
secret.management.SecretRequestException:获取密码失败[检索密码失败!]!

调用cntx.close() //评论     2019-06-27 09:46:02,695错误scrubber.configuration.RabbitMqConfig [main] ****** ------------------------- ------------------------------- *****
  2019-06-27 09:46:02,695错误scrubber.configuration.RabbitMqConfig [main]无法从PPM获取Rabbit用户的UserId密码
  2019-06-27 09:46:02,695错误scrubber.configuration.RabbitMqConfig [main]中止应用程序。正在呼叫...关机经理
  2019-06-27 09:46:02,695错误scrubber.configuration.RabbitMqConfig [main] ****** ------------------------- ------------------------------- *****

试图连接的模板连接工厂评论   2019-06-27 09:46:02,837信息org.springframework.amqp.rabbit.connection.AbstractConnectionFactory [main]尝试连接到:[NCBXMN.cb.domain.com:5672,Anbasjk.cb.domain.com:5672 ]

1 个答案:

答案 0 :(得分:0)

您还应该停止调用RabbitTemplate的任何操作。

您使用的是哪个版本?对于现代版本,连接工厂在destroy()建立连接后将禁止创建连接-当您关闭上下文时会发生这种情况。

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

destroy()设置stopped,如果它在收到ContextClosedEvent之后被调用。