wuyouming666
2024-05-11 0417fafe757f09bfb166b1dd1d9f096e966a30d8
springboot-vue3/src/main/java/com/example/springboot/component/MessageQueueReader.java
@@ -13,28 +13,19 @@
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        List<String> messages = new ArrayList<>();
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            boolean autoAck = false;
           // autoAck 参数设置为 false,然后手动确认消息处理完成
            // 循环获取队列中的所有消息
//            while (true) {
                GetResponse response = channel.basicGet(QUEUE_NAME, autoAck);
                GetResponse response = channel.basicGet(QUEUE_NAME, autoAck);
                if (response != null) {
                    String message = new String(response.getBody(), "UTF-8");
                    messages.add(message);
                    // 手动确认消息处理完成
                    long deliveryTag = response.getEnvelope().getDeliveryTag();
                    channel.basicAck(deliveryTag, false);
//                } else {
//                    // 如果队列为空,则退出循环
//                    break;
//                }
            }
        }