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'
RabbitmqApplication.java
package org.code2care.rabbitmq;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableRabbit
public class RabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqApplication.class, args);
}
}
RabbitMQConfig.java
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;
@Configuration
public class RabbitMQConfig {
private static final String EXCHANGE_NAME = "processor_exchange";
private static final String QUEUE_NAME = "collector_queue";
@Bean
public TopicExchange exchange() {
System.out.println("Topic Exchange created!");
return new TopicExchange(EXCHANGE_NAME);
}
@Bean
public Queue queue() {
return new Queue(QUEUE_NAME, true);
}
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
}
TopicMessageConsumer.java
package org.code2care.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@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

Received a message: Hey there!
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Cannot start Android Studio. No JDK found - Android-Studio
- Mac Auto Move Mouse Pointer Every X Seconds to Keep Screen Awake - MacOS
- HTML Images - Attributes and Formats - Html
- Steps to Kill a Running Process in Ubuntu Linux - Ubuntu
- Project JDK is not defined [IntelliJ IDEA] - Java
- Hotstar Disney+ mobile app test push notification - News
- Fetch as Google Crawl Error or Redirected Status - Google
- How to insert image into Google Sheets cell - Google