Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化

在上一篇中,我们介绍了消息的顺序收发保证:

Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证

在本文中我们主要介绍下复杂对象消息是否需要支持序列化以及消息的持久化。

在实际的业务应用开发中,我们经常会将复杂业务对象放到消息里面,实现异构系统之间的集成、模块间的解耦等等。

同时,我们还比较关注消息队列服务是否支持消息的持久化,消息队列如果宕机后持久化的消息是否可以还原?

在Azure Messaging的官方说明中,没有特地的介绍复杂对象消息是否需要支持序列化的要求,但是,我们在上篇博文中,有个消息创建方法,as following,

BrokeredMessage类的构造函数:

//
// Summary:
// Constructor that creates a BrokeredMessage from a given object using the
// provided XmlObjectSerializer
//
// Parameters:
// serializableObject:
// The serializable object.
//
// serializer:
// The serializer object.
//
// Exceptions:
// System.ArgumentNullException:
// Thrown when null serializer is passed to the method with a non-null serializableObject
//
// Remarks:
// You should be aware of the exceptions that their provided Serializer can
// throw and take appropriate actions. Please refer to for a possible list of
// exceptions and their cause.
public BrokeredMessage(object serializableObject, XmlObjectSerializer serializer);

看来消息的构造,支持动态传入XmlObjectSerializer, so,

         /// <summary>
/// 构造消息
/// </summary>
/// <param name="serializableObject">可序列化的对象</param>
/// <returns>消息</returns>
public BrokeredMessage Create(Object serializableObject)
{
var serializer = new DataContractSerializer(serializableObject.GetType(),
new DataContractSerializerSettings() { IgnoreExtensionDataObject = true, PreserveObjectReferences = true });
var message = new BrokeredMessage(serializableObject, serializer);
message.Properties.Add("Type", serializableObject.GetType().ToString()); return message;
}

接下来,我们用上一篇中的代码,做一个复杂对象消息收发的测试,我们还是用上次的SalesOrder类,但是增加一个SalesOrderItem集合和双向关联,来描述销售订单和销售订单明细的的1:n的业务领域模型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AzureMessaging.FIFO
{
/// <summary>
/// 销售订单类
/// </summary>
public class SalesOrder
{
/// <summary>
/// 订单ID
/// </summary>
public string OrderID { get; set; } /// <summary>
/// 订单编号
/// </summary>
public string Code { get; set; } /// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; } /// <summary>
/// 总价格
/// </summary>
public Decimal TotalPrice { get; set; } /// <summary>
/// 产品ID
/// </summary>
public int ProductID { get; set; } private List<SalesOrderItem> items; /// <summary>
/// 销售订单明细
/// </summary>
public List<SalesOrderItem> Items
{
get
{
if (items == null)
items = new List<SalesOrderItem>(); return items;
}
set
{
items = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AzureMessaging.FIFO
{
/// <summary>
/// 销售订单明细
/// </summary>
public class SalesOrderItem
{
/// <summary>
/// 标识
/// </summary>
public string ID { get; set; } /// <summary>
/// 客户ID
/// </summary>
public int CustomerID { get; set; } /// <summary>
/// 所属的销售订单ID
/// </summary>
public string SalesOrderID
{
get
{
if (Order != null)
return Order.OrderID; return string.Empty;
}
} /// <summary>
/// 所属的销售订单
/// </summary>
public SalesOrder Order { get; set; }
}
}

创建销售订单实例类方法:

  private static SalesOrder CreateSalesOrder(int i)
{
var order = new SalesOrder() { OrderID = i.ToString(), Code = "SalesOrder_" + i, CreateTime = DateTime.Now, ProductID = , TotalPrice = new decimal() };
order.Items.Add(new SalesOrderItem() { ID = Guid.NewGuid().ToString(), Order = order, CustomerID = }); return order;
}

在构造SalesOrder和SalesOrderItems时,我们做了双向关联。
消息顺序收发测试:

 using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AzureMessaging.FIFO
{
class Program
{
private static readonly string queueName = "OrderQueue";
static void Main(string[] args)
{
MessageSend();
Console.ReadKey(); MessageReceive();
Console.ReadKey();
} /// <summary>
/// 发送消息
/// </summary>
private static void MessageSend()
{
var sbUtils = new ServiceBusUtils(); //创建队列
sbUtils.CreateQueue(queueName, false); //顺序发送消息到OrderQueue
var queueSendClient = sbUtils.GetQueueClient(queueName);
for (int i = ; i < ; i++)
{
var order = CreateSalesOrder(i);
var message = sbUtils.Create(order);
queueSendClient.Send(message);
Console.WriteLine(string.Format("Send {0} MessageID: {1}", i, message.MessageId));
} Console.WriteLine("Send Completed!");
} /// <summary>
/// 接收消息
/// </summary>
private static void MessageReceive()
{
int index = ;
BrokeredMessage msg = null;
var sbUtils = new ServiceBusUtils();
var queueReveiveClient = sbUtils.GetReceiveQueueClient(queueName, ReceiveMode.ReceiveAndDelete);
while ((msg = queueReveiveClient.Receive(TimeSpan.FromMilliseconds())) != null)
{
Console.WriteLine(string.Format("Received {0} MessageID: {1}", index, msg.MessageId));
index++;
} ////删除队列
//sbUtils.DeleteQueue(queueName); Console.WriteLine("Receive Completed!");
} private static SalesOrder CreateSalesOrder(int i)
{
var order = new SalesOrder() { OrderID = i.ToString(), Code = "SalesOrder_" + i, CreateTime = DateTime.Now, ProductID = , TotalPrice = new decimal() };
order.Items.Add(new SalesOrderItem() { ID = Guid.NewGuid().ToString(), Order = order, CustomerID = }); return order;
}
}
}

Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化

可以看出,复杂对象消息只要指定适当的XmlObjectSerializer,即可。

在双向引用这种领域模型的设计场景下,我们配置了PreserveObjectReferences = true

var serializer = new DataContractSerializer(serializableObject.GetType(),
new DataContractSerializerSettings() { IgnoreExtensionDataObject = true, PreserveObjectReferences = true });

解决了序列化时循环引用的问题。
关于消息的持久化,Azure messaging有官方的说明:所有的队列都是持久化的,持久化存储是SQL Server,不提供内存中的消息队列。

毕竟是PaaS层的消息队列服务,消息的持久化和高可用性微软还是有保障的。

Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化

本篇中我们介绍并验证了Azure Messaging Service Bus复杂对象消息是否需要支持序列化和消息持久化,下一篇我们继续介绍消息的重复发送问题。

周国庆

2017/3

上一篇:OC之Block的用法和实现委托


下一篇:ubuntu server 18.04的安装 以及配置网络还有ssh服务