From 8d6b17bfa56fd767c6f9de666eb0dd44c6b032cd Mon Sep 17 00:00:00 2001
From: 廖井涛 <2265517004@qq.com>
Date: 星期四, 22 五月 2025 15:42:38 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/ERP_override
---
north-glass-erp/src/main/java/com/example/erp/common/RabbitMQUtil.java | 68 ++++++++++++++++++++++++++++++++++
1 files changed, 68 insertions(+), 0 deletions(-)
diff --git a/north-glass-erp/src/main/java/com/example/erp/common/RabbitMQUtil.java b/north-glass-erp/src/main/java/com/example/erp/common/RabbitMQUtil.java
new file mode 100644
index 0000000..31d8016
--- /dev/null
+++ b/north-glass-erp/src/main/java/com/example/erp/common/RabbitMQUtil.java
@@ -0,0 +1,68 @@
+package com.example.erp.common;
+
+import com.rabbitmq.client.*;
+
+import java.io.IOException;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeoutException;
+
+public class RabbitMQUtil {
+
+ private static final String SEND_QUEUE_NAME = "temperingUsed";
+ private static final String RECEIVE_QUEUE_NAME = "temperingReturn";
+ private static final String HOST = "localhost";
+ private static final String USERNAME = "guest";
+ private static final String PASSWORD = "guest";
+
+ private ConnectionFactory factory;
+ private Connection connection;
+ private Channel sendChannel;
+ private Channel receiveChannel;
+ private BlockingQueue<String> messageQueue;
+
+ public RabbitMQUtil() throws IOException, TimeoutException {
+ factory = new ConnectionFactory();
+ factory.setHost(HOST);
+ factory.setUsername(USERNAME);
+ factory.setPassword(PASSWORD);
+ connection = factory.newConnection();
+
+ sendChannel = connection.createChannel();
+ sendChannel.queueDeclare(SEND_QUEUE_NAME, false, false, false, null);
+
+ receiveChannel = connection.createChannel();
+ receiveChannel.queueDeclare(RECEIVE_QUEUE_NAME, false, false, false, null);
+
+ messageQueue = new ArrayBlockingQueue<>(100); // 璁剧疆闃熷垪澶у皬
+ startConsuming();
+ }
+
+ public void sendMessage(String message) throws IOException {
+ sendChannel.basicPublish("", SEND_QUEUE_NAME, null, message.getBytes());
+ }
+
+ public String receiveMessages() throws InterruptedException {
+ return messageQueue.take(); // 闃诲鐩村埌鏈夋秷鎭彲鐢�
+ }
+
+ private void startConsuming() throws IOException {
+ DeliverCallback deliverCallback = (consumerTag, delivery) -> {
+ String message = new String(delivery.getBody(), "UTF-8");
+ messageQueue.offer(message); // 灏嗘秷鎭斁鍏ラ槦鍒�
+ };
+ receiveChannel.basicConsume(RECEIVE_QUEUE_NAME, true, deliverCallback, consumerTag -> { });
+ }
+
+ public void close() throws IOException, TimeoutException {
+ if (sendChannel != null) {
+ sendChannel.close();
+ }
+ if (receiveChannel != null) {
+ receiveChannel.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ }
+}
--
Gitblit v1.8.0