Azure Messaging-ServiceBus Messaging
Azure Messaging-ServiceBus Messaging
上篇博文中我们介绍了Azure Messaging的重复消息机制、At most once 和At least once.
Azure Messaging-ServiceBus Messaging消息队列技术系列5-重复消息:at-least-once at-most-once
本文中我们主要研究并介绍Azure Messaging的消息回执机制:实际应用场景:
同步收发场景下,消息生产者和消费者双向应答模式,例如:张三写封信送到邮局中转站,然后李四从中转站获得信,然后在写一份回执信,放到中转站,然后张三去取,当然张三写信的时候就得写明回信地址。还
有,生成订单编号场景,发送一个生成订单编号的消息,消息消费者接收生成订单编号的消息,并通过消息回执返回。
Azure Messaging的消息回执机制主要通过:基于带会话的Queue/Topic、SessionId、ReplyTo属性来实现
在代码实现中,我们需要:
1. 两个工作线程,一个线程用于消息发送和接收回执消息,一个线程用于消息接收和发送消息回执。
2. 一个会话标识:ReceiptSession
3. 两个队列Queue:RequestQueue:发送消息、接收消息,ResponseQueue:发送回执消息,接收回执消息。
直接Show Code:
首先,我们在ServiceBusMQManager增加一个线程安全的创建带回话的QueueClient方法:

private static object syncObj = new object();
/// <summary>
/// 获取要求会话带Session的QueueClient
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>QueueClient</returns>
public QueueClient GetSessionQueueClient(string queueName)
{
var namespaceClient = NamespaceManager.Create();
if (!namespaceClient.QueueExists(queueName))
{
lock (syncObj)
{
if (!namespaceClient.QueueExists(queueName))
{
var queue = new QueueDescription(queueName) { RequiresSession = true };
namespaceClient.CreateQueue(queue);
}
}
} return QueueClient.Create(queueName, ReceiveMode.ReceiveAndDelete);
}

然后我们定义一些常量:

private static readonly string ReplyToSessionId = "ReceiptSession";
const double ResponseMessageTimeout = 20.0;
private static readonly string requestQueueName = "RequestQueue";
private static readonly string responseQueueName = "ResponseQueue";

实现发送并接收回执消息的方法:

/// <summary>
/// 发送并接收回执消息
/// </summary>
/// <param name="bills"></param>
public static void SendMessage()
{
var manager = new ServiceBusUtils();
var responseClient = manager.GetSessionQueueClient(responseQueueName);
var requestClient = manager.GetSessionQueueClient(requestQueueName); var messsageReceiver = responseClient.AcceptMessageSession(ReplyToSessionId);
var order = CreateSalesOrder(1); //发送消息
var message = new BrokeredMessage(order);
message.Properties.Add("Type", order.GetType().ToString());
message.SessionId = ReplyToSessionId;
message.MessageId = "OrderMessage001";
message.ReplyTo = responseQueueName;
requestClient.Send(message);
Console.WriteLine("Send message: " + message.MessageId + ", SalesOrder ID: " + order.OrderID); //接收消息回执
var receivedMessage = messsageReceiver.Receive(TimeSpan.FromSeconds(ResponseMessageTimeout * 2)); var receivedOrder = receivedMessage.GetBody<SalesOrder>();
Console.WriteLine("Receive receipt message: " + receivedMessage.MessageId + ", SalesOrder ID: " + receivedOrder.OrderID);
messsageReceiver.Close();
}

实现接收消息并发送回执方法:

1 /// <summary>
2 /// 接收消息并回执
3 /// </summary>
4 public static void ReceiveMessage()
5 {
6 var manager = new ServiceBusUtils();
7
8 var requestClient = manager.GetSessionQueueClient(requestQueueName);
9 var session = requestClient.AcceptMessageSession();
10 var requestMessage = session.Receive();
11
12 if (requestMessage != null)
13 {
14 var receivedOrder = requestMessage.GetBody<SalesOrder>();
15 Console.WriteLine("Receive message: " + requestMessage.MessageId + ", SalesOrder ID: " + receivedOrder.OrderID);
16
17 var responseMessage = new BrokeredMessage(receivedOrder);
18 responseMessage.Properties.Add("Type", receivedOrder.GetType().ToString());
19 responseMessage.ReplyToSessionId = ReplyToSessionId;
20 responseMessage.MessageId = "ResponseOrderMessage001";
21 responseMessage.SessionId = requestMessage.SessionId;
22
23 //发送回执消息
24 var responseClient = manager.GetSessionQueueClient(requestMessage.ReplyTo);
25 responseClient.Send(responseMessage);
26 Console.WriteLine("Send receipt message: " + responseMessage.MessageId + ", SalesOrder ID: " + receivedOrder.OrderID);
27 }
28 }

Main方法中,启动两个工作线程:一个线程用于消息发送和接收回执消息,一个线程用于消息接收和发送消息回执。
因为涉及到Azure Messaging中队列的第一次创建,Azure Messaging是不支持多个请求同时创建同一个队列的,因此,我们两个线程间做一个简单的Task.Delay(3000).Wait();

