Search code examples
javaspring-bootrabbitmqspring-rabbit

RabbitMQ message shown in exchange but queue is empty


I am practicing in Spring boot RabbitMQ. I have some problem about queue as i mentioned in question. I think i configured RabbitMQ right way. Here is my config file;

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

and here is my producer file;

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

}

so what is wrong with this situation. i can send messages and see on the exchange graph on rabbitmq management but when i go the queue tab, it says queue is empty


Solution

  • According to your code in the question you have just missed @Bean annotation on the binding() method.

    After adding it in the test application with your code I got a message in the queue properly:

    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