2 Commits

Author SHA1 Message Date
wonder 0628c6892a 🔄Update: 接收一条信息 2025-11-13 23:42:13 +08:00
wonder d0ef5e2950 🔄Update: 发送一条消息 2025-11-13 23:31:13 +08:00
3 changed files with 32 additions and 46 deletions
@@ -1,37 +0,0 @@
package cn.hezhaohui.mq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;
public class Publish {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Scanner scanner = new Scanner(System.in)) {
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String queue = channel.queueDeclare().getQueue();
channel.queueBind(queue, EXCHANGE_NAME, "");
while (true) {
String msg = scanner.nextLine();
if (msg.equals("exit")) {
break;
}
channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
System.out.println("[Sent] " + msg);
}
}
}
}
@@ -9,22 +9,20 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
public class Consume { public class Recv {
private static final String EXCHANGE_NAME = "logs"; private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws IOException, TimeoutException { public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory(); ConnectionFactory connectionFactory = new ConnectionFactory();
Connection connection = factory.newConnection(); Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel(); Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); System.out.println(" [*] Waiting for message");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
DeliverCallback deliverCallback = (consumerTag, delivery) -> { DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8); String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println("[Receive] " + message); System.out.println(" [x] Received '" + message + "'");
}; };
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {}); channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {});
} }
} }
+25
View File
@@ -0,0 +1,25 @@
package cn.hezhaohui.mq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) {
ConnectionFactory connectionFactory = new ConnectionFactory();
try (Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel()){
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
} catch (IOException | TimeoutException e) {
throw new RuntimeException(e);
}
}
}