• 微软消息队列MessageQueue(MQ)


     首先本地安装微软的消息队列服务器。

    基础类:

    namespace Core.MessageQueueTest
    {
        public class TestQueue : IDisposable
        {
            protected MessageQueueTransactionType transactionType = MessageQueueTransactionType.Automatic;
            protected MessageQueue queue;
            protected TimeSpan timeout;
            public TestQueue(string queuePath, int timeoutSeconds)
            {
                queue = new MessageQueue(queuePath);
                timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeoutSeconds));
    
                // Performance optimization since we don't need these features
                queue.DefaultPropertiesToSend.AttachSenderId = false;
                queue.DefaultPropertiesToSend.UseAuthentication = false;
                queue.DefaultPropertiesToSend.UseEncryption = false;
                queue.DefaultPropertiesToSend.AcknowledgeType = AcknowledgeTypes.None;
                queue.DefaultPropertiesToSend.UseJournalQueue = false;
            }
    
            /// <summary>
            /// Derived classes call this from their own Receive methods but cast
            /// the return value to something meaningful.
            /// </summary>
            public virtual object Receive()
            {
                try
                {
                    using (Message message = queue.Receive(timeout, transactionType))
                        return message;
                }
                catch (MessageQueueException mqex)
                {
                    if (mqex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                        throw new TimeoutException();
    
                    throw;
                }
            }
    
            /// <summary>
            /// Derived classes may call this from their own Send methods that
            /// accept meaningful objects.
            /// </summary>
            public virtual void Send(object msg)
            {
                queue.Send(msg, transactionType);
            }
    
            #region IDisposable Members
            public void Dispose()
            {
                queue.Dispose();
            }
            #endregion
    
        }
    }

    测试类:

    namespace Core.MessageQueueTest
    {
        public class QueueTestOrder : TestQueue
        {
            // Path example - FormatName:DIRECT=OS:MyMachineNamePrivate$OrderQueueName
            private static readonly string queuePath =System.Configuration.ConfigurationSettings.AppSettings["OrderQueuePath"];
            private static int queueTimeout = 20;
            public QueueTestOrder()
                : base(queuePath, queueTimeout)
            {
                // Set the queue to use Binary formatter for smaller foot print and performance
                queue.Formatter = new BinaryMessageFormatter();
            }
            /// <summary>
            /// Method to retrieve order messages from Message Queue
            /// </summary>
            /// <returns>All information for an order</returns>
            public new OrderInfo Receive()
            {
                // This method involves in distributed transaction and need Automatic Transaction type
                base.transactionType = MessageQueueTransactionType.Automatic;
                return (OrderInfo)((Message)base.Receive()).Body;
            }
    
            public OrderInfo Receive(int timeout)
            {
                base.timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeout));
                return Receive();
            }
    
            /// <summary>
            /// Method to send asynchronous order to  Message Queue
            /// </summary>
            /// <param name="orderMessage">All information for an order</param>
            public void Send(OrderInfo orderMessage)
            {
                // This method does not involve in distributed transaction and optimizes performance using Single type
                base.transactionType = MessageQueueTransactionType.Single;
                base.Send(orderMessage);
            }
        }
         [Serializable]
        public class OrderInfo
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }
    }

    测试:

     protected void btn_testSendToQueue_Click(object sender, EventArgs e)
            {
                OrderInfo model = new OrderInfo();
                model.Id = "1";
                model.Name = "测试MQ";
                QueueTestOrder order = new QueueTestOrder();
                order.Send(model);
            }
    
            protected void btn_reciveFromQueue_Click(object sender, EventArgs e)
            {
                QueueTestOrder order = new QueueTestOrder();
                OrderInfo model = new OrderInfo();
                model = (OrderInfo)order.Receive(10);
            }

    测试结果:发送接收信息 正常

  • 相关阅读:
    Chrome Native Messaging 与本地程序之间的通信
    由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
    linux
    maven POM.xml 标签详解 转
    java-线程观察整个生命周期
    java-Web项目开发中Spring整合Junit单元测试
    java-Map集合的四种遍历方式
    Java-8内置的核心函数式接口接口
    正则表达式
    Java-Atomic系列12大类实例讲解和原理分解
  • 原文地址:https://www.cnblogs.com/dean-Wei/p/8005510.html
Copyright © 2020-2023  润新知