引入RabbitMQ的jar包

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.1.</version>
</dependency>

创建消息生产者

import com.rabbitmq.client.Channel;
 import com.rabbitmq.client.Connection;
 import com.rabbitmq.client.ConnectionFactory;
 
 import java.io.IOException;
 import java.util.concurrent.TimeoutException;
 
 public class Producer {
     private static final String QUEUE_NAME ="queue.test";
 
     public static void main(String[] args) throws IOException, TimeoutException {
         //创建连接工厂
         ConnectionFactory connectionFactory = new ConnectionFactory();
         connectionFactory.setHost("localhost");
         //创建一个连接
         Connection connection = connectionFactory.newConnection();
         //创建一个通道
         Channel channel = connection.createChannel();
         //声明队列
         //queueDeclare第一个参数表示队列名称、
         // 第二个参数为是否持久化(true表示是,队列将在服务器重启时生存)、
         // 第三个参数为是否是独占队列(创建者可以使用的私有队列,断开后自动删除)、
         // 第四个参数为当所有消费者客户端连接断开时是否自动删除队列、第五个参数为队列的其他参数
         channel.queueDeclare(QUEUE_NAME,false,false,false,null);
         String msg = "hello rabbit";
         //发送消息到队列
         //basicPublish第一个参数为交换机名称、
         // 第二个参数为队列映射的路由key、
         // 第三个参数为消息的其他属性、
         // 第四个参数为发送信息的主体
         channel.basicPublish("",QUEUE_NAME,null,msg.getBytes("UTF-8"));
         System.out.println("Producer Send +'" + msg + "'");
         channel.close();
         connection.close();
     }
 }

创建消费者

package com.ysl.rabbit;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Customer {

    private static final String QUEUE_NAME ="queue.test";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        //创建一个连接
        Connection connection = connectionFactory.newConnection();
        //创建一个通道
        Channel channel = connection.createChannel();
        //声明队列
        //queueDeclare第一个参数表示队列名称、
        // 第二个参数为是否持久化(true表示是,队列将在服务器重启时生存)、
        // 第三个参数为是否是独占队列(创建者可以使用的私有队列,断开后自动删除)、
        // 第四个参数为当所有消费者客户端连接断开时是否自动删除队列、第五个参数为队列的其他参数
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        System.out.println("Customer Waiting Received messages");
        Consumer consumer = new DefaultConsumer(channel){
            /**
             * envelope主要存放生产者相关信息(比如交换机、路由key等)body是消息实体。
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("Customer Received '" + message + "'");
            }
        };
        //自动回复队列应答 -- RabbitMQ中的消息确认机制
        channel.basicConsume(QUEUE_NAME,true, consumer);
    }
}