subscriptionClient.AbandonAsync 与 subscriptionClient.closeAsync 有什么区别。
我需要检查订阅客户端中是否存在主题。由于某些限制,我无法使用 azure 管理客户端
subscriptionClient.AbandonAsync 与 subscriptionClient.closeAsync 有什么区别。
我需要检查订阅客户端中是否存在主题。由于某些限制,我无法使用 azure 管理客户端
1. subscriptionClient.AbandonAsync 与 subscriptionClient.closeAsync 有什么区别。
根据我的研究和测试,该方法subscriptionClient.AbandonAsync
用于告诉服务总线该消息已被丢弃,需要重新处理该消息。一旦消息的 DeliveryCount 达到 MaxDeliveryCount,消息就会移动到 DLQ,并指定MaxDeliveryCountExceeded
原因代码。更多详细信息,请参阅文档
该方法subscriptionClient.closeAsync
用于关闭订阅客户端,然后您的客户端无法接收来自订阅的消息。
我的测试:
代码:
namespace CoreReceiverApp
{
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
class Program
{
const string ServiceBusConnectionString = "<your_connection_string>";
const string TopicName = "<your_topic_name>";
const string SubscriptionName = "<your_subscription_name>";
static ISubscriptionClient subscriptionClient;
public static async Task Main(string[] args)
{
subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName);
Console.WriteLine("======================================================");
Console.WriteLine("Press ENTER key to exit after receiving all the messages.");
Console.WriteLine("======================================================");
// Register subscription message handler and receive messages in a loop
RegisterOnMessageHandlerAndReceiveMessages();
Console.ReadKey();
await subscriptionClient.CloseAsync();
}
static void RegisterOnMessageHandlerAndReceiveMessages()
{
// Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,
// Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
// False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
AutoComplete = false
};
// Register the function that processes messages.
subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message.
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
await subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
Console.WriteLine(message.SystemProperties.DeliveryCount);
}
static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
{
Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
Console.WriteLine("Exception context for troubleshooting:");
Console.WriteLine($"- Endpoint: {context.Endpoint}");
Console.WriteLine($"- Entity Path: {context.EntityPath}");
Console.WriteLine($"- Executing Action: {context.Action}");
return Task.CompletedTask;
}
}
}
有几个选项可以验证主题是否存在。我可以想到两种最常见的方法:
ManagementClient
.两者之间的区别在于您使用什么来验证您的代码。使用 SDK 库,您可以使用连接字符串或 TokenProvider(托管身份或其他方式进行身份验证)。使用管理库,您只能使用 Azure Active Directory 进行身份验证(需要租户 ID、客户端 ID、客户端密码、订阅 ID 和资源组名称)。
关于什么似乎是一个完全独立的问题
subscriptionClient.AbandonAsync 与 subscriptionClient.closeAsync 有什么区别。
subscriptionClient.AbandonAsync()
放弃该消息以允许另一个竞争消费者接收它。Where subscriptionClient.closeAsync()
closesSubscriptionClient
与代理的连接。