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#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP,CRM,企业进销存等各种数据库管理系统开发。Asp.net,Asp.net mvc,Webservice,WCF, Webapi等服务程序开发。
基于Oracle MySQL MSSql postgresql各种数据库的管理系统数据同步服务。以及基于MapXtreme, Arcgis Engine ,以及基于Arcgis for silverlight/Javascript的WebGIS等相关的GIS系统二次开发。基于Windows 10 Mobile的移动端开发方案。针对各种系统的二次开发维护,并提供相关开发的技术性支持,如程序BUG解决,应用系统架构,技术难题攻克等相关技术服务。
联系方式: QQ :80163278(devgis) 邮箱:devgis@qq.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