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

基础类:

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:MyMachineName\Private$\OrderQueueName
private static readonly string queuePath =System.Configuration.ConfigurationSettings.AppSettings["OrderQueuePath"];
private static int queueTimeout = ;
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 = "";
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();
}

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

微软消息队列MessageQueue(MQ)的更多相关文章

  1. 微软 消息队列 MessageQueue 简单使用

    1.在服务电脑上打开 消息队列 ①进入控制面板>程序>启用或关闭windows功能 ②将需要的勾选(我自己全选了哈哈哈) ③我的电脑 右键 打开管理 见到消息队列 在专用队列上新建专用队列 ...

  2. .Net下的MSMQ(微软消息队列)的同步异步调用

    一.MSMQ简介 MSMQ(微软消息队列)是Windows操作系统中消息应用程序的基础,是用于创建分布式.松散连接的消息通讯应用程序的开发工具.消息队列 和电子邮件有着很多相似处,他们都包含多个属性, ...

  3. 微软消息队列-MicroSoft Message Queue(MSMQ)队列的C#使用

    目录 定义的接口 接口实现 建立队列工厂 写入队列 获取消息 什么是MSMQ Message Queuing(MSMQ) 是微软开发的消息中间件,可应用于程序内部或程序之间的异步通信.主要的机制是:消 ...

  4. 分布式服务(RPC)+分布式消息队列(MQ)面试题精选

    ​ 分布式系统(distributed system)是建立在网络之上的软件系统.正是因为软件的特性,所以分布式系统具有高度的内聚性和透明性.因此,网络和分布式系统之间的区别更多的在于高层软件(特别是 ...

  5. 【转】MSMQ 微软消息队列 简单 示例

    MSMQ它的实现原理是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为Message),然后把它保存至一个系统公用空间的消息队列(Message Queue)中:本地或者是异地的消息接收程 ...

  6. .net微软消息队列(msmq)简单案例

    1.首先我们需要安装消息队列服务,它是独立的消息记录的服务,并保存在硬盘文件中. 我们添加名为:DMImgUpload的私有消息队列. 2.定义消息队列的连接字符串建议采用IP: (1)FormatN ...

  7. 阿里云消息队列(MQ)服务

    A.首先在阿里云上申请消息队列MQ服务: B.然后创建一个Topic(主题,一级主题):然后创建生产者与消费者: C.不过此时还没有结束 ,还需要创建一个AccessKey和AccessSecret( ...

  8. 常用的消息队列中间件mq对比

    原文地址:https://blog.csdn.net/qq_30764991/article/details/80239076 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量 ...

  9. 消息队列(MQ)

    什么是消息队列 消息队列,即MQ,Message Queue. 消息队列是典型的:生产者.消费者模型.生产者不断向消息队列中生产消息,消费者不断的从队列中获取消息.因为消息的生产和消费都是异步的,而且 ...

随机推荐

  1. springmvc中的web.xml配置(包含中文乱码解决)

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  2. Vue组件之间通信的三种方式

    最近在看梁颠编著的<Vue.js实战>一书,感觉颇有收获,特此记录一些比价实用的技巧. 组件是MVVM框架的核心设计思想,将各功能点组件化更利于我们在项目中复用,这类似于我们服务端面向对象 ...

  3. 数据采集框架Gobblin简介

    问题导读: Gobblin的架构设计是怎样的? Gobblin拥有哪些组建,如何实现可扩展? Gobblin采集执行流程的过程? 前面我们介绍Gobblin是用来整合各种数据源的通用型ETL框架,在某 ...

  4. 面试中的一些小问题之ES5和ES6的区别?

    1995年,JavaScript作为网景浏览器的一部分首次发布,起初并不叫JavaScript,而是叫LiveScript,但是因为当时Java正火,也算是为了搭上java的顺风车,于是改成了Java ...

  5. CMD命令行提示被禁用的情况下如何继续使用命令行工具执行命令

    1.直接在Windows搜索 左下 输入要执行的 CMD 命令单句.多句同时执行没有试出来. 暂时记录这一条.2016-12-20

  6. UI绘图与信息、样式

    UI的绘图功能是在视图尺寸确定以后,将信息和样式绘制的过程: 给信息和样式的修改提供的最后的机会. UILabel 控制行段间距的终极代码. class LineSpaceLabel: UILabel ...

  7. 牛客多校Round 10

    咕咕咕.... 去烽火台和兵马俑了

  8. [BOI2008]Elect 选举

    背包. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstri ...

  9. Oracle 数据库启动与关闭 各种方式详解整理

    概述 只有具备sysdba和sysoper系统特权的用户才能启动和关闭数据库. 在启动数据库之前应该启动监听程序,否则就不能利用命令方式来管理数据库,包括启动和关闭数据库. 虽然数据库正常运行,但如果 ...

  10. Python判断字符串是否全是字母或数字

    str.isnumeric(): True if 只包含数字:otherwise False.注意:此函数只能用于unicode string str.isdigit(): True if 只包含数字 ...