Consume DirectExchange messages using routing key and exchange in spring boot

Am trying to consume the message from exiting queue which is of type Direct Exchange(created with the help of exchange and routing key). I have only the exchange name and routing key and not the queue name. There were support for plain Java, but there was no place where I can find it for Spring boot.

@RabbitListener
    @RabbitHandler
    public void consumeMessage(Object message) {
        LOGGER.debug("Message Consumed.... : {}", message.toString());
    }

How can I consume messages with routing key and exchange name not the queue name as @RabbitListener asks for queue.

Upvotes: 1

Views: 2786

Answers (1)

Gary Russell
Gary Russell

Reputation: 174564

Consumers consume from queues not exchanges. You must bind a queue to the exchange with the routing key.

EDIT

There are several ways to automatically declare a queue on the broker.

    @RabbitListener(bindings = 
        @QueueBinding(exchange = @Exchange("myExchange"), 
            key = "myRk", value = @Queue("")))
    public void listen(String in) {
        System.out.println(in);
    }

This will bind an anonymous queue (auto-delete) which will be deleted when the application is stopped.

    @RabbitListener(bindings = 
        @QueueBinding(exchange = @Exchange("myExchange"), 
            key = "myRk", value = @Queue("foo")))
    public void listen(String in) {
        System.out.println(in);
    }

Will bind a permanent queue foo to the exchange with the routing key.

You can also simply declare #Bean s for the queue, exchange and binding.

See Configuring the broker.

Upvotes: 1

Related Questions