Skip to main content

Getting Started

Description

  • Create a Sender
  • Create a Reciever
  • Run an Application

Prerequisites

  • JDK 21
  • Maven
  • Docker
  • IntelliJ IDEA

Project Structure

.
├── pom.xml
└── src
├── main
│   ├── java
│   │   └── org
│   │   └── example
│   │   ├── CommonConfigs.java
│   │   ├── MessagePublisher.java
│   │   └── MessageSubscriber.java
│   └── resources
└── test
└── java

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>rabbitmq-getting-started</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.24.0</version>
</dependency>
</dependencies>
</project>

CommonConfigs.java

package org.example;

public class CommonConfigs {
public static final String DEFAULT_QUEUE = "Queue-Getting-Started";
public static final String AMQP_URL = "amqp://guest:guest@localhost:5672/";
}

MessagePublisher.java

package org.example;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class MessagePublisher {
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection(CommonConfigs.AMQP_URL);
Channel channel = connection.createChannel();

channel.queueDeclare(CommonConfigs.DEFAULT_QUEUE, true, false, false, null);
for (int i = 0; i < 4; i++) {
String message = CommonConfigs.DEFAULT_QUEUE + " with rabbitMQ - Msg" + i;
//publish - (exchange, routingKey, properties, message)
channel.basicPublish("", CommonConfigs.DEFAULT_QUEUE, null, message.getBytes());
}
channel.close();
connection.close();
}
}

MessageSubscriber.java

package org.example;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MessageSubscriber {

public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection(CommonConfigs.AMQP_URL);
Channel channel = connection.createChannel();

DeliverCallback deliverCallback = (s, delivery) -> {
System.out.println(new String(delivery.getBody(), "UTF-8"));
};

CancelCallback cancelCallback = s -> {
System.out.println(s);
};
channel.basicConsume(CommonConfigs.DEFAULT_QUEUE, true, deliverCallback, cancelCallback);
}
}

Running

Intellij IDE:

Run:

MessagePublisher

MessageSubscriber
Output:

Queue-Getting-Started with rabbitMQ - Msg0
Queue-Getting-Started with rabbitMQ - Msg1
Queue-Getting-Started with rabbitMQ - Msg2
Queue-Getting-Started with rabbitMQ - Msg3

Source Code:
https://github.com/ZbCiok/zjc-examples/tree/main/reactive-messaging/rabbitmq/rabbitmq-getting-started