Azure Messaging-ServiceBus Messaging消息队列技术系列2-编程SDK入门
各位,上一篇基本概念和架构中,我们介绍了Window Azure ServiceBus的消息队列技术的概览。接下来,我们进入编程模式和详细功能介绍模式,一点一点把ServiceBus技术研究出来。
本章我们主要介绍ServiceBus的编程SDK编程入门。
首先,微软提供了两个主要的Nuget Packages:
Microsoft Azure Service Bus 3.4.0
主要的NameSpace有以下几个:
1. Microsoft.ServiceBus,这个下面有两个主要的类:TokenProvider(用于创建Azure ServiceBus连接Token)NamespaceManager(管理ServiceBus的命名空间)。
2. Microsoft.ServiceBus.Messaging,这个命名空间下面主要提供了:MessageSession、BrokeredMessage、QueueClient、TopicClient、TopicDescription、QueueDescription、SubscriptionClient、SubscriptionDescription等核心类。
在正式编码之前,需要我们在Windows Azure的Portal上建立ServiceBus的NameSpace:

我们新建一个命名空间:servicebustest,选择的类型是:消息

新建完成后处于活动(可用)的状态:

接下来,我们要获取两个重要的配置:连接串和主秘钥


请将连接字符串拷贝下来,备用。

请将主秘钥拷贝下来,备用。
启动我们的ServiceBus的连接编码,首先需要在应用程序配置文件中增加ServiceBus的连接信息:
<appSettings>
<!-- Service Bus specific app setings for messaging connections -->
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://servicebustest.servicebus.chinacloudapi.cn/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=主秘钥"/>
</appSettings>
后续,所有的ServiceBus的连接操作,都会读取这个配置节。我们通过case by case的方式,show 给大家:
首先:ServiceBus的命名空间管理器:Microsoft.ServiceBus.NamespaceManager
这里,我们主要用以下两个方法来实例化:
public static NamespaceManager Create(); 例如:
var namespaceClient = NamespaceManager.Create();
通过NamespaceManager这个类,我们可以创建、删除、获取、重命名、更新、判断是否存在:队列、主题、订阅、规则等
1. 消息队列的创建、是否存在判断、删除操作
private void CreateQueue(string queueName, bool isRequiresSession = true)
{
var nm = NamespaceManager.Create();
if (nm.QueueExists(queueName))
{
nm.DeleteQueue(queueName);
} var queue = new QueueDescription(queueName) { RequiresSession = isRequiresSession };
nm.CreateQueue(queue); nm.DeleteQueue(queueName);
}
2. 创建QueueClient,用于发送、接收消息
/// <summary>
/// 创建队列客户端
/// </summary>
/// <returns>队列客户端</returns>
public QueueClient CreateQueueClient(string queueName, ReceiveMode mode = ReceiveMode.ReceiveAndDelete)
{
var namespaceClient = NamespaceManager.Create();
return QueueClient.Create(queueName,mode);
}
3. 主题的创建、删除、是否存在判断
private void CreateTopic(string topicName)
{
var nm = NamespaceManager.Create();
if (nm.TopicExists(topicName))
{
nm.DeleteTopic(topicName);
} var topic = new TopicDescription(topicName);
nm.CreateTopic(topic); nm.DeleteTopic(topicName);
}
4. 创建TopicClient,用于主题的发送和订阅接收消息
/// <summary>
/// 创建主题客户端
/// </summary>
/// <returns>主题客户端</returns>
public TopicClient GetTopicClient(string topicName)
{
var namespaceClient = NamespaceManager.Create();
return TopicClient.Create(topicName);
}
5. 创建消息BrokeredMessage,设置消息的属性
/// <summary>
/// 构造消息
/// </summary>
/// <param name="serializableObject">可序列化的对象</param>
/// <returns>消息</returns>
public static BrokeredMessage Create(Object serializableObject)
{
var serializer = new DataContractSerializer(serializableObject.GetType(),new DataContractSerializerSettings(){ IgnoreExtensionDataObject = true, PreserveObjectReferences = false});
var message = new BrokeredMessage(serializableObject);
message.Properties.Add("Type", serializableObject.GetType().ToString()); return message;
}
6.发送消息
/// <summary>
/// 发送多条消息
/// </summary>
/// <param name="receivePayBills">收发货订单集合</param>
public void Send(List<ReceivePayBill> receivePayBills)
{
var queueClient = GetQueueClient(queueName);
foreach (var receivePayBill in receivePayBills)
{
var message = this.Create(receivePayBill);
queueClient.Send(message);
} queueClient.Close();
}
7. 接收消息
/// <summary>
/// 接收消息
/// </summary>
/// <returns>收付款订单</returns>
public List<ReceivePayBill> Receive()
{
var bills = new List<ReceivePayBill>();
var queueClient = GetReceiveQueueClient(queueName, ReceiveMode.ReceiveAndDelete);
BrokeredMessage message;
while ((message = queueClient.Receive()) != null)
{
bills.Add(message.GetBody<ReceivePayBill>());
} return bills;
}
还有很多其他的示例代码。本文只是一个简单的入门级教程,接下来我们将按MQ场景逐个展示ServiceBus Messaging的特性。
Azure Messaging-ServiceBus Messaging消息队列技术系列2-编程SDK入门的更多相关文章
- Window Azure ServiceBus Messaging消息队列技术系列2-编程SDK入门
各位,上一篇基本概念和架构中,我们介绍了Window Azure ServiceBus的消息队列技术的概览.接下来,我们进入编程模式和详细功能介绍模式,一点一点把ServiceBus技术研究出来. 本 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证
上一篇:Window Azure ServiceBus Messaging消息队列技术系列2-编程SDK入门 http://www.cnblogs.com/tianqing/p/5944573.ht ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列-索引篇
Azure Messaging ServiceBus Messaging相关的技术系列,最近已经整理了不少了,统一做一个索引链接,置顶. 方便查找,并后续陆陆续续再增加. 学习消息队列技术,可以先看第 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
在上一篇中,我们介绍了消息的顺序收发保证: Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证 在本文中我们主要介绍下复杂对象消息是否需要支持序列 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列8-服务总线配额
上篇博文中我们介绍了Azure ServiceBus Messaging的消息事务机制: Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务(2017 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列5-重复消息:at-least-once at-most-once
上篇博客中,我们用实际的业务场景和代码示例了Azure Messaging-ServiceBus Messaging对复杂对象消息的支持和消息的持久化: Azure Messaging-Service ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列6-消息回执
上篇博文中我们介绍了Azure Messaging的重复消息机制.At most once 和At least once. Azure Messaging-ServiceBus Messaging消息 ...
- Window Azure ServiceBus Messaging消息队列技术系列1-基本概念和架构
前段时间研究了Window Azure ServiceBus Messaging消息队列技术,搞了很多技术研究和代码验证,最近准备总结一下,分享给大家. 首先,Windows Azure提供了两种类型 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列1-基本概念和架构
前段时间研究了Window Azure ServiceBus Messaging消息队列技术,搞了很多技术研究和代码验证,最近准备总结一下,分享给大家. 首先,Windows Azure提供了两种类型 ...
随机推荐
- 1、python同级目录及子目录模块引入
2个模块在同一个包内时(即引入和被引入的2个py文件在同一个目录下),直接引入模块名 1.引入与被引入模块或包在同一目录下时,直接引入模块名或者包名import modulename.py或者impo ...
- iframe 和 父窗口传递
iframe 向父窗口 window.parent.postMessage('向父窗口传递值',*); 父窗口向 iframe 内部子窗口传值 documnet.querySelector('ifra ...
- IE报错:[vuex] vuex requires a Promise polyfill in this browser.
使用的是vue2.0版本 IE报错提醒: 导致原因:使用了 ES6 中用来传递异步消息的的Promise,而IE的浏览器不支持 解决办法: 1.安装babel-polyfill模块,babel-plo ...
- 【Mock】mock基础、简单的单元测试代码练习。
说到接口测试,必问 mock,mock 通俗一点来说就是模拟接口返回.解决接口的依赖关系,主要是为了解耦,单元测试用的多. 什么是Mock unittest.mock 是一个用于在 Python 中进 ...
- LeetCode-111.Mininum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 火币网API文档——Websocket 请求与订阅示例
1. 访问地址 Pro 站行情请求地址为:wss://api.huobipro.com/ws HADAX 站行情请求地址为:wss://api.hadax.com/ws 2. 数据压缩 WebSock ...
- 数据库---mysql内置功能
一.视图 简介: 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用.使用视图我们可以把查询过 ...
- php循环方法实现先序、中序、后序遍历二叉树
二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). <?php class Node { public $v ...
- dfs1321
比较抽象吧,看到题时一点思想也没有,参考了别人的代码才知道...渣渣 #include <iostream>#include <stdio.h>#include <str ...
- [LeetCode] 35. Search Insert Position_Easy tag: Binary Search
Given a sorted array and a target value, return the index if the target is found. If not, return the ...