C# 消息队列源码
2022/6/18 1:22:26
本文主要是介绍C# 消息队列源码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Messaging; namespace MSMQTest { public class TestMessageQueue { private string Path; /// <summary> /// 1.通过Create方法创建使用指定路径的新消息队列 /// </summary> /// <param name="queuePath"></param> public void Createqueue(string queuePath) { try { if (!MessageQueue.Exists(queuePath)) { MessageQueue.Create(queuePath); } else { Console.WriteLine(queuePath + "已经存在!"); //MessageQueue.Delete(queuePath); //MessageQueue.Create(queuePath); //Console.WriteLine(queuePath + "删除重建"); } Path = queuePath; } catch (MessageQueueException e) { Console.WriteLine(e.Message); } } /// <summary> /// 2.连接消息队列并发送消息到队列 /// 远程模式:MessageQueue rmQ = new MessageQueue("FormatName:Direct=OS:machinename//private$//queue"); /// rmQ.Send("sent to regular queue - Atul");对于外网的MSMQ只能发不能收 /// </summary> public void SendMessage() { try { //连接到本地队列 MessageQueue myQueue = new MessageQueue(Path); //MessageQueue myQueue = new MessageQueue("FormatName:Direct=TCP:192.168.12.79//Private$//myQueue1"); //MessageQueue rmQ = new MessageQueue("FormatName:Direct=TCP:121.0.0.1//private$//queue");--远程格式 Message myMessage = new Message(); myMessage.Body = "消息内容34kuangbo去死"; myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); //发生消息到队列中 myQueue.Send(myMessage); Console.WriteLine("消息发送成功!"); Console.ReadLine(); } catch (ArgumentException e) { Console.WriteLine(e.Message); } } /// <summary> /// 3.连接消息队列并从队列中接收消息 /// </summary> public void ReceiveMessage() { MessageQueue myQueue = new MessageQueue(Path); myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); try { //从队列中接收消息 Message myMessage = myQueue.Receive();// myQueue.Peek();--接收后不消息从队列中移除 string context = myMessage.Body.ToString(); Console.WriteLine("消息内容:" + context); Console.ReadLine(); } catch (MessageQueueException e) { Console.WriteLine(e.Message); } catch (InvalidCastException e) { Console.WriteLine(e.Message); } } /// <summary> /// 4.清空指定队列的消息 /// </summary> public void ClealMessage() { MessageQueue myQueue = new MessageQueue(Path); myQueue.Purge(); Console.WriteLine("已清空对了{0}上的所有消息", Path); } /// <summary> /// 5.连接队列并获取队列的全部消息 /// </summary> public void GetAllMessage() { MessageQueue myQueue = new MessageQueue(Path); Message[] allMessage = myQueue.GetAllMessages(); XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); for (int i = 0; i < allMessage.Length; i++) { allMessage[i].Formatter = formatter; Console.WriteLine("第{0}机密消息为:{1}", i + 1, allMessage[i].Body.ToString()); } Console.ReadLine(); } } } //program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MSMQTest { class Program { static void Main(string[] args) { MyMessageQueue queue = new MyMessageQueue(); string qpath = "FormatName:Direct=TCP:192.168.0.102//private$//corp"; //".//Private$//myQueue2" qpath = ".\\private$\\corp"; queue.Createqueue(qpath); queue.SendMessage(); queue.GetAllMessage(); queue.ReceiveMessage(); //queue.ClealMessage(); Console.Read(); } } }
这篇关于C# 消息队列源码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具