Java: RabbitMq Create a Queue Example

We will make use of Spring Boot RabbitMQ dependency to create our Topic Exchange and a Queue.

Make sure to add the spring-boot-starter-amqp to the build.gradle file.

implementation 'org.springframework.boot:spring-boot-starter-amqp'
package org.code2care.rabbitmq;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class RabbitmqApplication {

	public static void main(String[] args) {, args);
package org.code2care.rabbitmq;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class RabbitMQConfig {

    private static final String EXCHANGE_NAME = "processor_exchange";
    private static final String QUEUE_NAME = "collector_queue";

    public TopicExchange exchange() {
        System.out.println("Topic Exchange created!");
        return new TopicExchange(EXCHANGE_NAME);

    public Queue queue() {
        return new Queue(QUEUE_NAME, true);

    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);

package org.code2care.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

public class TopicMessageConsumer {

    @RabbitListener(queues = "collector_queue")
    public void processMessage(String message) {
        System.out.println("Received a message: " + message);

Let's test by publishing a message to our queue via the RabbitMQ Management Console on http://localhost:15672

RabbitMQ Queue Publish a Message
Received a message: Hey there!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap