Spring-rabbitmq - start the Spring-boot server even if there is no connection

I am using spring boot with RabbitMQ. Everything works - messages are processed, and after the connection is lost, it automatically tries to restore the connection. However

I only have one problem:
When the Rabbit server is down (no way to establish a connection) and I try to start the spring-boot server it cannot start. I cannot verify (without machine access) what the exact content of the exception is, however it was due to a problem with setting the 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 have to edit because I was unaware of what's important here. In my case, the last argument is not null, it is some kind of hashmap (this is important to me). I have edited my code above.

Moreover, I do not exactly understand your answer. Could you be more precise?
To make sure I was clear enough: I would like to take advantage of automatic reconnection (it works now). Also, if during spring startup, the browser-browser server shuts down , it should start up and cycle through to try to reconnect (the application is not starting at the moment).

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);
        }    

     }

      

+3


source to share


1 answer


It is right. You are trying to register Broker objects manually:

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

      



You have to rely on the built-in automatic declaration mechanism in the Framework here .

In other words: you can declare those beans (including Binding

m BTW), but you shouldn't be calling at all rabbitAdmin.declare

. At least not from the bean definition phase.

0


source







All Articles