当前位置:   article > 正文

rabbit MQ 自动创建队列_rabbitmq自动创建队列

rabbitmq自动创建队列

再写项目时将rabbitMq的创建放在了生产端,然后就出现了队列没有创建的错误,在查了资料之后找到了错误之处

  • mq的生产端

可通过配置 RabbitAdmin 来实现自动创建队列

源码:

this.rabbitTemplate.execute((channel) -> {
                                        this.declareExchanges(channel, (Exchange[])exchanges.toArray(new Exchange[exchanges.size()]));
                                        this.declareQueues(channel, (Queue[])queues.toArray(new Queue[queues.size()]));
                                        this.declareBindings(channel, (Binding[])bindings.toArray(new Binding[bindings.size()]));
                                        return null;
                                    });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用:

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory defaultConnectionFactory){
        return new RabbitAdmin(defaultConnectionFactory);
    }
    
    // 在初始化RabbitTemplate时  将队列创建
    
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory , RabbitAdmin rabbitAdmin) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        //成功回调
        template.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println(correlationData.toString());
                System.out.println(b);
                System.out.println(s);
            }
        });
        // 使用RabbitAdmin      =====>

        rabbitAdmin.initialize();
        
        // 开启mandatory模式(开启失败回调)
        template.setMandatory(true);
        //失败回调
        template.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                System.out.println(returnedMessage.getMessage());
                System.out.println(returnedMessage.getExchange());
                System.out.println(returnedMessage.getReplyCode());
                System.out.println(returnedMessage.getReplyText());
                System.out.println(returnedMessage.getRoutingKey());
            }
        });
        return template;
    }
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • mq 的消费端

配置 SimpleRabbitListenerContainerFactory 来实现自动创建队列

SimpleRabbitListenerContainerFactory  已经默认帮我们 创建了 RabbitAdmin对象
  • 1

使用:

//并发数量
    public static final int DEFAULT_MIN_CONCURRENT = 2;
    public static final int DEFAULT_MAX_CONCURRENT = 24;

    @Bean("customContainerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                 ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(DEFAULT_MIN_CONCURRENT);//
        factory.setMaxConcurrentConsumers(DEFAULT_MAX_CONCURRENT);//24
        configurer.configure(factory, connectionFactory);
        return factory;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

RabbitAdmin : 管理 channel 可调用 this.declareQueues 创建队列

https://cloud.tencent.com/developer/article/1668606

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/639637
推荐阅读
相关标签
  

闽ICP备14008679号