微软云消息队列 Azure service bus queue
--更新:Bug 修复
The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue
消息dequeue时增加auto complete
public static async Task MessageDequeueAsync()
{
// Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of Concurrent calls to the callbackProcessMessagesAsync
, set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,// Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
// False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
AutoComplete = false
}; // Register the queue message handler and receive messages in a loop
queueClient.RegisterMessageHandler(
async (message, token) =>
{
// Process the message
await PostMessageToBot(message);
loggerCore.Information($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// Complete the message so that it is not received again.
// This can be done only if the queueClient is opened in ReceiveMode.PeekLock mode.
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
},
//async (exceptionEvent) =>
//{
// // Process the exception
// loggerCore.Error($"WeChat message dequeue exception:{exceptionEvent.Exception.Message}");
//}
messageHandlerOptions);
}
2.在Azure上设置duplication detect时间,由1~59s,设置为55s,大笔试lock duration 时间长,可供消费的时间。
前言
第一次使用消息队列,遇到了一些问题:同一个消息有多次出列。是一个消息只入列一次,还是多次?还是因为出列问题,出列了多次?
Microsoft Azure service bus queue
Azure service bus queue在Azure上创建一个service bus,在service bus 上创建一个 queue,创建的时候注意 enable duplicate detected message这个选项选上,防止消息重复入列。
找到SAS Policy: RootManageSharedAccessKey 的值,复制下来,用作connectstring。
Azure service bus sample on github
配置servicebus 的queue
namespace:Microsoft.Azure.ServiceBus;
初始化queueClient:
private static readonly Serilog.ILogger loggerCore = LoggerCoreFactory.GetLoggerCore();
const string ServiceBusConnectionString = "Endpoint=sb://{YOUR-NAMESPACE-ON-Azure}.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey={YOUR-KEYS}";
const string QueueName = "{YOUR-QUEUE-NAME}";
static IQueueClient queueClient;
static MessageQueueHandler()
{
queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
}
消息入列
这里添加messageID 作为入列依据,防止同一消息多次入列,然后不用频繁的关闭消息连接,影响性能。
public static async Task MessageEnqueueAsync(string message)
{
// Send messages.
await SendMessagesAsync(message);
// don't close frequently
//await queueClient.CloseAsync();
}
static async Task SendMessagesAsync(string messageXML)
{
try
{
// Create a new message to send to the queue.
var messageStr = ParseMessageType.Parse(messageXML);
var msgId = messageStr.Body.MsgId.Value;
var message = new Microsoft.Azure.ServiceBus.Message
{
MessageId = msgId,// avoid same message enqueue more than once
Body = Encoding.UTF8.GetBytes(messageXML),
};
loggerCore.Information($"message enqueue:{messageXML}");
// Send the message to the queue.
await queueClient.SendAsync(message);
}
catch (Exception exception)
{
loggerCore.Error($"message enqueue error:{exception.ToString()}");
}
}
出列
public static async Task MessageDequeueAsync()
{
// please choice PeekLock mode
queueClient = new QueueClient(ServiceBusConnectionString, QueueName, ReceiveMode.PeekLock);
// Register the queue message handler and receive messages in a loop
queueClient.RegisterMessageHandler(
async (message, token) =>
{
// Process the message
await PostMessageToBot(message);
loggerCore.Information($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// Complete the message so that it is not received again.
// This can be done only if the queueClient is opened in ReceiveMode.PeekLock mode.
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
},
async (exceptionEvent) =>
{
// Process the exception
loggerCore.Error($"WeChat message dequeue exception:{exceptionEvent.ToString()}");
});
}
这样消息就会根据入列的先后,逐次出列。
后记
几个有帮助的链接分享一下:
1.Read best practice. 就是如何最好的使用Azure queue,避免不需要的开销。
2.Enable queue duplicate detection 启用消息重复检测项,防止同一消息多次额enqueue。
微软云消息队列 Azure service bus queue的更多相关文章
- 【Microsoft Azure学习之旅】测试消息队列(Service Bus Queue)是否会丢消息
组里最近遇到一个问题,微软的Azure Service Bus Queue是否可靠?是否会出现丢失消息的情况? 具体缘由如下, 由于开发的产品是SaaS产品,为防止消息丢失,跨Module消息传递使用 ...
- 如何在ASP.NET Core中使用Azure Service Bus Queue
原文:USING AZURE SERVICE BUS QUEUES WITH ASP.NET CORE SERVICES 作者:damienbod 译文:如何在ASP.NET Core中使用Azure ...
- Windows Azure Service Bus (2) 队列(Queue)入门
<Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...
- Azure Service Bus(二)在NET Core 控制台中如何操作 Service Bus Queue
一,引言 上一篇讲到关于 Azure ServiceBus 的一些概念,讲到 Azure Service Bus(服务总线),其实也叫 "云消息服务",是微软在Azure 上提供的 ...
- Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别
<Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...
- 阿里云ONS而微软Azure Service Bus体系结构和功能比较
阿里云ONS而微软Azure Service bus体系结构和功能比较 版权所有所有,转载请注明出处http://blog.csdn.net/yangzhenping.谢谢! 阿里云的开放消息服务: ...
- Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue
<Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...
- 【服务总线 Azure Service Bus】ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题
Azure Service Bus 死信队列产生的原因 服务总线中有几个活动会导致从消息引擎本身将消息推送到 DLQ. 如 超过 MaxDeliveryCount 超过 TimeToLive 处理订阅 ...
- 【Azure 服务总线】Azure Service Bus中私信(DLQ - Dead Letter Queue)如何快速清理
在博文ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题一文中,介绍了服务总线产生私信的原因及可以通过代码的方式来清楚私信队列中的消息,避免长期占用空间(因为私信中 ...
随机推荐
- hadoop cdh5的pig隐式转化(int到betyarray)不行了
cdh3上,pig支持int到chararray的隐式转化,但到cdh5不行. pig code is as follows: %default Cleaned_Log /user/usergroup ...
- 【嵌入式开发】嵌入式 开发环境 (远程登录 | 文件共享 | NFS TFTP 服务器 | 串口连接 | Win8.1 + RedHat Enterprise 6.3 + Vmware11)
作者 : 万境绝尘 博客地址 : http://blog.csdn.net/shulianghan/article/details/42254237 一. 相关工具下载 嵌入式开发工具包 : -- 下 ...
- 2015 Objective-C 三大新特性
Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时,也有很多像我一样工作上依然 ...
- Shell入门之概念
1.一切皆是文件: 在bash Shell 中一切皆是文件,不管是我们认为的文本文件,还是那些文件夹的东西,在这里都是文件,Linux只管比特和字节流,而不关心他们最终组成了什么格式,这些工作交给在L ...
- 一键安装gitlab7在rehl6.4上
一键安装gitlab7在rehl6.4上 参考原文: http://blog.csdn.net/ubuntu64fan/article/details/38367579 1 关于gitlab7 无论如 ...
- (三十六)利用AFNetworking进行JSON数据解析
1.首先要安装CocoaPods,然后在需要AFNetworking的工程根目录建立podfile,内容如下: platform :ios, '7.0' pod 'AFNetworking' 2.然后 ...
- 【一天一道LeetCode】#53. Maximum Subarray
一天一道LeetCode系列 (一)题目 Find the contiguous subarray within an array (containing at least one number) w ...
- 《java入门第一季》之面向对象(谈谈接口)
软件中有接口,这里的接口与硬件中的接口还是有很大区别的. 这里介绍接口不考虑JDK8的新特性(JDK8开始接口里面可以有非抽象方法了,介绍JDK8新特性可能要到整个第一季写完吧!) 还是直接进入接口的 ...
- scrapy模拟登录微博
http://blog.csdn.net/pipisorry/article/details/47008981 这篇文章是介绍使用scrapy模拟登录微博,并爬取微博相关内容.关于登录流程为嘛如此设置 ...
- 使用Ext JS,不要使用页面做组件重用,尽量不要做页面跳转
今天,有人请教我处理办法,问题是: 一个Grid,选择某条记录后,单击编辑后,弹出编辑窗口(带编辑表单),编辑完成后单击保存按钮保存表单,并关闭窗口,刷新Grid. 这,本来是很简单的,但囿于开发人员 ...