〇、相关资料

1、java操作RabbitMQ


2、spring集成RabbitMQ


3、python操作RabbitMQ


一、环境准备

1.1 项目搭建

RabbitMQ-java api实现生产、消费和数据预览_RabbitMQ

1.2 引入依赖

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

二、生产消息

2.1 前置知识


2.2 代码实现

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

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class Product {
    private static String queue = "xx-rabbit-demo-message-queue-01";

    public static void main(String[] args) {

        pushMqMessage();

    }

    public static void pushMqMessage() {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //配置rabbitMQ的连接信息
        factory.setHost("1xxxxxx8");
        factory.setPort(5672);
        factory.setUsername("xx");
        factory.setPassword("xx");
        //定义连接
        Connection connection = null;
        //定义通道
        Channel channel = null;

        try {
            connection = factory.newConnection();//获取连接
            channel = connection.createChannel();//获取通道
            /**
             * 声明一个队列
             * 参数1:为队列名取任意值
             * 参数2:是否为持久化队列
             * 参数3:是否排外,如果排外则这个队列只允许一个消费者监听
             * 参数4:是否自动删除,如果为true则表示当前队列中没有消息,也没有消费者连接时就会自动删除这个队列。
             * 参数5:为队列的一些属性设置通常为null即可
             */
            channel.queueDeclare(queue,true,false,false,null);
            //定义消息
            for (int i = 0; i < 2; i++) {
                String message = getCusQueueMessage();
                channel.basicPublish("", queue, null, message.getBytes("utf-8"));
                /**
                 * 发送消息
                 * 参数1:为交换机名称,这里为空字符串表示不使用交换机
                 * 参数2:为队列名或RoutingKey,当指定了交换机名称以后这个值就是RoutingKey
                 * 参数3:为消息的属性信息,通常为空即可
                 * 参数4:为具体的消息数据的字节数组
                 */
                channel.basicPublish("", queue, null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println("消息发送成功!");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}


2.3 代码讲解


三、消费消息

3.1 前置知识


3.2 代码实现

package com.xxx.rabbit;

import com.rabbitmq.client.*;

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

public class Consumer {
    // 队列名称
    public static final String QUEUE_NAME = "xx-rabbit-demo-message-queue-01";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("1xx");
        factory.setPort(5672);
        factory.setUsername("xxxx");
        factory.setPassword("xxx");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 声明接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println(new String(message.getBody()));
        };

        // 声明取消消息
        CancelCallback callback = consumerTag -> {
            System.out.println("消息被中断");
        };

        /*
         * 消费者消费消息
         * 1. 消费哪个队列
         * 2. 消费者成功之后是否要应答 true代表自动应答,false代表手动应答
         * 3. 消费者未成功消费的回调
         * 4. 消费者取消消费的回调
         */
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, callback);
    }
}

3.3 代码讲解


四、预览消息

4.1 前置知识


4.2 代码实现

package com.xxx.rabbit;

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

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @Author xxx
 * @Since 2024/5/7 22:53
 * @Description Preview
 */
public class Preview {
    private static String queue = "xxx-rabbit-demo-message-queue-01";
    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //配置rabbitMQ的连接信息
        factory.setHost("1xx");
        factory.setPort(5672);
        factory.setUsername("xx");
        factory.setPassword("xx");
        //定义连接
        Connection connection = null;
        //定义通道
        Channel channel = null;

        try {
            connection = factory.newConnection();//获取连接
            channel = connection.createChannel();//获取通道
            // 获取队列中的一条消息,但不确认消息
            GetResponse response = channel.basicGet(queue, false);
            if (response != null) {
                byte[] body = response.getBody();
                System.out.println("Previewing message: " + new String(body, StandardCharsets.UTF_8));
                // 不确认消息,让消息保留在队列中
                channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true);
            } else {
                System.out.println("No messages in the queue.");
            }

            // 关闭连接
            channel.close();
            connection.close();
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }
}

4.3 代码讲解