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);
        // 设置账号信息,用户名、密码、vhost
        factory.setVirtualHost("lee-shop");
        factory.setUsername("lee");
        factory.setPassword("lee");
        // 通过工程获取连接
        Connection connection = factory.newConnection();
        return connection;
    }

}

创建生产者

 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         // 从连接中创建通道
 9         Channel channel = connection.createChannel();
10 
11         // 声明(创建)队列
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 }

创建消费者

 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         // 监听队列
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

原文地址:https://www.cnblogs.com/leechenxiang/p/5516659.html