sha*_*com 9 rabbitmq node.js node-amqp
如何在几次可配置的重新排队尝试后实现拒绝消息的机制?
换句话说,如果我正在订阅队列,我想保证相同的消息不再重新传递超过X次.
我的代码示例:
q.subscribe({ack: true}, function(data,headers,deliveryInfo,message) {
try{
doSomething(data);
} catch(e) {
message.reject(true);
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,最好的解决方案是在应用程序中处理这些错误,并在应用程序决定无法处理消息时拒绝它们。
如果您不想丢失信息,则应用程序应仅在将同一消息发送到错误队列后才拒绝该消息。
代码未测试:
q.subscribe({ack: true}, function () {
var numOfRetries = 0;
var args = arguments;
var self = this;
var promise = doWork.apply(self, args);
for (var numOfRetries = 0; numOfRetries < MAX_RETRIES; numOfRetries++) {
promise = promise.fail(function () { return doWork.apply(self, args); });
}
promise.fail(function () {
sendMessageToErrorQueue.apply(self, args);
rejectMessage.apply(self, args);
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1302 次 |
| 最近记录: |