RabbitMQ 一二事 - 简单队列使用
消息隊(duì)列目前流行的有三種
1. RabbitMQ
2. ActiveMQ
3.?Kafka
這三種都非常強(qiáng)大,RabbitMQ目前用的比較多,也比較流行,阿里也在用
ActiveMQ是阿帕奇出品,但是性能上和RMQ相比相對(duì)差一些
卡夫卡呢,使用場(chǎng)景不同,不多介紹,主要是用于日志收集方面,結(jié)合hadoop非常靈活
?
RabbitMQ官網(wǎng):http://www.rabbitmq.com/
安裝不多說(shuō)了,可以下載Windows版本,或者linux版本 下載頁(yè)面:http://www.rabbitmq.com/download.html
我在linux虛擬機(jī)上安裝的,安裝步驟簡(jiǎn)單,rpm直接安裝就行,步驟就略了?
成功后可以看到如下頁(yè)面:
?
簡(jiǎn)答隊(duì)列圖
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>定義一個(gè)類似連接池的類
public class ConnectionUtil {public static Connection getConnection() throws Exception {// 定義連接工廠ConnectionFactory factory = new ConnectionFactory();// 設(shè)置服務(wù)地址factory.setHost("192.168.1.205");// 端口factory.setPort(5672);// 設(shè)置賬號(hào)信息,用戶名、密碼、vhostfactory.setVirtualHost("lee-shop");factory.setUsername("lee");factory.setPassword("lee");// 通過(guò)工程獲取連接Connection connection = factory.newConnection();return connection;}}創(chuàng)建生產(chǎn)者
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)建)隊(duì)列 12 channel.queueDeclare(QUEUE_NAME, false, false, false, null); 13 14 // 消息內(nèi)容 15 String message = "Hello World!"; 16 channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); 17 System.out.println(" [x] Sent '" + message + "'"); 18 19 // 關(guān)閉通道和連接 20 channel.close(); 21 connection.close(); 22 } 23 }創(chuàng)建消費(fèi)者
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 // 聲明隊(duì)列 12 channel.queueDeclare(QUEUE_NAME, false, false, false, null); 13 14 // 定義隊(duì)列的消費(fèi)者 15 QueueingConsumer consumer = new QueueingConsumer(channel); 16 // 監(jiān)聽(tīng)隊(duì)列 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的時(shí)候可以進(jìn)入rmq的管理頁(yè)面查看對(duì)于的連接數(shù),頻道,以及消息隊(duì)列:
消費(fèi)者接受到的消息:
?
對(duì)應(yīng)的官網(wǎng)英文文檔如下:
http://www.rabbitmq.com/getstarted.html
?
總結(jié)
以上是生活随笔為你收集整理的RabbitMQ 一二事 - 简单队列使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: pycon-python 会议
- 下一篇: RabbitMQ 一二事(2) - 工作