【发布时间】:2021-04-11 20:11:38
【问题描述】:
我需要将消息从 Azure 主题订阅发送到死信队列,以防在读取和处理来自主题的消息时出现任何错误。所以我尝试测试直接向DLQ推送消息。
我的示例代码会是这样的
static void sendMessage()
{
// create a Service Bus Sender client for the queue
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
.connectionString(connectionString)
.sender()
.topicName(topicName)
.buildClient();
// send one message to the topic
senderClient.sendMessage(new ServiceBusMessage("Hello, World!"));
}
static void resceiveAsync() {
ServiceBusReceiverAsyncClient receiver = new ServiceBusClientBuilder()
.connectionString(connectionString)
.receiver()
.topicName(topicName)
.subscriptionName(subName)
.buildAsyncClient();
// receive() operation continuously fetches messages until the subscription is disposed.
// The stream is infinite, and completes when the subscription or receiver is closed.
Disposable subscription = receiver.receiveMessages().subscribe(message -> {
System.out.printf("Id: %s%n", message.getMessageId());
System.out.printf("Contents: %s%n", message.getBody().toString());
}, error -> {
System.err.println("Error occurred while receiving messages: " + error);
}, () -> {
System.out.println("Finished receiving messages.");
});
// Continue application processing. When you are finished receiving messages, dispose of the subscription.
subscription.dispose();
// When you are done using the receiver, dispose of it.
receiver.close();
}
我尝试获取死信队列路径
String dlq = EntityNameHelper.formatDeadLetterPath(topicName);
我得到了死信队列的路径,例如 = "mytopic/$deadletterqueue"
但是在将路径作为主题名称传递时它不起作用。它抛出一个Entity topic not found异常。
谁能给我建议
参考: How to move error message to Azure dead letter queue using Java?
How to push the failure messages to Azure service bus Dead Letter Queue in Spring Boot Java?
【问题讨论】:
标签: java azure servicebus azure-servicebus-topics dead-letter