RabbitMQ的使用

 一 下载安装和运行

1 Erlang:otp_win64_20.0.exe

2 消息代理中间件:rabbitmq-server-3.6.11.exe

安装完成后

3 开启管理插件:rabbitmq-plugins enable rabbitmq_management

4 查看插件:rabbitmq-plugins list

5 测试

二 编写消息生产者

1 添加依赖

      <dependencies>
            <dependency>
                  <groupId>com.rabbitmq</groupId>
                  <artifactId>amqp-client</artifactId>
                  <version>4.2.0</version>
            </dependency>
            <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-log4j12</artifactId>
                  <version>1.7.9</version>
            </dependency>
      </dependencies>

2 编写消息发送

package org.crazyit.cloud;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {

    public static void main(String[] args) throws Exception {
        //实例化连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //默认设置,可不设置
//        factory.setHost("localhost");
        //新建连接
        Connection conn = factory.newConnection();
        //新建通道
        Channel channel = conn.createChannel();
        //给队列命名
        String queueName = "hello";
        //通道和队列绑定
        channel.queueDeclare(queueName, false, false, false, null);
        //定义要发送的消息
        String message = "send a message,please receive!";
        //给指定队列发布消息
        channel.basicPublish("", queueName, null, message.getBytes());
        //关闭相关资源
        channel.close();
        conn.close();
    }

}

3 运行

4 观察RabbitMQ发生的变化

三 编写消息消费者

package org.crazyit.cloud;

import java.io.IOException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Receive {

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        Connection conn = factory.newConnection();
        Channel channel = conn.createChannel();
        
        String queueName = "hello";
        channel.queueDeclare(queueName, false, false, false, null);
        //新建一个消费者
        Consumer consumer = new DefaultConsumer(channel) {

            //重写该方法,用于接收消息
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                    BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("接收到的消息:" + msg);
            }
        };
        //消费者消费消息,消费queueName中的消息
        channel.basicConsume(queueName, consumer);
    }
}

四 测试

1 运行

2 控制台输出

接收到的消息:send a message,please receive!

五 交换器、绑定和队列

六 交换器类型

  • direct:根据生产者传过来的“routing key”是否等于“binding key”,来决定将消息发送给哪个队列。

  • topic:根据传过来的“routing key”是否匹配一定的表达式,来决定消息发送给哪个或者哪些队列。

  • fanout:将消息发送给交换器知道的全部队列,这种交换器会忽略设置的“routing key”。

  • headers:根据消息的头信息,来决定将消息发送给哪个队列。

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/81267735