基于微软的消息队列(序列化)
队列本身是一个软件,数据库也是一个软件,存储在内存中,但是内存中也不足够保存呢? 而且停电呢? 怎么办呢? 要保证队列的可靠性,所以我们需要使用单独的队列软件。消息队列,很高级的软件,大型的服务器 MicrosoftMagicQ,存在磁盘,安全可靠 还有事务的操作性 处理大量的数据 相比的好处就是相当地块 迅速存储大量的数据 为日后争取到时间 那么我们怎么放呢? 那么就必须对放入的数据进行序列化
把内存中的信息序列化 变成信息队列 进行网络传输
利用反射 可以写通用的程序 写了好几种序列化的程序 2进制的序列化程序
namespace SerializationSample {
class Program {
static void Main(string[] args) {
Person jobs = new Person(); jobs.Name = \"Jobs\"; jobs.Age = 50; jobs.Sex = \"meal\";
// 使用反射,取得对象的类型 System.Type type = typeof(Person);
// 利用反射取得所有的公共字段
System.Reflection.FieldInfo[] fields = type.GetFields();
foreach (System.Reflection.FieldInfo field in fields) {
Console.WriteLine(field.Name); Console.WriteLine(field.FieldType);
Console.WriteLine(\"——————————————————————\"); //从哪个对象获取值
Console.WriteLine(field.GetValue(jobs)); Console.WriteLine(); }
//[System.Serializable]可序列化标签
// 下面看一下系统提供的二进制序列化程序
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// 提供一个字节流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将对象序列化
formatter.Serialize(ms, jobs);
byte[] buffer = ms.ToArray();
Console.WriteLine(BitConverter.ToString(buffer)); Console.Read();
} } }
XML 2进制 Json的 2进制速度快 省内存 其他方式慢,占内存 但是是可以不序列化的 [System.Serializable] [System.NonSerialized]
Is Serializable接口
Xml序列化 soap协议
要加DLL system.runtime . Serialization.Formatters
基于soap协议的xml的序列化
Json方式下 {“age”:50}
序列化器很多种,因为应对很多不同的浏览器、服务器、程序等等 分布式开发的基础
必须要装消息队列软件 MSSQ 程序 打开与关闭windows信息队列
公共队列是server的
System messaging 创建消息队列
检查 删除 创建 使用
Send发对象 对象被序列化,然后发进去。会查看到一个小信封
还可以在队列中读取消息 但是是被序列话之后的消息 所以必须要做一个反序列化 所以需要指定一个正确的序列化器 这个时候就需要注意一下了 怎么使用序列化器呢?
如何查看我们的信息呢?
namespace MessageSample {
class Program {
static void Main(string[] args) {
//也可以使用代码来创建和删除消息队列
if (System.Messaging.MessageQueue.Exists(\".\\\\Private$\\\\order\")) {
System.Messaging.MessageQueue.Delete(\".\\\\Private$\\\\order\"); Console.WriteLine(\"已经删除 Order 消息队列\"); Console.ReadLine(); }
// 创建
System.Messaging.MessageQueue.Create(\".\\\\Private$\\\\order\"); Console.ReadLine();
// 使用消息队列
System.Messaging.MessageQueue queue
= new System.Messaging.MessageQueue(\".\\\\Private$\\\\order\");
// 发送消息
queue.Send(\"Hello, world.\");
// 以后,可以在队列中读取消息
System.Messaging.Message msg = queue.Receive();
// 消息中保存的是已经序列化之后的信息 // 需要指定正确的序列化器
System.Messaging.XmlMessageFormatter formatter = new System.Messaging.XmlMessageFormatter( new System.Type[] { typeof( string) } );
msg.Formatter = formatter;
// 获取信息
string hello = msg.Body as string; Console.WriteLine(hello); Console.Read();
} } }
查看实际信息 默认是XML的
那么应用在我们的petshop中就应该是
在工具中定义好我们用消息队列存储账目的信息的方法,这个时候 是传入的是 OrderModel insert与get呢? 是先删除 再新增 而get呢 是 获取抓取 再转
// 以后,可以在队列中读取消息
System.Messaging.Message msg = queue.Receive();
// 消息中保存的是已经序列化之后的信息 // 需要指定正确的序列化器
System.Messaging.XmlMessageFormatter formatter = new System.Messaging.XmlMessageFormatter( new System.Type[] { typeof( string) } );
msg.Formatter = formatter;
// 获取信息
string hello = msg.Body as string; Console.WriteLine(hello); Console.Read(); } }
由同步方式有异步(消息队列)的方式 呵呵
那么就要序列化 然后存为消息队列即可 但是
那么怎么处理呢?OrderProcess控制台的程序 还可以利用多线程来进行处理,这个就是里面最复杂的部分
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务