ZengTao
2024-04-23 9057d8571f11a552a326fa35fda9b5ec614f5c58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.springboot.component;
 
import com.rabbitmq.client.*;
 
import java.util.HashMap;
import java.util.Map;
 
public class ModuleB {
    private final static String QUEUE_NAME = "hangzhou2";
 
    public static void main(String[] argv) throws Exception {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("10.153.19.150");
     //   factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 声明队列
            Map<String, Object> args = new HashMap<>();
            args.put("x-max-length",10000);
            channel.queueDeclare(QUEUE_NAME, true, false, false, args);
 
            // 创建消费者
            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 -> {
           });
        }
    }
}