Quick Getting Started Guide to the RabBitmq framework in the Java class library
Rabbitmq is a powerful message queue middleware that is used to build a reliable, flexible and scalable distributed system.This article will provide you with a fast entry guide for the Rabbitmq framework to help you understand how to use it in the Java library.We will also provide some Java code examples to help you better understand and apply this framework.
Step 1: Install Rabbitmq
Before starting, you need to install Rabbitmq.You can download and install from the official website of Rabbitmq (https://www.rabbitmq.com/) and install it in accordance with the installation guide provided by them.
Step 2: Add dependencies
Use Rabbitmq in the Java project, you need to add the corresponding library to your project dependence.You can use Maven or Gradle to manage your project dependence.The following is a maven example pom.xml file, which is used to add Rabbitmq dependencies:
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.12.0</version>
</dependency>
</dependencies>
Step 3: Connect to Rabbitmq
Connect to Rabbitmq in the Java code, you need to create a connection factory and use it to create a connection.The following is an example code:
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class RabbitMQConnection {
private static final String HOST = "localhost";
private static final int PORT = 5672;
private static final String USERNAME = "guest";
private static final String PASSWORD = "guest";
public static Connection getConnection() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST);
factory.setPort(PORT);
factory.setUsername(USERNAME);
factory.setPassword(PASSWORD);
return factory.newConnection();
}
public static void main(String[] args) {
try {
Connection connection = getConnection();
System.out.println("Connected to RabbitMQ!");
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the above code, we create a connection to RabbitMQ by setting up the relevant attributes of the factory (such as host name, port, user name, and password).Then, we print out the news of successful connection and close the connection.
Step 4: Send and receive message
In Rabbitmq, the sending and receiving of the message is performed through switches and queues.The following is an example code that demonstrates how to send messages to the queue and receive the message from the queue:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
public class RabbitMQExample {
private static final String QUEUE_NAME = "hello";
public static void send() throws Exception {
Connection connection = RabbitMQConnection.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello, RabbitMQ!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("Sent message: " + message);
channel.close();
connection.close();
}
public static void receive() throws Exception {
Connection connection = RabbitMQConnection.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.basicConsume(QUEUE_NAME, true, (consumerTag, delivery) -> {
String message = new String(delivery.getBody());
System.out.println("Received message: " + message);
}, consumerTag -> {});
Thread.sleep (2000); // Time to receive messages
channel.close();
connection.close();
}
public static void main(String[] args) {
try {
send();
receive();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the above code, we created a queue called "Hello" and sent the message to the queue by calling the `BasicPublish` method.We then defined a consumer to receive the message and print it out when receiving the message.
Finally, we call the `send` method in the` main` method to send messages to the queue, and then call the `Receive` method to receive the message.
The above is the fast entry guide of the Rabbitmq framework in the Java class library.By connecting to Rabbitmq, sending messages, and receiving message, you can start to build a reliable distributed system based on Rabbitmq.Hope this article will help you!