RabbitMQ-简单队列Demo

一、环境

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的客户端依赖

  1. <dependency>  
  2.    <groupId>com.rabbitmq</groupId>  
  3.    <artifactId>amqp-client</artifactId>  
  4.    <version>3.4.1</version>  
  5. </dependency>  

2、获取MQ的连接

  1. package com.zxl.rabbitmq.util;  
  2.   
  3. import com.rabbitmq.client.ConnectionFactory;  
  4. import com.rabbitmq.client.Connection;  
  5.   
  6. /** 
  7.  * 获取MQ的连接 
  8.  *  
  9.  * @author chouasahiryou 
  10.  * @time 2019/08/09 
  11.  */  
  12.   
  13. public class ConnectionUtil {  
  14.   
  15.     public static Connection getConnection() throws Exception {  
  16.         // 定义连接工厂  
  17.         ConnectionFactory connectionFactory = new ConnectionFactory();  
  18.         // 设置服务地址  
  19.         connectionFactory.setHost("localhost");  
  20.         // 端口  
  21.         connectionFactory.setPort(5672);  
  22.         // 设置账号信息,用户名、密码、vhost  
  23.         connectionFactory.setUsername("guest");  
  24.         connectionFactory.setPassword("guest");  
  25.         connectionFactory.setVirtualHost("/");  
  26.         // 2通过连接工厂创建连接  
  27.         Connection connection = connectionFactory.newConnection();  
  28.         // 通过工程获取连接  
  29.         return connection;  
  30.     }  
  31. }  

3、生产者发送消息到队列(执行一次)

  1. package com.zxl.rabbitmq.simple;  
  2.   
  3. import com.zxl.rabbitmq.util.ConnectionUtil;  
  4. import com.rabbitmq.client.Channel;  
  5. import com.rabbitmq.client.Connection;  
  6.   
  7. /** 
  8.  * 生产者发送消息到队列 
  9.  *  
  10.  * @author chouasahiryou 
  11.  * @time 2019/08/09 
  12.  */  
  13.   
  14. public class Send {  
  15.   
  16.     private final static String QUEUE_NAME = "q_test_01";  
  17.   
  18.     public static void main(String[] argv) throws Exception {  
  19.         // 获取到连接以及mq通道  
  20.         Connection connection = ConnectionUtil.getConnection();  
  21.         // 从连接中创建通道  
  22.         Channel channel = connection.createChannel();  
  23.         // 声明(创建)队列  
  24.         channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);  
  25.         // 消息内容  
  26.         String message = "Hello World!";  
  27.         channel.basicPublish("", QUEUE_NAME, null, message.getBytes());  
  28.         System.out.println(" [x] Sent '" + message + "'");  
  29.         // 关闭通道和连接  
  30.         channel.close();  
  31.         connection.close();  
  32.     }  
  33. }  

4、管理工具中查看消息

点击上面的队列名称,查询具体的队列中的信息:

 

5、消费者从队列中获取消息

  1. package com.zxl.rabbitmq.simple;  
  2.   
  3. import com.zxl.rabbitmq.util.ConnectionUtil;  
  4.   
  5. import com.rabbitmq.client.Channel;  
  6. import com.rabbitmq.client.Connection;  
  7. import com.rabbitmq.client.QueueingConsumer;  
  8.   
  9. /** 
  10.  * 消费者从队列中获取消息 
  11.  *  
  12.  * @author chouasahiryou 
  13.  * @time 2019/08/09 
  14.  */  
  15.   
  16. public class Recv {  
  17.   
  18.     private final static String QUEUE_NAME = "q_test_01";  
  19.   
  20.     public static void main(String[] argv) throws Exception {  
  21.   
  22.         // 获取到连接以及mq通道  
  23.         Connection connection = ConnectionUtil.getConnection();  
  24.         // 从连接中创建通道  
  25.         Channel channel = connection.createChannel();  
  26.         // 声明队列  
  27.         channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);  
  28.         // 定义队列的消费者  
  29.         QueueingConsumer consumer = new QueueingConsumer(channel);  
  30.         // 监听队列  
  31.         channel.basicConsume(QUEUE_NAME, true, consumer);  
  32.         // 获取消息  
  33.         while (true) {  
  34.             QueueingConsumer.Delivery delivery = consumer.nextDelivery();  
  35.             String message = new String(delivery.getBody());  
  36.             System.out.println(" [x] Received '" + message + "'");  
  37.         }  
  38.     }  
  39. }  

运行结果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值