amqplib-init 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/module/MessageProcessor.js +45 -26
- package/package.json +1 -1
|
@@ -57,40 +57,24 @@ class MessageProcessor {
|
|
|
57
57
|
|
|
58
58
|
this.processingMessages.add(deliveryTag);
|
|
59
59
|
|
|
60
|
+
// 检查消息内容有效性,对无效消息直接确认
|
|
61
|
+
if (!this._isValidMessageContent(msg)) {
|
|
62
|
+
log.log(`⚠️ 收到无效消息 ${deliveryTag},内容为空或无法解析,自动确认以防止阻塞`);
|
|
63
|
+
this._acknowledgeMessage(msg, channel, deliveryTag, 0); // 立即确认无效消息
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
60
67
|
try {
|
|
61
68
|
const content = JSON.parse(msg.content.toString());
|
|
62
69
|
log.log(`🪴 队列收到消息: ${JSON.stringify(content)}`);
|
|
63
70
|
const startTime = Date.now();
|
|
64
71
|
|
|
65
|
-
// 🔥 兼容性检测:检查callback函数的参数数量
|
|
66
|
-
const callbackParams = callback.length;
|
|
67
|
-
let callbackPromise;
|
|
68
|
-
|
|
69
|
-
if (callbackParams >= 3) {
|
|
70
|
-
// 新版API:支持手动ACK (content, channel, msg)
|
|
71
|
-
log.log(`📋 使用新版手动ACK API (${callbackParams}个参数)`);
|
|
72
|
-
callbackPromise = callback(content, channel, msg);
|
|
73
|
-
} else {
|
|
74
|
-
// 旧版API:自动ACK (content)
|
|
75
|
-
log.log(`📋 使用旧版自动ACK API (${callbackParams}个参数)`);
|
|
76
|
-
callbackPromise = callback(content);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
72
|
// 执行消息处理回调
|
|
80
|
-
|
|
81
|
-
.then((
|
|
73
|
+
callback(content)
|
|
74
|
+
.then(() => {
|
|
82
75
|
const endTime = Date.now() - startTime;
|
|
83
76
|
log.log(`☘️ 消息处理完成,延迟: ${delay}ms,总时间: ${endTime}ms`);
|
|
84
|
-
|
|
85
|
-
// 🔥 检查返回值决定是否自动ACK
|
|
86
|
-
if (result === 'MANUAL_ACK') {
|
|
87
|
-
// 手动ACK模式:从处理集合中移除但不ACK,等待用户手动调用
|
|
88
|
-
this.processingMessages.delete(deliveryTag);
|
|
89
|
-
log.log(`📋 消息 ${deliveryTag} 进入手动ACK模式,等待手动确认`);
|
|
90
|
-
} else {
|
|
91
|
-
// 自动ACK模式:按原有逻辑处理
|
|
92
|
-
this._acknowledgeMessage(msg, channel, deliveryTag, delay);
|
|
93
|
-
}
|
|
77
|
+
this._acknowledgeMessage(msg, channel, deliveryTag, delay);
|
|
94
78
|
})
|
|
95
79
|
.catch(async (e) => {
|
|
96
80
|
log.log('‼️ 处理消息返回错误:', e);
|
|
@@ -102,6 +86,41 @@ class MessageProcessor {
|
|
|
102
86
|
}
|
|
103
87
|
}
|
|
104
88
|
|
|
89
|
+
/**
|
|
90
|
+
* 检查消息内容是否有效
|
|
91
|
+
* @param {Object} msg - RabbitMQ消息对象
|
|
92
|
+
* @returns {boolean} 消息内容是否有效
|
|
93
|
+
* @private
|
|
94
|
+
*/
|
|
95
|
+
_isValidMessageContent(msg) {
|
|
96
|
+
try {
|
|
97
|
+
// 检查消息对象是否存在
|
|
98
|
+
if (!msg || !msg.content) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 获取消息内容字符串
|
|
103
|
+
const contentStr = msg.content.toString();
|
|
104
|
+
|
|
105
|
+
// 检查内容是否为空或只包含空白字符
|
|
106
|
+
if (!contentStr || contentStr.trim() === '') {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 检查是否为null字符串
|
|
111
|
+
if (contentStr.toLowerCase() === 'null' || contentStr.toLowerCase() === 'undefined') {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 尝试解析JSON,如果失败则认为无效
|
|
116
|
+
JSON.parse(contentStr);
|
|
117
|
+
return true;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
// JSON解析失败,认为消息无效
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
105
124
|
/**
|
|
106
125
|
* 确认消息
|
|
107
126
|
* @param {Object} msg - RabbitMQ消息对象
|