RabbitMQ 一二事 - 简单队列使用
生活随笔
收集整理的這篇文章主要介紹了
RabbitMQ 一二事 - 简单队列使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
消息隊列目前流行的有三種
1. RabbitMQ
2. ActiveMQ
3.?Kafka
這三種都非常強大,RabbitMQ目前用的比較多,也比較流行,阿里也在用
ActiveMQ是阿帕奇出品,但是性能上和RMQ相比相對差一些
卡夫卡呢,使用場景不同,不多介紹,主要是用于日志收集方面,結合hadoop非常靈活
?
RabbitMQ官網:http://www.rabbitmq.com/
安裝不多說了,可以下載Windows版本,或者linux版本 下載頁面:http://www.rabbitmq.com/download.html
我在linux虛擬機上安裝的,安裝步驟簡單,rpm直接安裝就行,步驟就略了?
成功后可以看到如下頁面:
?
簡答隊列圖
pom方面需要引入如下jar包
1 <dependencies> 2 3 <dependency> 4 <groupId>com.rabbitmq</groupId> 5 <artifactId>amqp-client</artifactId> 6 <version>3.4.1</version> 7 </dependency> 8 9 <dependency> 10 <groupId>org.slf4j</groupId> 11 <artifactId>slf4j-log4j12</artifactId> 12 <version>1.7.7</version> 13 </dependency> 14 15 <dependency> 16 <groupId>org.apache.commons</groupId> 17 <artifactId>commons-lang3</artifactId> 18 <version>3.3.2</version> 19 </dependency> 20 21 <dependency> 22 <groupId>org.springframework.amqp</groupId> 23 <artifactId>spring-rabbit</artifactId> 24 <version>1.5.6.RELEASE</version> 25 </dependency> 26 27 </dependencies>定義一個類似連接池的類
public class ConnectionUtil {public static Connection getConnection() throws Exception {// 定義連接工廠ConnectionFactory factory = new ConnectionFactory();// 設置服務地址factory.setHost("192.168.1.205");// 端口factory.setPort(5672);// 設置賬號信息,用戶名、密碼、vhostfactory.setVirtualHost("lee-shop");factory.setUsername("lee");factory.setPassword("lee");// 通過工程獲取連接Connection connection = factory.newConnection();return connection;}}創(chuàng)建生產者
1 public class Send { 2 3 private final static String QUEUE_NAME = "test_queue"; 4 5 public static void main(String[] argv) throws Exception { 6 // 獲取到連接以及mq通道 7 Connection connection = ConnectionUtil.getConnection(); 8 // 從連接中創(chuàng)建通道 9 Channel channel = connection.createChannel(); 10 11 // 聲明(創(chuàng)建)隊列 12 channel.queueDeclare(QUEUE_NAME, false, false, false, null); 13 14 // 消息內容 15 String message = "Hello World!"; 16 channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); 17 System.out.println(" [x] Sent '" + message + "'"); 18 19 // 關閉通道和連接 20 channel.close(); 21 connection.close(); 22 } 23 }創(chuàng)建消費者
1 public class Recv { 2 3 private final static String QUEUE_NAME = "test_queue"; 4 5 public static void main(String[] argv) throws Exception { 6 7 // 獲取到連接以及mq通道 8 Connection connection = ConnectionUtil.getConnection(); 9 Channel channel = connection.createChannel(); 10 11 // 聲明隊列 12 channel.queueDeclare(QUEUE_NAME, false, false, false, null); 13 14 // 定義隊列的消費者 15 QueueingConsumer consumer = new QueueingConsumer(channel); 16 // 監(jiān)聽隊列 17 channel.basicConsume(QUEUE_NAME, true, consumer); 18 19 // 獲取消息 20 while (true) { 21 QueueingConsumer.Delivery delivery = consumer.nextDelivery(); 22 String message = new String(delivery.getBody()); 23 System.out.println(" [x] Received '" + message + "'"); 24 } 25 } 26 }debug的時候可以進入rmq的管理頁面查看對于的連接數,頻道,以及消息隊列:
消費者接受到的消息:
?
對應的官網英文文檔如下:
http://www.rabbitmq.com/getstarted.html
?
總結
以上是生活随笔為你收集整理的RabbitMQ 一二事 - 简单队列使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pycon-python 会议
- 下一篇: RabbitMQ 一二事(2) - 工作