package com.example.springboot.component; import com.rabbitmq.client.*; public class ModuleB { private final static String QUEUE_NAME = "hangzhoumes"; public static void main(String[] argv) throws Exception { // 创建连接工厂 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 创建消费者 DeliverCallback deliverCallback = (consumerTag, delivery) -> { String receivedMessage = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + receivedMessage + "'"); }; // 开始消费消息 channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { }); } } }