Creating Queues, Exchanges and Bindings in RabbitMQ with Spring Boot

Heitor Furtado
2 min readJun 19, 2022

--

Now that we already have RabbitMQ cluster up and running and the connection configured in a java application with Spring Boot, we can create our queues, exchanges and routing keys using Spring Boot.

Previous stories:

In this simulation we are sending a dummy payment to another system such as a fraud checker for example. We will create 1 direct exchange, 1 queue and 1 binding.

Let’s create a @Configuration or @Component class for create the queues, exchanges and routing keys in RabbitMQ cluster if they don’t already exist. You can use your RabbitMQConfig class that we already created before if you prefer, but this way things are more organized

Creating the Exchange

We’ll create a durable, non-auto-deleting direct exchange using the classes DirectExchange or with ExchangeBuilder from org.springframework.amqp.core.

public DirectExchange(String name, boolean durable, boolean autoDelete)

Example:

Creating the Queue

Creating a durable queue using the classes Queue or QueueBuilder from org.springframework.amqp.core.

public Queue(String name, boolean durable)

Example:

Creating the Binding

Now we need to bind our exchange to our queue. We will doing this using the classes Binding or BindingBuilder

public Binding(String destination, DestinationType destinationType, String exchange, String routingKey,
@Nullable Map<String, Object> arguments)

Example:

Obs: the queue( ) and the directExchange( ) in the builder are already created above.

Code Example

application.yml

All the information about queues, exchanges and routing keys are declared in our application.yml or application.properties

AMQPPaymentConfig.java

Putting all together:

--

--

Heitor Furtado

Java developer, always learning new things and trying to improve my english