微软消息队列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: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)的更多相关文章
- 微软 消息队列 MessageQueue 简单使用
1.在服务电脑上打开 消息队列 ①进入控制面板>程序>启用或关闭windows功能 ②将需要的勾选(我自己全选了哈哈哈) ③我的电脑 右键 打开管理 见到消息队列 在专用队列上新建专用队列 ...
- .Net下的MSMQ(微软消息队列)的同步异步调用
一.MSMQ简介 MSMQ(微软消息队列)是Windows操作系统中消息应用程序的基础,是用于创建分布式.松散连接的消息通讯应用程序的开发工具.消息队列 和电子邮件有着很多相似处,他们都包含多个属性, ...
- 微软消息队列-MicroSoft Message Queue(MSMQ)队列的C#使用
目录 定义的接口 接口实现 建立队列工厂 写入队列 获取消息 什么是MSMQ Message Queuing(MSMQ) 是微软开发的消息中间件,可应用于程序内部或程序之间的异步通信.主要的机制是:消 ...
- 分布式服务(RPC)+分布式消息队列(MQ)面试题精选
分布式系统(distributed system)是建立在网络之上的软件系统.正是因为软件的特性,所以分布式系统具有高度的内聚性和透明性.因此,网络和分布式系统之间的区别更多的在于高层软件(特别是 ...
- 【转】MSMQ 微软消息队列 简单 示例
MSMQ它的实现原理是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为Message),然后把它保存至一个系统公用空间的消息队列(Message Queue)中:本地或者是异地的消息接收程 ...
- .net微软消息队列(msmq)简单案例
1.首先我们需要安装消息队列服务,它是独立的消息记录的服务,并保存在硬盘文件中. 我们添加名为:DMImgUpload的私有消息队列. 2.定义消息队列的连接字符串建议采用IP: (1)FormatN ...
- 阿里云消息队列(MQ)服务
A.首先在阿里云上申请消息队列MQ服务: B.然后创建一个Topic(主题,一级主题):然后创建生产者与消费者: C.不过此时还没有结束 ,还需要创建一个AccessKey和AccessSecret( ...
- 常用的消息队列中间件mq对比
原文地址:https://blog.csdn.net/qq_30764991/article/details/80239076 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量 ...
- 消息队列(MQ)
什么是消息队列 消息队列,即MQ,Message Queue. 消息队列是典型的:生产者.消费者模型.生产者不断向消息队列中生产消息,消费者不断的从队列中获取消息.因为消息的生产和消费都是异步的,而且 ...
随机推荐
- 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [ [ 1, 5, 9], [ ...
- 231 Power of Two 2的幂
给定一个整数,写一个函数来判断它是否是2的幂. 详见:https://leetcode.com/problems/power-of-two/description/ Java实现: class Sol ...
- 专题三:自定义Web服务器
前言: 经过前面的专题中对网络层协议和HTTP协议的简单介绍相信大家对网络中的协议有了大致的了解的, 本专题将针对HTTP协议定义一个Web服务器,我们平常浏览网页通过在浏览器中输入一个网址就可以看到 ...
- 使用SpringMvc的一个注意事项
在Intelij Idea下,如果在新建项目时使用了自带的模板,那么自动生成的web.xml里的DispatcherServlet配置节点默认的servlet-mapping是这样的: 而习惯上,我们 ...
- spring用来干什么,解决的问题
// 1. 实体类 class User{ } //2. dao class UserDao{ .. 访问db } //3. service class UserService{ UserDao ...
- nginx平滑升级实战
Nginx 平滑升级 1.查看旧版Nginx的编译参数 [root@master ~]# /usr/local/nginx/sbin/nginx -V [root@master ~]# ll ngin ...
- Windows下运行jekyll,编码已不再是问题
很久没更新jekyll了,所以好奇着去官网看了下更新记录,发现如下更新条目(版本1.3.0/2013-11-04发布): Add encoding configuration option (#144 ...
- TWaver 3D应用于大型数据中心(续)
在2014年11月份,我们当时发了一篇有关TWaver HTML5 3D应用于大型数据中心的文章,该blog比较详细的描述一些常用的功能的实现方法,比如:动态添加机柜,告警,温度,湿度等相关的功能的具 ...
- MONO Design创建电信3D机房
前面我们简单介绍了下一分钟创建3D机房,实则mono Design的功能远远不止这些,试想一下,如果我们花上10分钟来创建一个电信机房,那么MONO design又会给我们带来什么样的惊喜呢? 我们从 ...
- ibatis常用16条SQL语句
(1) 输入参数为单个值 <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" p ...