以下是Java搭建RabbitMQ消息中间件过程详解的完整攻略,包含两个示例说明。
示例1:简单队列模式
步骤1:安装RabbitMQ
首先,您需要安装RabbitMQ。您可以从RabbitMQ官网下载适合您操作系统的安装包进行安装。
步骤2:添加依赖
在pom.xml文件中添加以下依赖:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.3</version>
</dependency>
步骤3:发送消息
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer {
private final static String QUEUE_NAME = "my_queue";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("Message sent: " + message);
}
}
}
步骤4:接收消息
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer {
private final static String QUEUE_NAME = "my_queue";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("Waiting for messages...");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("Message received: " + message);
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
}
步骤5:运行程序
运行发送消息的程序后,您将看到以下输出:
Message sent: Hello World!
运行接收消息的程序后,您将看到以下输出:
Waiting for messages...
Message received: Hello World!
示例2:工作队列模式
步骤1:安装RabbitMQ
同示例1。
步骤2:添加依赖
同示例1。
步骤3:发送消息
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer {
private final static String QUEUE_NAME = "my_queue";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
for (int i = 1; i <= 10; i++) {
String message = "Message " + i;
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("Message sent: " + message);
}
}
}
}
步骤4:接收消息
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer {
private final static String QUEUE_NAME = "my_queue";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("Waiting for messages...");
channel.basicQos(1);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("Message received: " + message);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Message processed");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
};
channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {
});
}
}
步骤5:运行程序
运行发送消息的程序后,您将看到以下输出:
Message sent: Message 1
Message sent: Message 2
Message sent: Message 3
Message sent: Message 4
Message sent: Message 5
Message sent: Message 6
Message sent: Message 7
Message sent: Message 8
Message sent: Message 9
Message sent: Message 10
运行接收消息的程序后,您将看到以下输出:
Waiting for messages...
Message received: Message 1
Message processed
Message received: Message 2
Message processed
Message received: Message 3
Message processed
Message received: Message 4
Message processed
Message received: Message 5
Message processed
Message received: Message 6
Message processed
Message received: Message 7
Message processed
Message received: Message 8
Message processed
Message received: Message 9
Message processed
Message received: Message 10
Message processed
以上就是Java搭建RabbitMQ消息中间件过程详解的完整攻略,包含两个示例说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java搭建RabbitMq消息中间件过程详解 - Python技术站