RabbitMQ快速入门

RabbitMQ快速⼊⻔

引入依赖

复制代码
				<dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.22.0</version>
        </dependency>

编写生产者代码

具体步骤:

  1. 建立连接

  2. 开启信道

  3. 声明交换机

  4. 声明队列

  5. 发送消息

  6. 释放资源

    public static void main(String[] args) throws IOException, TimeoutException {
    //1.建立连接
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(ip地址,填自己的);
    connectionFactory.setPort(5672); //需提前开放端口号
    connectionFactory.setUsername("admin");//账号
    connectionFactory.setPassword("admin");//密码
    connectionFactory.setVirtualHost("study");//虚拟主机
    Connection connection = connectionFactory.newConnection();
    //2.开启信道
    Channel channel = connection.createChannel();
    //3.声明交换机 使用内置的交换机
    //4.声明队列
    /**
    * Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
    * Map<String, Object> arguments) throws IOException;
    *
    * 参数说明:
    * queue:队列名称
    * durable:可持久化
    * exclusive:是否独占
    * autoDelete:是否自动删除
    * arguments:额外参数
    /
    channel.queueDeclare("hello",true,false,false,null);
    //5.发消息
    /
    *
    * void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
    *
    * 参数声明:
    * exchange:交换机名称,不写代表使用内置交换机
    * routingKey:路由名称, routingKey = 队列名称 (使用内置交换机,routingKey与队列名称保持一致)
    * props:属性配置
    * body:消息
    */
    for (int i = 0; i < 10; i++) {
    String msg = "hello rabbitmq ~" + i;
    channel.basicPublish("","hello",null,msg.getBytes());
    }
    System.out.println("消息发送成功~");

    复制代码
         //6.资源释放
         channel.close();
         connection.close();
     }

编写消费者代码

具体步骤:

  1. 建立连接

  2. 创建信道

  3. 声明队列

  4. 消费消息

  5. 释放资源

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
    //1.建立连接
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(ip地址);
    connectionFactory.setPort(5672);
    connectionFactory.setUsername("admin");
    connectionFactory.setPassword("admin");
    connectionFactory.setVirtualHost("study");

    复制代码
         Connection connection = connectionFactory.newConnection();
         //2.创建Channel
         Channel channel = connection.createChannel();
         //3.声明队列(可以省略)
         channel.queueDeclare("hello",true,false,false,null);
         //4.消费消息
         DefaultConsumer consumer = new DefaultConsumer(channel){
             //从队列中收到消息,就会执行的方法
             @Override
             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                 System.out.println("接收到消息: " + new String(body));
             }
         };
         /**
          * String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
          * 参数说明:
          *  queue:队列名称
          *  autoAck:是否自动确认
          *  callback:接收到消息后,执行的逻辑
          */
         channel.basicConsume("hello",true,consumer);
         //等待程序完成
         Thread.sleep(5000);
    
         //5.释放资源
         channel.close();
         connection.close();
    
     }
相关推荐
J_liaty4 小时前
RabbitMQ面试题终极指南
开发语言·后端·面试·rabbitmq
maozexijr7 小时前
RabbitMQ Exchange Headers类型存在的意义?
分布式·rabbitmq
独自破碎E7 小时前
RabbitMQ的消息确认机制是怎么工作的?
分布式·rabbitmq
maozexijr10 小时前
注解实现rabbitmq消费者和生产者
分布式·rabbitmq
Java 码农1 天前
RabbitMQ集群部署方案及配置指南09
分布式·rabbitmq
论迹1 天前
RabbitMQ
分布式·rabbitmq
Java 码农1 天前
RabbitMQ集群部署方案及配置指南08--电商业务延迟队列定制化方案
大数据·分布式·rabbitmq
Java 码农1 天前
Spring Boot集成RabbitMQ的各种队列使用案例
spring boot·rabbitmq·java-rabbitmq
vb2008111 天前
Ubuntu 系统下 RabbitMQ 作为 MQTT 代理的配置方案
mqtt·rabbitmq
win x1 天前
RabbitMQ快速上手
分布式·rabbitmq