一、环境
1、下载安装Erlang
下载地址:http://www.erlang.org/download
2、下载安装RabbitMQ
下载地址:http://www.rabbitmq.com/download.html
3、启用管理工具
安装完成后,开始菜单会出现如下列表:
(1) RabbitMQ Service – (re)install
(2) RabbitMQ Service – start
4、查看管理页面
在浏览器中输入地址查看:http://127.0.0.1:15672/
默认账号登录:guest / guest
二、代码
生产者将消息发送到队列,消费者从队列中获取消息。
1、导入RabbitMQ的客户端依赖
- <dependency>
- <groupId>com.rabbitmq</groupId>
- <artifactId>amqp-client</artifactId>
- <version>3.4.1</version>
- </dependency>
2、获取MQ的连接
- package com.zxl.rabbitmq.util;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.Connection;
- /**
- * 获取MQ的连接
- *
- * @author chouasahiryou
- * @time 2019/08/09
- */
- public class ConnectionUtil {
- public static Connection getConnection() throws Exception {
- // 定义连接工厂
- ConnectionFactory connectionFactory = new ConnectionFactory();
- // 设置服务地址
- connectionFactory.setHost("localhost");
- // 端口
- connectionFactory.setPort(5672);
- // 设置账号信息,用户名、密码、vhost
- connectionFactory.setUsername("guest");
- connectionFactory.setPassword("guest");
- connectionFactory.setVirtualHost("/");
- // 2通过连接工厂创建连接
- Connection connection = connectionFactory.newConnection();
- // 通过工程获取连接
- return connection;
- }
- }
3、生产者发送消息到队列(执行一次)
- package com.zxl.rabbitmq.simple;
- import com.zxl.rabbitmq.util.ConnectionUtil;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- /**
- * 生产者发送消息到队列
- *
- * @author chouasahiryou
- * @time 2019/08/09
- */
- public class Send {
- private final static String QUEUE_NAME = "q_test_01";
- public static void main(String[] argv) throws Exception {
- // 获取到连接以及mq通道
- Connection connection = ConnectionUtil.getConnection();
- // 从连接中创建通道
- 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(" [x] Sent '" + message + "'");
- // 关闭通道和连接
- channel.close();
- connection.close();
- }
- }
4、管理工具中查看消息
点击上面的队列名称,查询具体的队列中的信息:
5、消费者从队列中获取消息
- package com.zxl.rabbitmq.simple;
- import com.zxl.rabbitmq.util.ConnectionUtil;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.QueueingConsumer;
- /**
- * 消费者从队列中获取消息
- *
- * @author chouasahiryou
- * @time 2019/08/09
- */
- public class Recv {
- private final static String QUEUE_NAME = "q_test_01";
- public static void main(String[] argv) throws Exception {
- // 获取到连接以及mq通道
- Connection connection = ConnectionUtil.getConnection();
- // 从连接中创建通道
- Channel channel = connection.createChannel();
- // 声明队列
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- // 定义队列的消费者
- QueueingConsumer consumer = new QueueingConsumer(channel);
- // 监听队列
- channel.basicConsume(QUEUE_NAME, true, consumer);
- // 获取消息
- while (true) {
- QueueingConsumer.Delivery delivery = consumer.nextDelivery();
- String message = new String(delivery.getBody());
- System.out.println(" [x] Received '" + message + "'");
- }
- }
- }
运行结果: