Overview
How RabbitMQ Works and RabbitMQ Core Concepts
Prerequisite
Java RabbitMQ Publish/Subscribe Example
Tools and technologies used
- RabbitMQ Java client- 5.5.1
- IDE - Eclipse Noen
- Maven 3.5.3
- JavaSE - 1.8
Development Steps
- Create a Simple Maven Project
- Project Directory Structure
- Add jar Dependencies to pom.xml
- Create EmitLog(Publisher)
- Create ReceiveLogs(Subscriber)
- Run an Application
1. Create a Simple Maven Project
3. Add jar Dependencies to pom.xml
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.javaguides.rabbitmq</groupId>
<artifactId>rabbitmq-publisher-subscriber-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.5.1</version>
</dependency>
</dependencies>
</project>
4. Create an EmitLog Program
package net.javaguides.rabbitmq.pubsub;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class EmitLog {
private static final String EXCHANGE_NAME = "pub-sub-queue";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
for (int i = 0; i < 10; i++) {
String message = "Helloworld message - " + i;
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
}
}
}
}
5. Create ReceiveLogs
package net.javaguides.rabbitmq.pubsub;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
/**
* Recieve logs program
* @author Ramesh Fadatare
*
*/
public class ReceiveLogs {
private static final String EXCHANGE_NAME = "pub-sub-queue";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) - > {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(queueName, true, deliverCallback, consumerTag - > {});
}
}
6. Running Programs
Related Articles and Tutorials
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Hi thank you for your clip, I have a question that didn't found solution on gg, this is when rabbitmq server Down, then the spring boot application is going to loop retry connect to rabbitmq and all other services don't work properly. What I want is the spring boot just show a message (maybe console) that inform if rabbitmq server down, and other services (not related rabbitmq) work normally and when it re-connect rabbitmq success it show a message to inform rabbitmq server Up. Could you give a brief way to do that? Thank you!
ReplyDelete