简体   繁体   中英

Spring-rabbitmq - start spring-boot server even in case of lack of connection

I am using spring boot with RabbitMQ. Everything is working - processing messages works and after losing connection it automatically tries to reconnect. However,

I have only one problem:
When Rabbit server is swtiched off (no possibility to establish connection) and I try to launch spring-boot server it can't start. I can't check now (no access to machine) what exact content of exception is, however it was about problem with instatiation of beans. Can you help me ?

@Configuration
public class RabbitConfig{
       private String queueName = "myQueue";
       private String echangeName = "myExchange";

      @Bean
        public FanoutExchange exchange(RabbitAdmin rabbitAdmin)   {
            FanoutExchange exch = new 
             FanoutExchange(echangeName);
            rabbitAdmin.declareExchange(exch);
            return exch;
        }

        @Bean
        public Queue queue(FanoutExchange exchange, RabbitAdmin rabbitAdmin) {
        HashMap<String, Object> args = new HashMap<String, Object>();
        args.put("x-message-ttl", 20);
        args.put("x-dead-letter-exchange", "dlx_exchange_name");

            Queue queue = new Queue(queueName, true, false, false, args);
            rabbitAdmin.declareQueue(queue);
            rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange));
            return queue;
        }

 }

Edit
I must edit, because I was not aware of fact that is is important here. In my case the last argument is not null, it is some Hashmap (it is important for me). I edited my code above.

Moreover, I don't understand your answer exactly. Could you be more precisely ?
In order make sure that I was sufficiently clear: I would like to take advantage of automatic reconnection (now it is working). Additionally, If during starting spring boot server rabbit broker is shutdown it should start and cyclically try to reconnect (at this moment application doesn't start).

Edit2

  @Configuration
    public class RabbitConfig{
           private String queueName = "myQueue";
           private String echangeName = "myExchange";

          @Bean
            public FanoutExchange exchange(RabbitAdmin rabbitAdmin)   {
                FanoutExchange exch = new 
                 FanoutExchange(echangeName);
                //rabbitAdmin.declareExchange(exch);
                return exch;
            }

            @Bean
            public Queue queue(FanoutExchange exchange, RabbitAdmin rabbitAdmin) {
            HashMap<String, Object> args = new HashMap<String, Object>();
            args.put("x-message-ttl", 20);
            args.put("x-dead-letter-exchange", "dlx_exchange_name");

                Queue queue = new Queue(queueName, true, false, false, args);
                //rabbitAdmin.declareQueue(queue);
                //rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange));
                return queue;
            }
         // EDIT 3: now, we are made to create binding bean

        @Autowired
        Queue queue; // inject bean by name
        @Autowired
        Exchange exchange;

        @Bean
        public Binding binding() {
             return BindingBuilder.bind(queue.to(exchange);
        }    

     }

That's correct. You try to register Broker entities manually:

rabbitAdmin.declareExchange(exch);
...
rabbitAdmin.declareQueue(queue);
rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange));

You should rely here on the built-in auto-declaration mechanism in the Framework .

In other words: you're good to declare those beans (including Binding m BTW), but you have not to call rabbitAdmin.declare at all. At least not from the bean definition phase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM