-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Sb cross receiver samples #39514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JoshLove-msft
merged 7 commits into
Azure:main
from
JoshLove-msft:sb-cross-receiver-samples
Oct 26, 2023
Merged
Sb cross receiver samples #39514
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
29bc60c
Add samples demonstrating cross receiver settlement
JoshLove-msft b5362d5
Fix
JoshLove-msft 2112721
Fix
JoshLove-msft 0e7db27
fix line break
JoshLove-msft 35c9dd2
Fix sln
JoshLove-msft 72bb7ad
regenerate
JoshLove-msft dea608b
Apply suggestions from code review
JoshLove-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...s/Azure.Messaging.ServiceBus/samples/Sample16_CrossReceiverMessageSettlement.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Cross Receiver Message Settlement | ||
|
|
||
| The message settlement APIs on the `ServiceBusReceiver` require passing in the | ||
| `ServiceBusReceivedMessage`. This can be limiting for scenarios in which the message needs to be persisted and then | ||
| settled across process boundaries. There are two strategies that can be used for settling a message with a different | ||
| receiver. The recommended strategy depends on the specific application scenario. | ||
|
|
||
| ## Storing the entire `ServiceBusReceivedMessage` | ||
|
|
||
| If it is necessary to store the entire message and then rehydrate it in another process, use the following strategy. | ||
| First we can get the raw AMQP message bytes and lock token as shown below: | ||
| *Note: The lock token is not | ||
| actually part of the AMQP message so it needs to stored separately from the AMQP bytes.* | ||
|
|
||
| ```C# Snippet:ServiceBusWriteReceivedMessage | ||
| var client1 = new ServiceBusClient(connectionString); | ||
| ServiceBusSender sender = client1.CreateSender(queueName); | ||
|
|
||
| var message = new ServiceBusMessage("some message"); | ||
| await sender.SendMessageAsync(message); | ||
|
|
||
| ServiceBusReceiver receiver1 = client1.CreateReceiver(queueName); | ||
| ServiceBusReceivedMessage receivedMessage = await receiver1.ReceiveMessageAsync(); | ||
| ReadOnlyMemory<byte> amqpMessageBytes = receivedMessage.GetRawAmqpMessage().ToBytes().ToMemory(); | ||
| ReadOnlyMemory<byte> lockTokenBytes = Guid.Parse(receivedMessage.LockToken).ToByteArray(); | ||
| ``` | ||
|
|
||
| In order to rehydrate the message in another process, we would do the following: | ||
|
|
||
| ```C# Snippet:ServiceBusReadReceivedMessage | ||
| AmqpAnnotatedMessage amqpMessage = AmqpAnnotatedMessage.FromBytes(new BinaryData(amqpMessageBytes)); | ||
| ServiceBusReceivedMessage rehydratedMessage = ServiceBusReceivedMessage.FromAmqpMessage(amqpMessage, new BinaryData(lockTokenBytes)); | ||
|
|
||
| var client2 = new ServiceBusClient(connectionString); | ||
| ServiceBusReceiver receiver2 = client2.CreateReceiver(queueName); | ||
| await receiver2.CompleteMessageAsync(rehydratedMessage); | ||
| ``` | ||
|
|
||
| ## Storing only the lock token | ||
|
|
||
| If the entire message is not needed when settling the message in a different process, you can simply preserve the | ||
| lock token. In the example below, we store off the lock token using its GUID bytes. You can also simply store a | ||
| string if that is easier for your scenario. | ||
|
|
||
| ```C# Snippet:ServiceBusWriteReceivedMessageLockToken | ||
| var client1 = new ServiceBusClient(connectionString); | ||
| ServiceBusSender sender = client1.CreateSender(queueName); | ||
|
|
||
| var message = new ServiceBusMessage("some message"); | ||
| await sender.SendMessageAsync(message); | ||
|
|
||
| ServiceBusReceiver receiver1 = client1.CreateReceiver(queueName); | ||
| ServiceBusReceivedMessage receivedMessage = await receiver1.ReceiveMessageAsync(); | ||
| ReadOnlyMemory<byte> lockTokenBytes = Guid.Parse(receivedMessage.LockToken).ToByteArray(); | ||
| ``` | ||
|
|
||
| In order to rehydrate the message in another process using the lock token, we would do the following: | ||
|
JoshLove-msft marked this conversation as resolved.
Outdated
|
||
| *Note: Because we only stored the lock token, when we rehydrate the message all of the properties of the | ||
| `ServiceBusReceivedMessage` other than `LockToken` will have default values.* | ||
|
|
||
| ```C# Snippet:ServiceBusReadReceivedMessageLockToken | ||
| ServiceBusReceivedMessage rehydratedMessage = ServiceBusModelFactory.ServiceBusReceivedMessage(lockTokenGuid: new Guid(lockTokenBytes.ToArray())); | ||
|
|
||
| var client2 = new ServiceBusClient(connectionString); | ||
| ServiceBusReceiver receiver2 = client2.CreateReceiver(queueName); | ||
| await receiver2.CompleteMessageAsync(rehydratedMessage); | ||
| ``` | ||
91 changes: 91 additions & 0 deletions
91
...cebus/Azure.Messaging.ServiceBus/tests/Samples/Sample16_CrossReceiverMessageSettlement.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core.Amqp; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Azure.Messaging.ServiceBus.Tests.Samples | ||
| { | ||
| public class Sample16_CrossReceiverMessageSettlement : ServiceBusLiveTestBase | ||
| { | ||
| [Test] | ||
| public async Task RehydrateReceivedMessageUsingRawBytes() | ||
| { | ||
| await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) | ||
| { | ||
| #if SNIPPET | ||
| string connectionString = "<connection_string>"; | ||
| string queueName = "<queue_name>"; | ||
| #else | ||
| string connectionString = TestEnvironment.ServiceBusConnectionString; | ||
| string queueName = scope.QueueName; | ||
| #endif | ||
|
|
||
| #region Snippet:ServiceBusWriteReceivedMessage | ||
|
|
||
| var client1 = new ServiceBusClient(connectionString); | ||
| ServiceBusSender sender = client1.CreateSender(queueName); | ||
|
|
||
| var message = new ServiceBusMessage("some message"); | ||
| await sender.SendMessageAsync(message); | ||
|
|
||
| ServiceBusReceiver receiver1 = client1.CreateReceiver(queueName); | ||
| ServiceBusReceivedMessage receivedMessage = await receiver1.ReceiveMessageAsync(); | ||
| ReadOnlyMemory<byte> amqpMessageBytes = receivedMessage.GetRawAmqpMessage().ToBytes().ToMemory(); | ||
| ReadOnlyMemory<byte> lockTokenBytes = Guid.Parse(receivedMessage.LockToken).ToByteArray(); | ||
| #endregion | ||
|
|
||
| #region Snippet:ServiceBusReadReceivedMessage | ||
| AmqpAnnotatedMessage amqpMessage = AmqpAnnotatedMessage.FromBytes(new BinaryData(amqpMessageBytes)); | ||
| ServiceBusReceivedMessage rehydratedMessage = ServiceBusReceivedMessage.FromAmqpMessage(amqpMessage, new BinaryData(lockTokenBytes)); | ||
|
|
||
| var client2 = new ServiceBusClient(connectionString); | ||
| ServiceBusReceiver receiver2 = client2.CreateReceiver(queueName); | ||
| await receiver2.CompleteMessageAsync(rehydratedMessage); | ||
| #endregion | ||
|
|
||
| Assert.AreEqual("some message", rehydratedMessage.Body.ToString()); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task RehydrateReceivedMessageUsingLockToken() | ||
| { | ||
| await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) | ||
| { | ||
| #if SNIPPET | ||
| string connectionString = "<connection_string>"; | ||
| string queueName = "<queue_name>"; | ||
| #else | ||
| string connectionString = TestEnvironment.ServiceBusConnectionString; | ||
| string queueName = scope.QueueName; | ||
| #endif | ||
|
|
||
| #region Snippet:ServiceBusWriteReceivedMessageLockToken | ||
|
|
||
| var client1 = new ServiceBusClient(connectionString); | ||
| ServiceBusSender sender = client1.CreateSender(queueName); | ||
|
|
||
| var message = new ServiceBusMessage("some message"); | ||
| await sender.SendMessageAsync(message); | ||
|
|
||
| ServiceBusReceiver receiver1 = client1.CreateReceiver(queueName); | ||
| ServiceBusReceivedMessage receivedMessage = await receiver1.ReceiveMessageAsync(); | ||
| ReadOnlyMemory<byte> lockTokenBytes = Guid.Parse(receivedMessage.LockToken).ToByteArray(); | ||
| #endregion | ||
|
|
||
| #region Snippet:ServiceBusReadReceivedMessageLockToken | ||
|
|
||
| ServiceBusReceivedMessage rehydratedMessage = ServiceBusModelFactory.ServiceBusReceivedMessage(lockTokenGuid: new Guid(lockTokenBytes.ToArray())); | ||
|
|
||
| var client2 = new ServiceBusClient(connectionString); | ||
| ServiceBusReceiver receiver2 = client2.CreateReceiver(queueName); | ||
| await receiver2.CompleteMessageAsync(rehydratedMessage); | ||
| #endregion | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.