• MSMQ简例


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Messaging;

    namespace Demo
    {
        public partial class WebForm9 : System.Web.UI.Page
        {
            MyNewQueue myNewQueue = new MyNewQueue();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack) return;           
                myNewQueue.SendMessage();           
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                myNewQueue.ReceiveMessage();
            }
        }

        public class Order
        {
            public int orderId;
            public DateTime orderTime;
        };   

        /// <summary>
        /// Provides a container class for the example.
        /// </summary>
        public class MyNewQueue
        {
            private MessageQueue myQueue;
            public MyNewQueue()
            {
                if (!MessageQueue.Exists(".\\private$\\myQueue"))
                    MessageQueue.Create(".\\private$\\myQueue");
                myQueue = new MessageQueue();
                myQueue.Path = ".\\private$\\myQueue";
            }

            public void SendMessage()
            {
                Order sentOrder = new Order();
                sentOrder.orderId = 3;
                sentOrder.orderTime = DateTime.Now;

                myQueue.Send(sentOrder);

                return;
            }

            public  void ReceiveMessage()
            {
                myQueue.Formatter = new XmlMessageFormatter(new Type[]{typeof(Order)});

                try
                {
                    Message myMessage = myQueue.Receive(new TimeSpan(0, 0, 3));
                    Order myOrder = (Order)myMessage.Body;

                    HttpContext.Current.Response.Write("Order ID: " + myOrder.orderId.ToString());
                    HttpContext.Current.Response.Write("Sent: " + myOrder.orderTime.ToString());
                }

                catch (MessageQueueException)
                {
                }

                // Handle invalid serialization format.
                catch (InvalidOperationException e)
                {

                }
                return;
            }
        }
    }

  • 相关阅读:
    Ubuntu_14.04安装docker
    CentOS配置java运行环境
    github上传自己的开源代码
    eclipse使用maven插件创建web项目
    jar包解压后,修改完配置文件,再还原成jar包
    Python学习的几本建议书籍
    流批
    函数
    程序
    习 题
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2072475.html
Copyright © 2020-2023  润新知