RabbitMQ
Java
Spring Framework
Topic Setup
Programming Configuration

How to setup multiple topics in a RabbitMQ Java config class using Spring Framework?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

RabbitMQ is a popular open-source message broker that supports multiple messaging protocols. It is particularly well-suited for handling complex messaging needs such as those in distributed systems. In Java applications using the Spring Framework, configuring multiple topics in RabbitMQ can be streamlined using Spring AMQP's support for RabbitMQ.

Prerequisites

Before setting up the configuration, ensure that:

  • Java SDK is installed.
  • Maven or Gradle is set up for dependency management.
  • RabbitMQ server is installed and running.
  • Spring Boot is used for simplifying configuration.

Add Dependencies

First, add the required dependencies in your pom.xml if you are using Maven:

xml
1<dependencies>
2    <dependency>
3        <groupId>org.springframework.boot</groupId>
4        <artifactId>spring-boot-starter-amqp</artifactId>
5    </dependency>
6    <dependency>
7        <groupId>org.springframework.boot</groupId>
8        <artifactId>spring-boot-starter-web</artifactId>
9        <!-- Optional: for web capabilities -->
10    </dependency>
11</dependencies>

For Gradle, include:

groovy
1dependencies {
2    implementation 'org.springframework.boot:spring-boot-starter-amqp'
3    implementation 'org.springframework.boot:spring-boot-starter-web' // Optional: for web capabilities
4}

Configuration Class

Create a configuration class in your Java project which will define the RabbitMQ setup. Here, configure the connection factory, message listener container, and topics (exchanges and queues).

java
1import org.springframework.amqp.core.Binding;
2import org.springframework.amqp.core.BindingBuilder;
3import org.springframework.amqp.core.TopicExchange;
4import org.springframework.amqp.core.Queue;
5import org.springframework.amqp.rabbit.connection.ConnectionFactory;
6import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
7import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
8import org.springframework.context.annotation.Bean;
9import org.springframework.context.annotation.Configuration;
10
11@Configuration
12public class RabbitMQConfig {
13
14    final static String queueName = "spring-boot";
15    final static String topicExchangeName = "spring-boot-exchange";
16
17    @Bean
18    Queue queue() {
19        return new Queue(queueName, false);
20    }
21
22    @Bean
23    TopicExchange exchange() {
24        return new TopicExchange(topicExchangeName);
25    }
26
27    @Bean
28    Binding binding(Queue queue, TopicExchange exchange) {
29        return BindingBuilder.bind(queue).to(exchange).with("topic.#"); // Routing key pattern
30    }
31
32    @Bean
33    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
34                                             MessageListenerAdapter listenerAdapter) {
35        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
36        container.setConnectionFactory(connectionFactory);
37        container.setQueueNames(queueName);
38        container.setMessageListener(listenerAdapter);
39        return container;
40    }
41
42    @Bean
43    MessageListenerAdapter listenerAdapter(Receiver receiver) {
44        return new MessageListenerAdapter(receiver, "receiveMessage");
45    }
46
47    // Define a receiver class within this configuration or as a separate Bean
48    public static class Receiver {
49        public void receiveMessage(String message) {
50            System.out.println("Received <" + message + ">");
51        }
52    }
53}

Explanation of Components

  • Queue: Defines the queue where messages are sent.
  • TopicExchange: Defines a topic exchange that dispatches messages to queues based on a pattern matching between the routing key and the pattern specified in the binding.
  • Binding: Defines the relationship between the exchange and the queue using a routing key.
  • SimpleMessageListenerContainer: Listens for messages from the queue and delivers them to the registered listener.
  • MessageListenerAdapter: Adapts the listener method to the listener container.

Summary of Configuration Components

ComponentDescriptionPurpose
QueueDefines the destination where messages are kept until consumed.Storage of messages
TopicExchangeA type of exchange that routes messages to one or many queues based on pattern matching.Message routing
BindingLinks a queue to an exchange with a specific routing key pattern.Routing rule definition
SimpleMessageListenerContainerPolls queues and delivers messages to listeners.Message consumption management
MessageListenerAdapterAdapts a Receiver method to the signature expected by the container.Translation and message handling

Running and Testing

Once the application is configured and running, you can send messages to your RabbitMQ exchange with the routing key that matches the specified pattern (e.g., topic.messages). The configured listener will receive the messages and process them as needed.

By organizing configuration in this way, you can easily scale up to multiple queues and topics, managing complex routing rules and processing needs in a Spring-based Java application effectively.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design