RabbitMQ消息显示在交换中,但队列为空

w9apscun  于 4个月前  发布在  RabbitMQ
关注(0)|答案(1)|浏览(66)

我正在练习Sping Boot RabbitMQ。我有一些关于队列的问题,就像我在问题中提到的。我认为我配置RabbitMQ的方式是正确的。这里是我的配置文件;

@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmq.queue.name}")
    private String queue;

    @Value("${rabbitmq.exchange.name}")
    private String exchange;

    @Value("${rabbitmq.routing.key}")
    private String routingKey;

    @Bean
    public Queue queue(){
        return new Queue(queue);
    }
        // spring bean for rabbitmq exchange
    @Bean
    public TopicExchange exchange(){
        return new TopicExchange(exchange);
    }

        // binding between queue and exchange using routing key
    public Binding binding(){
        return BindingBuilder
                .bind(queue())
                .to(exchange())
                .with(routingKey);
    }

字符串
这是我的制作人档案

@Service
public class RabbitMQProducer {

    @Value("${rabbitmq.exchange.name}")
    private String exchange;

    @Value("${rabbitmq.routing.key}")
    private String routingKey;

    private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQProducer.class);

    private RabbitTemplate rabbitTemplate;

    public RabbitMQProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMessage(String message){
        LOGGER.info(String.format("Message sent -> %s", message));
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }

}


所以这种情况有什么问题。我可以发送消息,并在rabbitmq管理上的交换图上看到,但当我进入队列选项卡时,它说队列是空的

b4qexyjb

b4qexyjb1#

根据问题中的代码,您刚刚错过了binding()方法上的@Bean注解。
在将它与您的代码一起添加到测试应用程序中之后,我在队列中正确地获得了一条消息:

Exchange    so-77612007-exchange
Routing Key     so-77612007-routing-key
Redelivered     ○
Properties  
priority:   0
delivery_mode:  2
headers:    
content_encoding:   UTF-8
content_type:   text/plain
Payload
12 bytes
Encoding: string
    

test message

字符串

相关问题