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();
                }
            }
    

      

  • 相关阅读:
    iframe应用 相互之间发送消息 postMessage
    function angular.bootstrap()
    总结
    1-angular.bind
    jQuery treeTable v 1.4.2
    声明了一个模块和一个控制器AngularJS的处理过程
    angularJs $templateCache
    $.fn.zTree 的使用
    Java异常throws与throw的区别
    Pom报错
  • 原文地址:https://www.cnblogs.com/jake-ge/p/5526964.html
Copyright © 2011-2022 走看看