« Azure Service Bus » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications |
|||
(45 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
[[Category:Azure]] | [[Category:Azure]] | ||
= Links = | |||
* [https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-how-to-use-topics-subscriptions Get started with Azure Service Bus topics and subscriptions] | |||
* [https://www.nuget.org/packages/Azure.Messaging.ServiceBus Azure.Messaging.ServiceBus nuget package with exemple] | |||
* [https://www.serverless360.com/azure-service-bus Azure Service Bus on ServerLess360] | |||
= [https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview Description] = | = [https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview Description] = | ||
It is a message broker with message queues and publish-subscribe topics. | It is a message broker with message queues and publish-subscribe topics. | ||
{| class="wikitable wtp" | |||
! Name | |||
! Description | |||
|- | |||
| Queue || Allows for Sending and Receiving of messages. Often used for point-to-point communication. | |||
|- | |||
| Topic || As opposed to Queues, Topics are better suited to publish/subscribe scenarios.<br>A topic can be sent to, but requires a subscription, of which there can be multiple in parallel, to consume from. | |||
|- | |||
| Subscription || The mechanism to consume from a Topic.<br>Each subscription is independent, and receives a copy of each message sent to the topic.<br>Rules and Filters can be used to tailor which messages are received by a specific subscription. | |||
|} | |||
[[File:AzureServiceBusQueue.png|600px]] | |||
[[File:AzureServiceBusTopic.png|600px]] | |||
= Configure the service bus on Azure = | = Configure the service bus on Azure = | ||
* [https://portal.azure.com Azure portal] → Service Bus → Add | |||
* Create namespace: the namespace will be the url use by the service bus {{boxx|[namespace].servicebus.windows.net}} | |||
* Pricing tiers: Basic, Standard (Topics), Premium (private network, dedicated capacity) | |||
* Once deployed, go to the newly create service bus namespace | |||
** add queue | |||
** add topic then add subscription | |||
= Sender = | |||
Add Nuget package {{boxx|Azure.Messaging.ServiceBus}} | |||
<filebox fn='Program.cs'> | |||
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String"; | |||
private const string QueueName = "queue1"; | |||
private const string TopicName = "topic1"; | |||
static async Task Main(string[] args) | |||
{ | |||
var client = new ServiceBusClient(ConnectionString); | |||
try // use await using in C# 8 / .Net Core | |||
{ | |||
var sender = client.CreateSender(QueueName); | |||
var sender = client.CreateSender(TopicName); | |||
var message = new ServiceBusMessage("Hello!"); | |||
await sender.SendMessageAsync(message); | |||
Console.WriteLine($"Sent {message.Body}"); | |||
} | |||
finally | |||
{ | |||
await client.DisposeAsync(); | |||
} | |||
} | |||
</filebox> | |||
= Receiver = | |||
Add Nuget package {{boxx|Azure.Messaging.ServiceBus}} | |||
{{warn | 1 receiver for 1 subscription.}} | |||
<filebox fn='Program.cs'> | |||
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String"; | |||
private const string QueueName = "queue1"; | |||
private const string SubscriptionName = "subscription1"; | |||
static async Task Main(string[] args) | |||
{ | |||
var client = new ServiceBusClient(ConnectionString); | |||
var processor = client.CreateProcessor(QueueName); | |||
var processor = client.CreateProcessor(TopicName, SubscriptionName); | |||
try | |||
{ | |||
processor.ProcessMessageAsync += MessageHandler; | |||
processor.ProcessErrorAsync += ErrorHandler; | |||
await processor.StartProcessingAsync(); | |||
Console.ReadKey(); | |||
} | |||
finally | |||
{ | |||
await processor.DisposeAsync(); | |||
await client.DisposeAsync(); | |||
} | |||
} | |||
static async Task MessageHandler(ProcessMessageEventArgs args) | |||
{ | |||
string body = args.Message.Body.ToString(); | |||
Console.WriteLine(body); | |||
// we can evaluate application logic and use that to determine how to settle the message. | |||
await args.CompleteMessageAsync(args.Message); | |||
} | |||
static Task ErrorHandler(ProcessErrorEventArgs args) | |||
{ | |||
// the error source tells me at what point in the processing an error occurred | |||
Console.WriteLine(args.ErrorSource); | |||
// the fully qualified namespace is available | |||
Console.WriteLine(args.FullyQualifiedNamespace); | |||
// as well as the entity path | |||
Console.WriteLine(args.EntityPath); | |||
Console.WriteLine(args.Exception.ToString()); | |||
return Task.CompletedTask; | |||
} | |||
</filebox> | |||
== [https://docs.microsoft.com/en-us/dotnet/api/azure.messaging.servicebus.servicebusprocessoroptions?view=azure-dotnet ServiceBusProcessorOptions] == | |||
<kode lang='cs'> | |||
var options = new ServiceBusProcessorOptions | |||
{ | |||
ReceiveMode = ServiceBusReceiveMode.PeekLock, | |||
AutoCompleteMessages = true, | |||
MaxAutoLockRenewalDuration = TimeSpan.FromMinutes(5), // renew the lock automatically till duration | |||
MaxConcurrentCalls = 1, // number of messages processed concurrently | |||
PrefetchCount = 0, // quietly acquire messages, up to the PrefetchCount limit | |||
SubQueue = SubQueue.None | |||
}; | |||
var processor = client.CreateProcessor(TopicName, SubscriptionName, options); | |||
</kode> | |||
{| class="wikitable wtp wtmono1" | |||
! Receive mode | |||
! Description | |||
|- | |||
| PeekLock || The client sends a request to receive a message from Service Bus.<br>Then it sends a request to complete / abandon / defer / dead-letter the message. | |||
|- | |||
| ReceiveAndDelete || The send of a request to receive a message and the send of the request to complete the message are combined in a single request.<br>This reduces the overall number of operations, and can improve the overall message throughput.<br>This performance gain comes at the risk of losing messages. | |||
|} | |||
{| class="wikitable wtp wtmono1" | |||
! Auto-complete messages | |||
! Description | |||
|- | |||
| true || Automatically complete messages after the message handler has completed processing without any exception raised.<br>No need to call <syntaxhighlight lang='cs' inline>await args.CompleteMessageAsync(message);</syntaxhighlight> | |||
|- | |||
| false || Don't complete the message processing automatically.<br>A request is required to manually complete / abandon / defer / dead-letter the message. | |||
|} | |||
{| class="wikitable wtp wtmono1" | |||
! SubQueue | |||
! Description | |||
|- | |||
| None || No subqueue | |||
|- | |||
| DeadLetter || Add a secondary subqueue to hold messages that can't be delivered to any receiver. | |||
|- | |||
| TransferDeadLetter || Add a secondary subqueue to hold messages that can't be delivered to any receiver when the following conditions apply: | |||
* a message passes through more than four queues or topics that are chained together | |||
* the destination queue or topic is disabled or deleted | |||
* the destination queue or topic exceeds the maximum entity size | |||
|} |
Dernière version du 10 janvier 2022 à 21:03
Links
- Get started with Azure Service Bus topics and subscriptions
- Azure.Messaging.ServiceBus nuget package with exemple
- Azure Service Bus on ServerLess360
Description
It is a message broker with message queues and publish-subscribe topics.
Name | Description |
---|---|
Queue | Allows for Sending and Receiving of messages. Often used for point-to-point communication. |
Topic | As opposed to Queues, Topics are better suited to publish/subscribe scenarios. A topic can be sent to, but requires a subscription, of which there can be multiple in parallel, to consume from. |
Subscription | The mechanism to consume from a Topic. Each subscription is independent, and receives a copy of each message sent to the topic. Rules and Filters can be used to tailor which messages are received by a specific subscription. |
Configure the service bus on Azure
- Azure portal → Service Bus → Add
- Create namespace: the namespace will be the url use by the service bus [namespace].servicebus.windows.net
- Pricing tiers: Basic, Standard (Topics), Premium (private network, dedicated capacity)
- Once deployed, go to the newly create service bus namespace
- add queue
- add topic then add subscription
Sender
Add Nuget package Azure.Messaging.ServiceBus
Program.cs |
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String"; private const string QueueName = "queue1"; private const string TopicName = "topic1"; static async Task Main(string[] args) { var client = new ServiceBusClient(ConnectionString); try // use await using in C# 8 / .Net Core { var sender = client.CreateSender(QueueName); var sender = client.CreateSender(TopicName); var message = new ServiceBusMessage("Hello!"); await sender.SendMessageAsync(message); Console.WriteLine($"Sent {message.Body}"); } finally { await client.DisposeAsync(); } } |
Receiver
Add Nuget package Azure.Messaging.ServiceBus
1 receiver for 1 subscription. |
Program.cs |
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String"; private const string QueueName = "queue1"; private const string SubscriptionName = "subscription1"; static async Task Main(string[] args) { var client = new ServiceBusClient(ConnectionString); var processor = client.CreateProcessor(QueueName); var processor = client.CreateProcessor(TopicName, SubscriptionName); try { processor.ProcessMessageAsync += MessageHandler; processor.ProcessErrorAsync += ErrorHandler; await processor.StartProcessingAsync(); Console.ReadKey(); } finally { await processor.DisposeAsync(); await client.DisposeAsync(); } } static async Task MessageHandler(ProcessMessageEventArgs args) { string body = args.Message.Body.ToString(); Console.WriteLine(body); // we can evaluate application logic and use that to determine how to settle the message. await args.CompleteMessageAsync(args.Message); } static Task ErrorHandler(ProcessErrorEventArgs args) { // the error source tells me at what point in the processing an error occurred Console.WriteLine(args.ErrorSource); // the fully qualified namespace is available Console.WriteLine(args.FullyQualifiedNamespace); // as well as the entity path Console.WriteLine(args.EntityPath); Console.WriteLine(args.Exception.ToString()); return Task.CompletedTask; } |
ServiceBusProcessorOptions
var options = new ServiceBusProcessorOptions { ReceiveMode = ServiceBusReceiveMode.PeekLock, AutoCompleteMessages = true, MaxAutoLockRenewalDuration = TimeSpan.FromMinutes(5), // renew the lock automatically till duration MaxConcurrentCalls = 1, // number of messages processed concurrently PrefetchCount = 0, // quietly acquire messages, up to the PrefetchCount limit SubQueue = SubQueue.None }; var processor = client.CreateProcessor(TopicName, SubscriptionName, options); |
Receive mode | Description |
---|---|
PeekLock | The client sends a request to receive a message from Service Bus. Then it sends a request to complete / abandon / defer / dead-letter the message. |
ReceiveAndDelete | The send of a request to receive a message and the send of the request to complete the message are combined in a single request. This reduces the overall number of operations, and can improve the overall message throughput. This performance gain comes at the risk of losing messages. |
Auto-complete messages | Description |
---|---|
true | Automatically complete messages after the message handler has completed processing without any exception raised. No need to call await args.CompleteMessageAsync(message);
|
false | Don't complete the message processing automatically. A request is required to manually complete / abandon / defer / dead-letter the message. |
SubQueue | Description |
---|---|
None | No subqueue |
DeadLetter | Add a secondary subqueue to hold messages that can't be delivered to any receiver. |
TransferDeadLetter | Add a secondary subqueue to hold messages that can't be delivered to any receiver when the following conditions apply:
|