1 static void Main(string[] args)
2 {
3 var sendTask = Task.Factory.StartNew(() => { SendMessage(); });
4 Task.Delay(3000).Wait();
5 var receiveTask = Task.Factory.StartNew(() => { ReceiveMessage(); });
6
7 Task.WaitAll(sendTask, receiveTask);
8
9 Console.ReadKey();
10 }

我们看看程序输出:

Azure 服务总线中的队列:

可以看出:Azure Messaging-ServiceBus Messaging 基于带会话的Queue/Topic、SessionId、ReplyTo属性来实现消息回执机制。
Azure Messaging-ServiceBus Messaging的更多相关文章
- Window Azure ServiceBus Messaging消息队列技术系列2-编程SDK入门
各位,上一篇基本概念和架构中,我们介绍了Window Azure ServiceBus的消息队列技术的概览.接下来,我们进入编程模式和详细功能介绍模式,一点一点把ServiceBus技术研究出来. 本 ...
- Window Azure ServiceBus Messaging消息队列技术系列1-基本概念和架构
前段时间研究了Window Azure ServiceBus Messaging消息队列技术,搞了很多技术研究和代码验证,最近准备总结一下,分享给大家. 首先,Windows Azure提供了两种类型 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列-索引篇
Azure Messaging ServiceBus Messaging相关的技术系列,最近已经整理了不少了,统一做一个索引链接,置顶. 方便查找,并后续陆陆续续再增加. 学习消息队列技术,可以先看第 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证
上一篇:Window Azure ServiceBus Messaging消息队列技术系列2-编程SDK入门 http://www.cnblogs.com/tianqing/p/5944573.ht ...
- 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消息队列技术系列1-基本概念和架构
前段时间研究了Window Azure ServiceBus Messaging消息队列技术,搞了很多技术研究和代码验证,最近准备总结一下,分享给大家. 首先,Windows Azure提供了两种类型 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列2-编程SDK入门
各位,上一篇基本概念和架构中,我们介绍了Window Azure ServiceBus的消息队列技术的概览.接下来,我们进入编程模式和详细功能介绍模式,一点一点把ServiceBus技术研究出来. 本 ...
- Azure Messaging
上篇博文中我们介绍了Azure ServiceBus Messaging的消息事务机制: Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务(2017 ...
- Storm-源码分析- Messaging (backtype.storm.messaging)
先定义两个接口和一个类 TaskMessage类本身比较好理解, 抽象storm的message格式 对于IContext, 注释也说了, 定义messaging plugin, 通过什么渠道去发送m ...
随机推荐
- Virtualizing memory type
A processor, capable of operation in a host machine, including memory management logic to support a ...
- C语言深度剖析-----数组基础
数组的概念 数组的大小 实例 内存占用 长度 a[5] 不指定初始值的话,随机给数值 数组地址与数组名 a为数组首地址,&a为数组地址,值相等,意义不同 数组名不可以直接相等 例:主义区分指针 ...
- UVA 11889 - Benefit 可直接枚举
看题传送门 题目大意: 输入两个整数A和C,求最小的整数B,使得lcm(A,B)=C.如果无解,输出NO SOLUTION 思路: A*B=C*gcd(A,B) 所以 B / gcd(A,B) = C ...
- sum()函数——MATLAB
a=sum(A) %列求和 b=sum(A,2) %行求和 c=sum(A(:)) %矩阵求和 假定A为一个矩阵: sum(A)以矩阵A的每一列为对象,对一列内的数字求和. sum(A,2)以矩阵A ...
- 恢复SLAVE上的某几张表的简要方法
同步报错是遇到最多的一个问题,如果你修复后发现还没有解决,通常的方法就是在Master上重新dump出一份,然后在slave上恢复.这个方法是针对整个库不是很大的情况下使用的,那如果是较大,全部dum ...
- [转]在Win7 64位注册ActiveX控件
原文作者: K_H_H 原文地址: http://blog.sina.com.cn/s/blog_56b96c5a0100ztc7.html 首先必须以管理员身份运行cmd.exe,即在cmd.ex ...
- Swift入坑系列—集合类型
数组(Arrays) 字典(Dictionaries) 数组(Arrays) 在OC里面,NSArray和NSMutableArray这两个类可以存储任意类型的对象,并且不提供所返回对象的任何特别信息 ...
- php面试题二--解决网站大流量高并发方案(从url到硬盘来解决高并发方案总结)
php面试题二--解决网站大流量高并发方案(从url到硬盘来解决高并发方案总结) 一.总结 从外到内解决网站大流量高并发问题---从提交一个url开始(从用户按下搜索栏回车键开始) url最开始会到d ...
- Vim 在 windows 下的应用
常用命令的学习. 第一部分 Esc:返回到 正常模式 h j k l:左下上右 x:删除字符(normal mode) :q!:放弃所有更改并退出vim :wq:保存所有更改并退出vim i:进入编辑 ...
- Android Widget和悬浮窗 原理
1.简单介绍 Android widget是桌面插件,在android系统应用开发层面有特殊用途. AppWidget是把一个进程的控件嵌入到别外一个进程的窗口里的一种方法.悬浮窗的效果与Widget ...