使用最新的服务总线客户端库,您可以像这样访问死信队列。请注意在创建接收器时指定死信队列时语言之间的一致性:
const { ServiceBusClient } = require("@azure/service-bus");
const client = new ServiceBusClient("<connectionstring>");
const deadletterReceiver = client.createReceiver("queueName", { subQueueType: "deadLetter" });
const messages = await deadletterReceiver.receiveMessages(1);
if (messages.length > 0) {
console.log("Received the message from DLQ - ", messages[0].body);
// Mark message as complete, i.e. remove from DLQ
await deadletterReceiver.completeMessage(messages[0]);
} else {
console.log("Error: No messages were received from the DLQ.");
}
await deadletterReceiver.close();
import com.azure.messaging.servicebus.*;
import com.azure.messaging.servicebus.models.SubQueue;
ServiceBusReceiverClient deadletterReceiver = new ServiceBusClientBuilder()
.connectionString("<connectionstring>")
.receiver() // Use this for session or non-session enabled queue or topic/subscriptions
.queueName("queuename")
.subQueue(SubQueue.DEAD_LETTER_QUEUE)
.buildClient();
IterableStream<ServiceBusReceivedMessage> messages = receiver.receiveMessages(10);
messages.forEach(message -> {
System.out.printf("Id: %s. Contents: %s%n", message.getMessageId(),
message.getBody().toString());
//Remove message from DLQ
deadletterReceiver.complete(message);
});
deadletterReceiver.close();
import asyncio
from azure.servicebus import ServiceBusMessage, ServiceBusSubQueue
from azure.servicebus.aio import ServiceBusClient
servicebus_client = ServiceBusClient.from_connection_string(conn_str="<connectionstring>")
dlq_receiver = servicebus_client.get_queue_receiver(queue_name="queuename",
sub_queue=ServiceBusSubQueue.DEAD_LETTER)
async with dlq_receiver:
received_msgs = await dlq_receiver.receive_messages(max_message_count=10, max_wait_time=5)
for msg in received_msgs:
print(msg)
# remove the message from the DLQ
await dlq_receiver.complete_message(msg)
using Azure.Messaging.ServiceBus;
await using var client = new ServiceBusClient("<connectionstring>");
ServiceBusReceiver deadletterReceiver = client.CreateReceiver(
"queuename",
new ServiceBusReceiverOptions { SubQueue = SubQueue.DeadLetter }
);
ServiceBusReceivedMessage message = await deadletterReceiver.ReceiveMessageAsync();
if (message != null)
{
Console.WriteLine($"DeadLetter message = {message.Body}");
// remove the message from the DLQ
await deadletterReceiver.CompleteMessageAsync(message);
}
else
{
// DLQ was empty on last receive attempt
Console.WriteLine("Error: No messages were received from the DLQ.");
}
await deadletterReceiver.CloseAsync();