Build a Spring AMQP RabbitMQ Publish‑Subscribe App in Minutes

This step‑by‑step guide shows how to set up a RabbitMQ server, create a Spring Boot project with Gradle or Maven, define a message receiver, configure queues, exchanges and bindings, and send and receive text messages using RabbitTemplate and a CommandLineRunner.

Programmer DD
Programmer DD
Programmer DD
Build a Spring AMQP RabbitMQ Publish‑Subscribe App in Minutes

What You’ll Build

You will create a simple Spring Boot application that publishes a text message to a RabbitMQ queue and receives it with a POJO listener.

Prerequisites

About 15 minutes

Java 8+ and an IDE (STS or IntelliJ IDEA)

Gradle 2.3+ or Maven 3.0+

RabbitMQ installed locally (or via Docker)

Install RabbitMQ

On macOS you can use Homebrew: brew install rabbitmq Start the broker: rabbitmq-server Or run it with Docker Compose:

version: '3'
services:
  rabbitmq:
    image: rabbitmq:management
    ports:
      - "5672:5672"
      - "15672:15672"

Project Structure

Both Gradle and Maven use the standard layout:

src/main/java/hello/...
src/main/resources/

Gradle Build File (build.gradle)

buildscript {
    repositories { mavenCentral() }
    dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE" }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'gs-spring-boot'
    version = '0.1.0'
}

repositories { mavenCentral() }

dependencies {
    compile "org.springframework.boot:spring-boot-starter-amqp"
    compile "org.springframework.boot:spring-boot-starter-web"
    testCompile "junit:junit"
}

Maven Build File (pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-messaging-rabbitmq</artifactId>
    <version>0.1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Create the Message Receiver (Receiver.java)

package hello;

import java.util.concurrent.CountDownLatch;
import org.springframework.stereotype.Component;

@Component
public class Receiver {
    private CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }

    public CountDownLatch getLatch() {
        return latch;
    }
}

Configure Spring Beans (Application.java)

package hello;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    static final String queueName = "spring-boot";

    @Bean
    Queue queue() { return new Queue(queueName, false); }

    @Bean
    TopicExchange exchange() { return new TopicExchange("spring-boot-exchange"); }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory cf, MessageListenerAdapter mla) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(cf);
        container.setQueueNames(queueName);
        container.setMessageListener(mla);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Send a Text Message (Runner.java)

package hello;

import java.util.concurrent.TimeUnit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Runner implements CommandLineRunner {
    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;
    private final ConfigurableApplicationContext context;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate, ConfigurableApplicationContext context) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
        this.context = context;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
        context.close();
    }
}

Running the Application

Execute the main method of Application. Spring Boot will start the listener container, the Runner will publish "Hello from RabbitMQ!" to the spring-boot queue, the receiver prints the message, and the context shuts down.

Build an Executable JAR

With Gradle:

./gradlew build
java -jar build/libs/gs-messaging-rabbitmq-0.1.0.jar

With Maven:

./mvnw clean package
java -jar target/gs-messaging-rabbitmq-0.1.0.jar

Conclusion

You now have a working publish‑subscribe example using Spring AMQP and RabbitMQ. The same patterns can be extended to more complex messaging scenarios, different exchange types, or other Spring Boot applications.

This guide is licensed under CC BY‑NC‑SA 4.0.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Messagingspring-bootspring-amqp
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.