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

问题描述 投票:0回答:1

我正在练习 Spring 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管理的交换图上查看,但是当我进入队列选项卡时,它说队列为空

java spring-boot rabbitmq spring-rabbit
1个回答
0
投票

根据问题中的代码,您刚刚错过了

@Bean
方法上的
binding()
注释。

使用您的代码将其添加到测试应用程序后,我在队列中正确收到一条消息:

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
© www.soinside.com 2019 - 2024. All rights reserved.