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,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to install homebrew (brew) on M1 Mac - MacOS
- Comprehensive CSS Tutorial with Hands-on Lessons (Revision) - CSS
- Check Reboot History Mac - MacOS
- Convert Multidimensional Array toString In Java - Java
- Download a SSL Certificate from a URL in Terminal - Bash
- Fix: Spring Boot + Caching: DefaultSerializer requires a Serializable payload - Java
- Python: How to add Progress Bar in Console with Examples - Python
- How to Change Mac Computer and Localhost Name using Terminal - MacOS