我需要将我的消息从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”
但它在传递路径作为主题名时不起作用。它引发实体主题找不到异常。
有谁能告诉我这件事吗
参考:如何使用java将错误消息移动到azure死信队列?
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dead-letter-queues#moving-发送到dlq的消息
如何在springbootjava中将失败消息推送到azure服务总线死信队列?
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-java-how-to-use-topics-subscriptions-legacy#receive-来自订阅的邮件
1条答案
按热度按时间64jmpszr1#
您可能知道,如果在处理过程中抛出异常,并且超出了最大Delivery计数,则消息将自动移动到死信队列。如果要显式地将消息移动到dlq,也可以这样做。一个常见的情况是,如果您知道消息永远不会成功,因为它的内容。
您不能直接向dlq发送新消息,因为这样系统中会有两条消息。您需要对父实体调用特殊操作。也,
<topic path>/$deadletterqueue
不工作,因为这将是所有订阅的dlq。正确的实体路径如下所示:https://github.com/azure/azure-service-bus/blob/master/samples/java/azure-servicebus/deadletterqueue/src/main/java/com/microsoft/azure/servicebus/samples/deadletterqueue/deadletterqueue.java
此示例代码适用于队列,但您应该能够很容易地将其改编为主题: