本文介绍了NodeJS @ azure/service-bus:有任何手动将消息发送到死信的示例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找不到任何示例来手动将消息发送到死信队列.
I could not find any examples to send message to deadletter queue manually.
let { ServiceBusClient, ReceiveMode } = require('@azure/service-bus'),
serviceBusClient = ServiceBusClient.createFromConnectionString(SERVICE_BUS_SECRET),
queueClient = serviceBusClient.createQueueClient(QUEUE),
receiver = queueClient.createReceiver(ReceiveMode.peekLock);
const onMessageHandler = async (brokeredMessage) => {
await brokeredMessage.deadletter({
deadletterReason: 'testingReason',
deadLetterErrorDescription: 'testingDesc'
});
};
const onErrorHandler = (error) => {
console.log(error);
};
receiver.registerMessageHandler(onMessageHandler, onErrorHandler);
当我运行上述代码时,会出现类似 deadletter()的错误.
When I run above code, there is error like deadletter() is not defined.
推荐答案
关于此问题,请参考以下代码
Regarding the issue, please refer to the following code
async function main() {
const sbClient = ServiceBusClient.createFromConnectionString(
connectionString
);
const queueClient = sbClient.createQueueClient(queueName);
try {
const receiver = queueClient.createReceiver(ReceiveMode.peekLock);
const receiverPromise = new Promise((resolve, _reject) => {
const onMessageHandler = async (brokeredMessage) => {
console.log("Received message: ", brokeredMessage.messageId);
await brokeredMessage.deadLetter({
deadletterReason: "test",
deadLetterErrorDescription: "test",
});
console.log(
`send message ${brokeredMessage.messageId} to dead letter queue successfully `
);
};
const onErrorHandler = (err) => {
if (err.retryable === true) {
console.log(
"Receiver will be recreated. A recoverable error occurred:",
err
);
resolve();
} else {
console.log("Error occurred: ", err);
}
};
receiver.registerMessageHandler(onMessageHandler, onErrorHandler, {
autoComplete: false,
});
});
await receiverPromise;
await receiver.close();
} finally {
await queueClient.close();
await sbClient.close();
}
}
main().catch((err) => {
console.log("Error occurred: ", err);
});
这篇关于NodeJS @ azure/service-bus:有任何手动将消息发送到死信的示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!