From d0ef5e2950216abe73168ee68c957468bb4fbc4e Mon Sep 17 00:00:00 2001 From: Wonder Date: Thu, 13 Nov 2025 23:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=84Update:=20=E5=8F=91=E9=80=81?= =?UTF-8?q?=E4=B8=80=E6=9D=A1=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cn/hezhaohui/mq/Send.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/cn/hezhaohui/mq/Send.java diff --git a/src/main/java/cn/hezhaohui/mq/Send.java b/src/main/java/cn/hezhaohui/mq/Send.java new file mode 100644 index 0000000..0ae19e8 --- /dev/null +++ b/src/main/java/cn/hezhaohui/mq/Send.java @@ -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); + } + } +}