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!
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:
- How to do calculations in Mac Terminal - MacOS
- How to Go To /usr/local/bin on Mac Terminal? - MacOS
- Show Chrome Developer Console Keyboard Shortcut on macOS - Chrome
- Android Constant and Resource Type Mismatches Lint - Android
- How to make ZSH as the default shell on Ubuntu - zsh
- Create an AWS User using CLI2 Command at a specific Path - AWS
- Is Facebook is down? Is it just for me? - Facebook
- How to Upload Video to YouTube from Android - Android