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!
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: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- No CPU ABI system image available for this target Error Android Virtual Device - Android
- How to reset an Apple Watch without an iPhone - Apple
- Fix: Failed to configure a DataSource: url attribute is not specified [Spring Data JPA] - Java
- Show Hide SharePoint column in List Library form with the conditional formula - SharePoint
- Internet Explorer browser auto redirect to Microsoft Edge for compatibility with modern web sites - Microsoft
- How to change the default location of Mac Screenshot - MacOS
- Create RabbitMQ Exchange using Java Spring Boot Example - Java
- How to hard reset Mac Terminal Window - MacOS