|
| 1 | +// |
| 2 | +// Copyright © Microsoft Corporation, All Rights Reserved |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS |
| 11 | +// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
| 12 | +// ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A |
| 13 | +// PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT. |
| 14 | +// |
| 15 | +// See the Apache License, Version 2.0 for the specific language |
| 16 | +// governing permissions and limitations under the License. |
| 17 | + |
| 18 | +namespace AutoForward |
| 19 | +{ |
| 20 | + using Microsoft.Azure.ServiceBus; |
| 21 | + using Microsoft.Azure.ServiceBus.Core; |
| 22 | + using System; |
| 23 | + using System.Text; |
| 24 | + using System.Threading.Tasks; |
| 25 | + |
| 26 | + // This sample demonstrates how to automatically forward messages from a queue, |
| 27 | + // subscription, or deadletter queue into another queue or topic. |
| 28 | + // The sample assumes prior setup of a topology of Service Bus entities |
| 29 | + // as described in the accompanying README file. |
| 30 | + public class Program : MessagingSamples.Sample |
| 31 | + { |
| 32 | + public async Task Run(string connectionString) |
| 33 | + { |
| 34 | + Console.WriteLine("\nSending messages\n"); |
| 35 | + |
| 36 | + // Create sender and send message M1 into the source topic |
| 37 | + var topicSender = new MessageSender(connectionString, "AutoForwardSourceTopic"); |
| 38 | + await topicSender.SendAsync(CreateMessage("M1")); |
| 39 | + |
| 40 | + // Create sender and send message M2 directly into the target queue |
| 41 | + var queueSender = new MessageSender(connectionString, "AutoForwardTargetQueue"); |
| 42 | + await queueSender.SendAsync(CreateMessage("M2")); |
| 43 | + |
| 44 | + // Create the receiver on the target queue |
| 45 | + Console.WriteLine("\nReceiving messages\n"); |
| 46 | + var targetQueueReceiver = new MessageReceiver(connectionString, "AutoForwardTargetQueue"); |
| 47 | + for (int i = 0; i < 2; i++) |
| 48 | + { |
| 49 | + // We are expecting twp messages to arrive into the target queue. |
| 50 | + // 1) Message M2 has been sent directly |
| 51 | + // 2) Message M1 has been auto-forwarded from a subscription on the source topic |
| 52 | + var message = await targetQueueReceiver.ReceiveAsync(TimeSpan.FromSeconds(10)); |
| 53 | + if (message != null) |
| 54 | + { |
| 55 | + await this.PrintReceivedMessage(message); |
| 56 | + await targetQueueReceiver.CompleteAsync(message.SystemProperties.LockToken); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + throw new Exception("Expected message not received."); |
| 61 | + } |
| 62 | + } |
| 63 | + await targetQueueReceiver.CloseAsync(); |
| 64 | + } |
| 65 | + |
| 66 | + async Task PrintReceivedMessage(Message receivedMessage) |
| 67 | + { |
| 68 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 69 | + await Console.Out.WriteLineAsync(string.Format("Received message:\n" + "\tLabel:\t{0}\n" + "\tBody:\t{1}\n", receivedMessage.Label, Encoding.UTF8.GetString(receivedMessage.Body))); |
| 70 | + foreach (var p in receivedMessage.UserProperties) |
| 71 | + { |
| 72 | + await Console.Out.WriteLineAsync(string.Format("\tProperty:\t{0} = {1}", p.Key, p.Value)); |
| 73 | + } |
| 74 | + Console.ResetColor(); |
| 75 | + } |
| 76 | + |
| 77 | + // Create a new Service Bus message. |
| 78 | + public static Message CreateMessage(string label) |
| 79 | + { |
| 80 | + // Creat1e a Service Bus message. |
| 81 | + var msg = new Message(Encoding.UTF8.GetBytes("This is the body of message \"" + label + "\".")); |
| 82 | + msg.UserProperties.Add("Priority", 1); |
| 83 | + msg.UserProperties.Add("Importance", "High"); |
| 84 | + msg.Label = label; |
| 85 | + msg.TimeToLive = TimeSpan.FromSeconds(90); |
| 86 | + return msg; |
| 87 | + } |
| 88 | + |
| 89 | + public static int Main(string[] args) |
| 90 | + { |
| 91 | + try |
| 92 | + { |
| 93 | + var app = new Program(); |
| 94 | + app.RunSample(args, app.Run); |
| 95 | + } |
| 96 | + catch (Exception e) |
| 97 | + { |
| 98 | + Console.WriteLine(e.ToString()); |
| 99 | + return 1; |
| 100 | + } |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments