Step 1: Add RabbitMQ Spring Boot Dependecny
Make sure to add the rabbitMQ Spring boot dependency in your maven/gradle configuration file.
spring-boot-starter-amqp
Step 2: Create Topic Exchange and Queue
@Configuration
public class RabbitMQConfig {
private static final String EXCHANGE_NAME = "rmq_c2c_exchange";
private static final String QUEUE_NAME = "rmq_c2c_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);
}
}
Step 3: Create the Queue Listener @RabbitListener
Annotate your method with @RabbitListener to make it a queue listener.
@Component
public class RmqQueueListener {
@RabbitListener(queues = "rmq_c2c_queue")
public void processMessage(String message) {
System.out.println("Message: " + message);
}
}
Step 4: Add RabbitMQ Properties in application.properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Step 5: Create a RabbitMQ docker instance
run -d --hostname uat-rabbitmq1 --name uat-rabbitmq1 -p 15672:15672 -p 5672:5672 rabbitmq:management
Step 5: Publush a message
Run the Spring Boot Application and login to the RabbitMQ Management client on your browser at http://localhost:15672 and go to Queue tab and publish a message.

On your IDE console you should see the message received by the RabbitMQ Listener method.
-
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:
- How to Clear All Cells Output in Jupyter Notebook - Python
- How to Convert Python String to DateTime Object - Python
- How to know if someone has read your WhatsApp message - WhatsApp
- How to turn off Facebook autoplay videos on timeline - Facebook
- Copy file from one directory to other in Php - PHP
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement - Java
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows - Git
- Read file from Windows CMD (Command Line) - Windows