--更新:Bug 修复

The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue

  1. 消息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 callback ProcessMessagesAsync, 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的更多相关文章

  1. 【Microsoft Azure学习之旅】测试消息队列(Service Bus Queue)是否会丢消息

    组里最近遇到一个问题,微软的Azure Service Bus Queue是否可靠?是否会出现丢失消息的情况? 具体缘由如下, 由于开发的产品是SaaS产品,为防止消息丢失,跨Module消息传递使用 ...

  2. 如何在ASP.NET Core中使用Azure Service Bus Queue

    原文:USING AZURE SERVICE BUS QUEUES WITH ASP.NET CORE SERVICES 作者:damienbod 译文:如何在ASP.NET Core中使用Azure ...

  3. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  4. Azure Service Bus(二)在NET Core 控制台中如何操作 Service Bus Queue

    一,引言 上一篇讲到关于 Azure ServiceBus 的一些概念,讲到 Azure Service Bus(服务总线),其实也叫 "云消息服务",是微软在Azure 上提供的 ...

  5. Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别

    <Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...

  6. 阿里云ONS而微软Azure Service Bus体系结构和功能比较

    阿里云ONS而微软Azure Service bus体系结构和功能比较 版权所有所有,转载请注明出处http://blog.csdn.net/yangzhenping.谢谢! 阿里云的开放消息服务: ...

  7. Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue

    <Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...

  8. 【服务总线 Azure Service Bus】ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题

    Azure Service Bus 死信队列产生的原因 服务总线中有几个活动会导致从消息引擎本身将消息推送到 DLQ. 如 超过 MaxDeliveryCount 超过 TimeToLive 处理订阅 ...

  9. 【Azure 服务总线】Azure Service Bus中私信(DLQ - Dead Letter Queue)如何快速清理

    在博文ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题一文中,介绍了服务总线产生私信的原因及可以通过代码的方式来清楚私信队列中的消息,避免长期占用空间(因为私信中 ...

随机推荐

  1. Java-IO之InputStreamReader和OutputStreamWriter

    InputStreamReader和OutputStreamWriter是字节流通向字符流的桥梁.它使用指定的差染色体读写字节并将其解码为字符.InputStreamReader的作用是将字节输入流转 ...

  2. struts2国际化全例 错误解决

    在struts2中需要做国际化的有: jsp页面的国际化,action错误信息的国际化,转换错误信息的国际化,校验错误信息的国际化 在之前的例子中已经做过和国际化相关的例子了,在struts.xml中 ...

  3. Docker教程:使用docker配置python开发环境

    http://blog.csdn.net/pipisorry/article/details/50808034 Docker的安装和配置 [Docker教程:docker的安装] [Docker教程: ...

  4. 重载重写重定义-易混淆概念-C++编译器处理方式

    1.函数重载 1)必须在同一个类中进行. 2)子类无法重载父类的函数,父类同名函数将被名称覆盖 3)重载是在编译期间根据参数类型和个数决定函数调用 2.函数重写 1)必须发生于父类与子类之间 2)并且 ...

  5. 设计比较好,有助于学习的Github上的iOS App源码 (中文)

    Github版 中文 : TeamTalk 蘑菇街. 开源IM. 电商强烈推荐. MyOne-iOS 用OC写的<一个> iOS 客户端 zhihuDaily 高仿知乎日报 Coding ...

  6. 多线程爬虫Miner

    多线程爬虫Miner 需要配置项:1.URL包含关键字.2.存储方式:DB-数据库存储;FILE-文件存储.3.爬取页面最大深度.4.下载页面线程数.5.分析页面线程数.6.存储线程数. ------ ...

  7. UNIX环境高级编程——标准I/O库缓冲区和内核缓冲区的区别

    1.C标准库的I/O缓冲区         UNIX的传统 是Everything is a file,键盘.显示器.串口.磁盘等设备在/dev 目录下都有一个特殊的设备文件与之对应,这些设备文件也可 ...

  8. jdbc连接mysql加载驱动程序com.mysql.jdbc.Driver

    在开发环境如eclipse,中加载指定数据库的驱动程序.需要下载MySQL支持JDBC的驱动程序mysql-connector-java-5.1.25-bin.jar. 而具体在Java程序中加载驱动 ...

  9. Leetcode_112_Path Sum

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41910495 Given a binary tree an ...

  10. Adapterview和adapter的联系

    在J2EE中提供过一种非常好的框架--MVC框架,实现原理:数据模型M(Model)存放数据,利用控制器C(Controller)将数据显示在视图V(View)上.在Android中有这样一种高级控件 ...