| | |
| | | package com.mes.service;
|
| | |
|
| | | import com.rabbitmq.client.Channel;
|
| | | import com.rabbitmq.client.Connection;
|
| | | import com.rabbitmq.client.ConnectionFactory;
|
| | | import com.rabbitmq.client.DeliverCallback;
|
| | |
|
| | | import java.util.HashMap;
|
| | | import java.util.Map;
|
| | | import com.mes.common.RabbitMQUtils;
|
| | |
|
| | | 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-bytes",20000);
|
| | | // args.put("x-max-length",5000);
|
| | | channel.queueDeclare(QUEUE_NAME, false, 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 -> {
|
| | | });
|
| | |
|
| | |
|
| | | RabbitMQUtils receiver = new RabbitMQUtils();
|
| | |
|
| | | try {
|
| | |
|
| | | receiver.consumeMessageById("2",QUEUE_NAME);
|
| | | // String receivedMessage = receiver.consumeSelectedMessage(1,QUEUE_NAME);
|
| | | // String receivedMessage = String.valueOf(receiver.readMessages(QUEUE_NAME,false));
|
| | | // System.out.println("Received message: " + receivedMessage);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | // // 创建连接工厂
|
| | | // 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-bytes",1024 * 1024);
|
| | | //// args.put("x-max-length",5000);
|
| | | // channel.queueDeclare(QUEUE_NAME, false, 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 -> {
|
| | | // });
|
| | | // }
|
| | | }
|
| | | }
|
| | |
|