首页 > 其他分享 >MSMQ微软消息队列的学习(先进先出)

MSMQ微软消息队列的学习(先进先出)

时间:2022-12-12 23:07:00浏览次数:41  
标签:string MSMQ 队列 public new path Message 先进先出

学习通过MSMQ发送简单类型的消息和复杂类型的消息

看代码:

namespace MSMQ
{
class Program
{
static void Main(string[] args)
{
const string path = @".\private$\myQueue";
MyQueue.Createqueue(path);
MyQueue.SendMessage(path, "OK1");//队列,先进先出
MyQueue.SendMessage(path, "Ok2");
MyQueue.SendMessage(path, "Ok3");
MyQueue.ReceiveMessage(path);

MyQueue.SendMessage(path, new Book { BookId = 1, BookName = "Code Complete", BookPrice = 98, BookAuthor = "zzl" });
MyQueue.SendMessage(path, new Book { BookId = 2, BookName = "Move Method", BookPrice = 47, BookAuthor = "zzl" });

Console.WriteLine(MyQueue.ReceiveEntityMessage(path));
Console.ReadKey();
}

}

/// <summary>
/// MSMQ消息队列
/// </summary>
public static class MyQueue
{
/// <summary>
/// 通过Create方法创建使用指定路径的新消息队列
/// </summary>
/// <param name="queuePath"></param>
public static void Createqueue(string queuePath)
{
try
{
if (!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(queuePath);
}
else
{
Console.WriteLine(queuePath + "已经存在!");
}
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
}

/// <summary>
/// 连接消息队列并发送消息到队列
/// </summary>
public static void SendMessage(string path, string msg)
{
try
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(path);

Message myMessage = new Message();
myMessage.Body = msg;
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
//发送消息到队列中
myQueue.Send(myMessage);
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
}


/// <summary>
/// 连接消息队列并从队列中接收消息
/// </summary>
public static void ReceiveMessage(string path)
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(path);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
try
{
//从队列中接收消息
Message myMessage = myQueue.Receive();
string context = (string)myMessage.Body; //获取消息的内容
Console.WriteLine("消息内容为:" + context);
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
catch (InvalidCastException e)
{
Console.WriteLine(e.Message);
}
}

/// <summary>
/// 清空指定队列的消息
/// </summary>
public static void ClearMessage(string path)
{
MessageQueue myQueue = new MessageQueue(path);
myQueue.Purge();
}

/// <summary>
/// 连接队列并获取队列的全部消息
/// </summary>
public static void GetAllMessage(string path)
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(path);
Message[] message = myQueue.GetAllMessages();
XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
foreach (Message t in message)
{
t.Formatter = formatter;
Console.WriteLine(t.Body.ToString());
}
}

/// <summary>
/// 连接消息队列并发送消息到队列
/// </summary>
public static bool SendMessage(string path, Book book)
{
bool flag = false;
try
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(path);

System.Messaging.Message myMessage = new System.Messaging.Message(book);
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(Book) });
//发送消息到队列中
myQueue.Send(myMessage);
flag = true;
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
return flag;
}

/// <summary>
/// 连接消息队列并从队列中接收消息
/// </summary>
public static string ReceiveEntityMessage(string path)
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(path)
{
Formatter = new XmlMessageFormatter(new Type[] { typeof(Book) })
};
try
{
//从队列中接收消息
System.Messaging.Message myMessage = myQueue.Peek();
Book book = myMessage.Body as Book; //获取消息的内容
return string.Format("编号:{0},书名:{1},作者:{2},定价:{3}",
book.BookId,
book.BookName,
book.BookAuthor,
book.BookPrice);
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
catch (InvalidCastException e)
{
Console.WriteLine(e.Message);
}
return null;
}


}
public interface IEntity { }
public class Book : IEntity
{
public int BookId { get; set; }
public string BookName { get; set; }
public string BookAuthor { get; set; }
public double BookPrice { get; set; }
}

}

作者:仓储大叔,张占岭,
荣誉:微软MVP

标签:string,MSMQ,队列,public,new,path,Message,先进先出
From: https://blog.51cto.com/u_15765017/5932117

相关文章

  • 秒懂:JCTool 的 Mpsc 超高性能无锁队列 (史上最全+10W字长文)
    文章很长,而且持续更新,建议收藏起来,慢慢读!疯狂创客圈总目录博客园版为您奉上珍贵的学习资源:免费赠送:《尼恩Java面试宝典》持续更新+史上最全+面试必备2000页+面......
  • 剑指offer 字符流中第一个不重复的字符(思维,队列)
    题目描述请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“g......
  • zookeeper和消息队列kafka
    一、Zookeeper是什么?1、Zookeeper服务集群的条件2、Zookeeper工作机制3、Zookeeper数据结构4、Zookeper特点5、Zookeeper选举机制6、Zookeeper应用场景二、Zookeepe......
  • 优先队列算法
    publicclassBinaryHeap<AnyTypeextendsComparable<?superAnyType>>{privatestaticfinalintDEFAULT_CAPACITY=10;privateintcurrentSize;privat......
  • IIS 之 连接数、并发连接数、最大并发工作线程数、队列长度、最大工作进程数
    http://t.zoukankan.com/l1pe1-p-7742936.html一、IIS连接数一般购买过虚拟主机的朋友都熟悉购买时,会限制IIS连接数,顾名思义即为IIS服务器可以同时容纳客户请求的最......
  • 迭代器源码分析【栈与队列】
      拓展:栈与队列 ......
  • 232.用栈实现队列
    232.用栈实现队列力扣题目链接(opensnewwindow)使用栈实现队列的下列操作:push(x)--将一个元素放入队列的尾部。pop()--从队列首部移除元素。peek()--返回队列......
  • 队列(代码源)
    第一题队列#include<bits/stdc++.h>usingnamespacestd;constintN=100010;intq[N];intfront=1,rear=0;intmain(){ intop;cin>>op; while(op--) { stri......
  • 用两个栈实现队列
    请用栈实现一个队列,支持如下四种操作:push(x)–将元素x插到队尾;pop()–将队首的元素弹出,并返回该元素;peek()–返回队首元素;empty()–返回队列是否为空;class......
  • 队列之王: Disruptor 原理、架构、源码 一文穿透
    文章很长,而且持续更新,建议收藏起来,慢慢读!疯狂创客圈总目录博客园版为您奉上珍贵的学习资源:免费赠送:《尼恩Java面试宝典》持续更新+史上最全+面试必备2000页+面......