什么是Service Bus Topics/Subscriptions
Service Bus Topics/Subscriptions提供基于发布/订阅模式的消息通信模型,如图所示:
与Service Bus Queues不一样的是,Topics/Subscriptions使用发布/订阅模式实现了一对多的通信。就像我们订阅杂志,有若干个订阅方,同一本杂志可以根据订阅的数量发布给多个订阅者。我们可以理解成每一个Subscription就是一个Queue,发布者将消息发送给Topic,Topic再将消息复制多份,并发送到多个Queue中,且Queue之间不会互相影响,订阅者从对应的Queue中获取消息。在Topic将消息发送给Subscription的时候,可以设定过滤规则,假设发送的消息是订单信息,图中Subscription 1,我们可以设置成只接收来自北京的订单,则Topic只会将来自北京的订单消息发送给Subscription 1。
Service Bus Queues的优势所在同样适用于Service Bus Topics/Subscriptions,请参看Service Bus Queue入门。
如何使用Service Bus Topics/Subscriptions
使用Service Bus Topics/Subscriptions的方法与Service Bus Queues类似,同样需要引用相关程序集以及添加终结点配置,代码如下:
创建Topic
1: string topicName = "DataCollectionTopic";
2: NamespaceManager namespaceClient = NamespaceManager.Create();
3: TopicDescription topicDescription = null;
4: if (namespaceClient.TopicExists(topicName))
5: {
6: topicDescription = namespaceClient.GetTopic(topicName);
7: }
8: else
9: {
10: topicDescription = namespaceClient.CreateTopic(topicName);
11: }
创建Subscription
1: string subscription1 = "Subscription1";
2: string subscription2 = "Subscription2";
3:
4: SubscriptionDescription subscriptionDescription1 = null;
5: SubscriptionDescription subscriptionDescription2 = null;
6:
7: SqlFilter sqlFilter1 = new SqlFilter("IssueTitle = 'Package lost'");
8: SqlFilter sqlFilter2 = new SqlFilter("IssueTitle = 'Product damaged'");
9:
10: subscriptionDescription1 = GetSubscription(topicName, namespaceClient, subscription1, sqlFilter1);
11: subscriptionDescription2 = GetSubscription(topicName, namespaceClient, subscription2, sqlFilter2);
1: private SubscriptionDescription GetSubscription(string topicName, NamespaceManager namespaceClient, string subscriptionName,Filter filter)
2: {
3: SubscriptionDescription subscriptionDescription = null;
4: if (namespaceClient.SubscriptionExists(topicName, subscriptionName))
5: {
6: subscriptionDescription = namespaceClient.GetSubscription(topicName, subscriptionName);
7: }
8: else
9: {
10: subscriptionDescription = namespaceClient.CreateSubscription(topicName, subscriptionName, filter);
11: }
12: return subscriptionDescription;
13: }
在创建Subscription时,我们使用了SqlFilter来做为Subscription的过滤器,SqlFilter对消息的Properties进行过滤。
发送消息
1: MessagingFactory factory = MessagingFactory.Create();
2: TopicClient topicClient = factory.CreateTopicClient(topicName);
3: BrokeredMessage message = new BrokeredMessage();
4: message.Properties.Add("IssueTitle", "Package lost");
5: topicClient.Send(message);
根据过滤规则,这条消息将被发送到subscriptionDescription1里面。
订阅方接收消息
1: string topicName = "DataCollectionTopic";
2: MessagingFactory factory = MessagingFactory.Create();
3: SubscriptionClient client = factory.CreateSubscriptionClient(topicName, subscriptionName, ReceiveMode.PeekLock);
4: BrokeredMessage message = client.Receive(TimeSpan.FromSeconds(1));
5: if (message != null)
6: {
7: Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
8: try
9: {
10: ProcessMessage();
11: message.Complete();
12: }
13: catch
14: {
15: message.Abandon();
16: }
17:
18: }
具体例子采用用Winform客户端程序模拟发布方和订阅方,在订阅方,通过两个backGroundWorker组件来模拟分别从两个订阅中获取数据。
请点击 这里 下载源码。