zoukankan      html  css  js  c++  java
  • MSMQ

    1.安装MSMQ

    2.添加私有的队列

    3.MSMQ可以发送的类型可以是任意类型,包括类

            static string strServer = @"FormatName:Direct=TCP:10.7.46.42private$msmq";
    
            static void Main(string[] args)
            {
                DeleteAllMessage();
                SendMessage();
                ReveiveMessage();
            }
    
            public static void DeleteAllMessage()
            {
                MessageQueue myQueue = new MessageQueue(strServer);
                myQueue.Purge();        //删除此队列中包含的所有消息
                myQueue.Dispose();
            }
    
            public static void SendMessage()
            {
                MessageQueue myQueue = new MessageQueue(strServer);
                Message myMessage = new Message();
                try
                {
                    myMessage.Body = new Person() { Name = "jake", Age = 29, Birthday = Convert.ToDateTime("1987/07/20") };
                    myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(Person) });
                    //设置消息发送的优先级别
                    myMessage.Priority = MessagePriority.Highest; //最高消息优先级
                    myQueue.Send(myMessage);
                }
                catch(Exception ex)
                {
    
                }
                finally
                {
                    myMessage.Dispose();
                    myQueue.Dispose();
                }
    
            }
    
            public static void ReveiveMessage()
            {
                //连接到本地队列
                MessageQueue myQueue = new MessageQueue(strServer);
                myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Person) });
                Message myMessage = null;
                try
                {
                    int count = myQueue.GetAllMessages().Length;
                    //从队列中接收消息
                    //Peek: 返回队列中第一条消息的副本,而不从队列中移除该消息
                    //Receive: 接收队列中的第一条消息,但不将它从队列中移除
                    //PeekById: 返回具有指定消息标识符的消息的副本,但不从队列中移除消息
                    //ReceiveById: 接收匹配给定标识符的消息,并将其从队列中移除
                    myMessage = myQueue.Receive();
                    Person p = (Person)myMessage.Body; //获取消息的内容
                    Console.WriteLine("Name:" + p.Name);
                    Console.WriteLine("Age:" + p.Age.ToString());
                    Console.WriteLine("Birthday:" + p.Birthday.ToString("yyyy/MM/dd"));
                    count = myQueue.GetAllMessages().Length;
                    Console.ReadLine();
    
                }
                catch
                {
                    Console.WriteLine("error4");
                }
                finally
                {
                    myMessage.Dispose();
                    myQueue.Dispose();
                }
            }
    

      

  • 相关阅读:
    zmodem协议的使用(SecureCRT)
    Centos6.5下安装php
    CentOS6.5下Mysql数据库的安装与配置
    YUM更换源--yum找不到安装包(转)
    百度Ueditor编辑器的Html模式自动替换样式的解决方法
    TextArea里Placeholder换行问题
    tp可用的超强第三方图表类库-JpGraph
    确保 PHP 应用程序的安全
    在线网站速度测试
    几款整站抓取的工具
  • 原文地址:https://www.cnblogs.com/jake-ge/p/5526964.html
Copyright © 2011-2022 走看看